<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[Freeswitch-trunk][16306] </title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<div id="header">FreeSWITCH Subversion</div>
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://fisheye.freeswitch.org/changelog/FreeSWITCH?cs=16306">16306</a></dd>
<dt>Author</dt> <dd>verifier</dd>
<dt>Date</dt> <dd>2010-01-14 08:08:33 -0600 (Thu, 14 Jan 2010)</dd>
</dl>

<h3>Log Message</h3>
<pre>Rewrote the parser + trying to validate contained events.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#freeswitchtrunkcontribverifierEventSockettrunkFreeSwitchEventSocketTestParserTestcs">freeswitch/trunk/contrib/verifier/EventSocket/trunk/FreeSwitch.EventSocket.Test/ParserTest.cs</a></li>
<li><a href="#freeswitchtrunkcontribverifierEventSockettrunkFreeSwitchEventSocketTestProgramcs">freeswitch/trunk/contrib/verifier/EventSocket/trunk/FreeSwitch.EventSocket.Test/Program.cs</a></li>
<li><a href="#freeswitchtrunkcontribverifierEventSockettrunkFreeSwitchEventSocketEventSocketcs">freeswitch/trunk/contrib/verifier/EventSocket/trunk/FreeSwitchEventSocket/EventSocket.cs</a></li>
<li><a href="#freeswitchtrunkcontribverifierEventSockettrunkFreeSwitchEventSocketEventsEventBasecs">freeswitch/trunk/contrib/verifier/EventSocket/trunk/FreeSwitchEventSocket/Events/EventBase.cs</a></li>
<li><a href="#freeswitchtrunkcontribverifierEventSockettrunkFreeSwitchEventSocketFreeSwitchEventSocketcsproj">freeswitch/trunk/contrib/verifier/EventSocket/trunk/FreeSwitchEventSocket/FreeSwitch.EventSocket.csproj</a></li>
<li><a href="#freeswitchtrunkcontribverifierEventSockettrunkFreeSwitchEventSocketStringParsercs">freeswitch/trunk/contrib/verifier/EventSocket/trunk/FreeSwitchEventSocket/StringParser.cs</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#freeswitchtrunkcontribverifierEventSockettrunkFreeSwitchEventSocketParsercs">freeswitch/trunk/contrib/verifier/EventSocket/trunk/FreeSwitchEventSocket/Parser.cs</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="freeswitchtrunkcontribverifierEventSockettrunkFreeSwitchEventSocketTestParserTestcs"></a>
<div class="modfile"><h4>Modified: freeswitch/trunk/contrib/verifier/EventSocket/trunk/FreeSwitch.EventSocket.Test/ParserTest.cs (16305 => 16306)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/contrib/verifier/EventSocket/trunk/FreeSwitch.EventSocket.Test/ParserTest.cs        2010-01-14 07:03:24 UTC (rev 16305)
+++ freeswitch/trunk/contrib/verifier/EventSocket/trunk/FreeSwitch.EventSocket.Test/ParserTest.cs        2010-01-14 14:08:33 UTC (rev 16306)
</span><span class="lines">@@ -4,7 +4,54 @@
</span><span class="cx"> 
</span><span class="cx"> namespace FreeSwitch.EventSocket.Test
</span><span class="cx"> {
</span><del>-    class ParserTest
</del><ins>+    public class ParserTest
</ins><span class="cx">     {
</span><ins>+            private const string Auth = &quot;Content-Type: auth/request\n\n&quot;;
+
+            private const string Messages =
+                    &quot;Content-Type: event\nContent-Length:10\n\n1234567890\n\nContent-Type:event\nContent-Length:0\n\n&quot;;
+                private const string Messages2 =
+                        &quot;Content-Type:event\nContent-Length:0\n\nContent-Type: event\nContent-Length:10\n\n1234567890\n\n&quot;;
+
+                private const string Partial1 =
+                        &quot;Content-Type: event\nContent-Length:10\n\n12345&quot;;
+                private const string Partial2 =
+                        &quot;67890\n\nContent-Type:event\nContent-Length:0\n\n&quot;;
+
+                public void ParseAuth()
+                {
+                        Parser parser = new Parser();
+                        parser.Append(Auth);
+                        PlainEventMsg msg = parser.ParseOne();
+
+                }
+
+                public void ParseMessages()
+                {
+                        Parser parser = new Parser();
+                        parser.Append(Messages);
+                        var msg = parser.ParseOne();
+
+                        msg = parser.ParseOne();
+                }
+
+                public void ParseMessages2()
+                {
+                        Parser parser = new Parser();
+                        parser.Append(Messages2);
+                        var msg = parser.ParseOne();
+
+                        msg = parser.ParseOne();
+                }
+
+                public void ParsePartials()
+                {
+                        Parser parser = new Parser();
+                        parser.Append(Partial1);
+                        var msg = parser.ParseOne();
+                        parser.Append(Partial2);
+                        msg = parser.ParseOne();
+                        msg = parser.ParseOne();
+                }
</ins><span class="cx">     }
</span><span class="cx"> }
</span></span></pre></div>
<a id="freeswitchtrunkcontribverifierEventSockettrunkFreeSwitchEventSocketTestProgramcs"></a>
<div class="modfile"><h4>Modified: freeswitch/trunk/contrib/verifier/EventSocket/trunk/FreeSwitch.EventSocket.Test/Program.cs (16305 => 16306)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/contrib/verifier/EventSocket/trunk/FreeSwitch.EventSocket.Test/Program.cs        2010-01-14 07:03:24 UTC (rev 16305)
+++ freeswitch/trunk/contrib/verifier/EventSocket/trunk/FreeSwitch.EventSocket.Test/Program.cs        2010-01-14 14:08:33 UTC (rev 16306)
</span><span class="lines">@@ -9,6 +9,12 @@
</span><span class="cx">     {
</span><span class="cx">         static void Main(string[] args)
</span><span class="cx">         {
</span><ins>+                        ParserTest pt = new ParserTest();
+                        pt.ParseMessages();
+                        pt.ParseAuth();
+                        pt.ParsePartials();
+                        pt.ParseMessages2();
+
</ins><span class="cx">             /*EventManager mgr = new EventManager();
</span><span class="cx">             mgr.Subscribe(Events.GetChannelEvents());
</span><span class="cx">             mgr.Start(&quot;localhost&quot;);
</span><span class="lines">@@ -20,12 +26,12 @@
</span><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         private int _counter;
</span><del>-        private EventParser _parser;
</del><ins>+        private Parser _parser;
</ins><span class="cx">         Random _rand = new Random((int)DateTime.Now.Ticks);
</span><span class="cx"> 
</span><span class="cx">         public void Test()
</span><span class="cx">         {
</span><del>-            _parser = new EventParser();
</del><ins>+            _parser = new Parser();
</ins><span class="cx">             string text = File.ReadAllText(&quot;C:\\mymsgs.txt&quot;);
</span><span class="cx">             Thread[] threads = new Thread[5];
</span><span class="cx">             for (int i = 0; i &lt; 5; ++i)
</span></span></pre></div>
<a id="freeswitchtrunkcontribverifierEventSockettrunkFreeSwitchEventSocketEventSocketcs"></a>
<div class="modfile"><h4>Modified: freeswitch/trunk/contrib/verifier/EventSocket/trunk/FreeSwitchEventSocket/EventSocket.cs (16305 => 16306)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/contrib/verifier/EventSocket/trunk/FreeSwitchEventSocket/EventSocket.cs        2010-01-14 07:03:24 UTC (rev 16305)
+++ freeswitch/trunk/contrib/verifier/EventSocket/trunk/FreeSwitchEventSocket/EventSocket.cs        2010-01-14 14:08:33 UTC (rev 16306)
</span><span class="lines">@@ -25,7 +25,7 @@
</span><span class="cx">                 private readonly Queue&lt;CmdBase&gt; _commands = new Queue&lt;CmdBase&gt;();
</span><span class="cx">                 private readonly object _lockobj = new object();
</span><span class="cx">                 private readonly ManualResetEvent _parseEvent = new ManualResetEvent(false);
</span><del>-                private readonly EventParser _parser = new EventParser();
</del><ins>+                private readonly Parser _parser = new Parser();
</ins><span class="cx">                 private readonly Thread _parseThread;
</span><span class="cx">                 private readonly byte[] _readBuffer = new byte[8192];
</span><span class="cx">                 private bool _authed;
</span></span></pre></div>
<a id="freeswitchtrunkcontribverifierEventSockettrunkFreeSwitchEventSocketEventsEventBasecs"></a>
<div class="modfile"><h4>Modified: freeswitch/trunk/contrib/verifier/EventSocket/trunk/FreeSwitchEventSocket/Events/EventBase.cs (16305 => 16306)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/contrib/verifier/EventSocket/trunk/FreeSwitchEventSocket/Events/EventBase.cs        2010-01-14 07:03:24 UTC (rev 16305)
+++ freeswitch/trunk/contrib/verifier/EventSocket/trunk/FreeSwitchEventSocket/Events/EventBase.cs        2010-01-14 14:08:33 UTC (rev 16306)
</span><span class="lines">@@ -134,7 +134,14 @@
</span><span class="cx">                     m_coreId = value;
</span><span class="cx">                     break;
</span><span class="cx">                 case &quot;event-date-local&quot;:
</span><del>-                    m_dateLocal = DateTime.Parse(value);
</del><ins>+                                        try
+                                        {
+                                                m_dateLocal = DateTime.Parse(value);
+                                        }
+                                        catch(Exception err)
+                                        {
+                                                Console.WriteLine(err.ToString());
+                                        }
</ins><span class="cx">                     break;
</span><span class="cx">                 case &quot;event-date-gmt&quot;:
</span><span class="cx">                     m_dateGMT = DateTime.Parse(value);
</span></span></pre></div>
<a id="freeswitchtrunkcontribverifierEventSockettrunkFreeSwitchEventSocketFreeSwitchEventSocketcsproj"></a>
<div class="modfile"><h4>Modified: freeswitch/trunk/contrib/verifier/EventSocket/trunk/FreeSwitchEventSocket/FreeSwitch.EventSocket.csproj (16305 => 16306)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/contrib/verifier/EventSocket/trunk/FreeSwitchEventSocket/FreeSwitch.EventSocket.csproj        2010-01-14 07:03:24 UTC (rev 16305)
+++ freeswitch/trunk/contrib/verifier/EventSocket/trunk/FreeSwitchEventSocket/FreeSwitch.EventSocket.csproj        2010-01-14 14:08:33 UTC (rev 16306)
</span><span class="lines">@@ -96,8 +96,8 @@
</span><span class="cx">     &lt;Compile Include=&quot;Ivr\IvrQueue.cs&quot; /&gt;
</span><span class="cx">     &lt;Compile Include=&quot;Ivr\PrivateWaitDtmf.cs&quot; /&gt;
</span><span class="cx">     &lt;Compile Include=&quot;LogWriterHandler.cs&quot; /&gt;
</span><ins>+    &lt;Compile Include=&quot;Parser.cs&quot; /&gt;
</ins><span class="cx">     &lt;Compile Include=&quot;PlainEventMsg.cs&quot; /&gt;
</span><del>-    &lt;Compile Include=&quot;EventParser.cs&quot; /&gt;
</del><span class="cx">     &lt;Compile Include=&quot;Events.cs&quot; /&gt;
</span><span class="cx">     &lt;Compile Include=&quot;Events\EventApiCommand.cs&quot; /&gt;
</span><span class="cx">     &lt;Compile Include=&quot;Events\EventBase.cs&quot; /&gt;
</span></span></pre></div>
<a id="freeswitchtrunkcontribverifierEventSockettrunkFreeSwitchEventSocketParsercs"></a>
<div class="addfile"><h4>Added: freeswitch/trunk/contrib/verifier/EventSocket/trunk/FreeSwitchEventSocket/Parser.cs (0 => 16306)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/contrib/verifier/EventSocket/trunk/FreeSwitchEventSocket/Parser.cs                                (rev 0)
+++ freeswitch/trunk/contrib/verifier/EventSocket/trunk/FreeSwitchEventSocket/Parser.cs        2010-01-14 14:08:33 UTC (rev 16306)
</span><span class="lines">@@ -0,0 +1,161 @@
</span><ins>+using System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace FreeSwitch.EventSocket
+{
+        public class Parser
+        {
+                private readonly StringBuilder _text = new StringBuilder();
+                private readonly Queue&lt;string&gt; _piecesToAppend = new Queue&lt;string&gt;();
+
+                public string Text
+                {
+                        get { return _text.ToString(); }
+                }
+
+                public void Append(string text)
+                {
+                        lock (_piecesToAppend)
+                                _piecesToAppend.Enqueue(text);
+                }
+
+                internal static void ParseHeaders(PlainEventMsg msg, string header)
+                {
+                        StringParser parser = new StringParser(header);
+                        string name = parser.Read(':', true);
+                        while (name != string.Empty)
+                        {
+                                switch (name)
+                                {
+                                        case &quot;Content-Length&quot;:
+                                                msg.ContentLength = int.Parse(parser.ReadLine(true));
+                                                break;
+                                        case &quot;Content-Type&quot;:
+                                                msg.ContentType = parser.ReadLine(true);
+                                                break;
+                                        case &quot;Reply-Text&quot;:
+                                                msg.Body = parser.ReadLine(true);
+                                                break;
+                                }
+
+                                // empty line == end of header
+                                if (parser.EOL)
+                                        break;
+
+                                name = parser.Read(':', true);
+                        }
+                }
+
+                public PlainEventMsg ParseOne()
+                {
+                        AppendPieces();
+
+                        int index = 0;
+                        PlainEventMsg msg = FindHeaders(ref index);
+                        if (msg == null)
+                                return null;
+
+                        // no body
+                        if (msg.ContentLength &lt;= 0)
+                        {
+                                _text.Remove(0, index);
+                                return msg;
+                        }
+
+                        // too few bytes.
+                        if (_text.Length-index &lt; msg.ContentLength)
+                                return null;
+
+                        // ignore empty line between header and body.
+                        IgnoreEmptyLines(ref index);
+
+                        int start = index;
+                        FindEmptyLine(ref index);
+
+                        int size = index - start + 2; // include \n\n
+
+                        // Validate size.
+                        //
+                        // Someone suggested that the sizes are correct and that 
+                        // the events was wrapped. Well. I've checked if the
+                        // specified position is a \n\n, and it's not.
+                        //
+                        // Don't force me to check source code of another implementation
+                        //
+                        if (size != msg.ContentLength)
+                        {
+                                // Check if the buffer contains header + body only,
+                                // then use the reported size, despite that our check failed.
+                                if (_text.Length == msg.ContentLength + start)
+                                        size = msg.ContentLength;
+                                // buffer contains more than one message.
+                                else if (_text.Length &gt; msg.ContentLength+start)
+                                {
+                                        // Check if the reported size ends with a \n,
+                                        // in that case we have a container event.
+                                        if (_text[msg.ContentLength + start - 1] == '\n')
+                                                size = msg.ContentLength;
+                                }
+                        }
+
+                        char[] body = new char[size];
+                        _text.CopyTo(start, body, 0, size);
+                        msg.Body = new string(body);
+                        _text.Remove(0, index);
+                        return msg;
+                }
+
+                private PlainEventMsg FindHeaders(ref int index)
+                {
+                        IgnoreEmptyLines(ref index);
+
+                        int start = index;
+                        FindEmptyLine(ref index);
+                        if (start == index)
+                                return null; //got no header.
+
+                        char[] headerBuffer = new char[index - start];
+                        _text.CopyTo(start, headerBuffer, 0, index - start);
+                        
+                        PlainEventMsg eventMsg = new PlainEventMsg();
+                        ParseHeaders(eventMsg, new string(headerBuffer));
+                        return eventMsg;
+                }
+
+
+                private void IgnoreEmptyLines(ref int index)
+                {
+                        while (index &lt; _text.Length &amp;&amp; _text[index] == '\n')
+                                ++index;
+                }
+
+                private void FindEmptyLine(ref int index)
+                {
+                        int pos = index;
+                        while (index &lt; _text.Length-1)
+                        {
+                                if (_text[index] == '\n' &amp;&amp; _text[index + 1] == '\n')
+                                        return;
+                                ++index;
+                        }
+
+                        index = pos;
+                }
+
+                private void AppendPieces()
+                {
+                        lock (_piecesToAppend)
+                        {
+                                while (_piecesToAppend.Count &gt; 0)
+                                        _text.Append(_piecesToAppend.Dequeue());
+                        }
+                }
+
+                public void Clear()
+                {
+                        _piecesToAppend.Clear();
+                        _text.Length = 0;
+                }
+        }
+}
</ins></span></pre></div>
<a id="freeswitchtrunkcontribverifierEventSockettrunkFreeSwitchEventSocketStringParsercs"></a>
<div class="modfile"><h4>Modified: freeswitch/trunk/contrib/verifier/EventSocket/trunk/FreeSwitchEventSocket/StringParser.cs (16305 => 16306)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/contrib/verifier/EventSocket/trunk/FreeSwitchEventSocket/StringParser.cs        2010-01-14 07:03:24 UTC (rev 16305)
+++ freeswitch/trunk/contrib/verifier/EventSocket/trunk/FreeSwitchEventSocket/StringParser.cs        2010-01-14 14:08:33 UTC (rev 16306)
</span><span class="lines">@@ -188,19 +188,19 @@
</span><span class="cx">             {
</span><span class="cx">                 ++_pos; // skip delimiter
</span><span class="cx">                 int endpos = TrimEnd(); // skip 
</span><del>-                return _text.Substring(startpos, endpos - startpos);
</del><ins>+                return _text.Substring(startpos, endpos - startpos-1);
</ins><span class="cx">             }
</span><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         private int TrimEnd()
</span><span class="cx">         {
</span><del>-            if (EOF)
-                return _pos;
</del><ins>+                        if (EOF || !IsWS(_text[_pos]))
+                                return _pos;
</ins><span class="cx"> 
</span><span class="cx">             int pos = _pos;
</span><span class="cx">             while (IsWS(_text[pos]))
</span><span class="cx">                 --pos;
</span><del>-            return pos;
</del><ins>+            return pos+1;
</ins><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         private int FindWS(bool stopAtEOL)
</span></span></pre>
</div>
</div>
<div id="footer">See you at ClueCon</div>

</body>
</html>