<!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][14774] </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=14774">14774</a></dd>
<dt>Author</dt> <dd>michaelgg</dd>
<dt>Date</dt> <dd>2009-09-05 12:34:41 -0500 (Sat, 05 Sep 2009)</dd>
</dl>

<h3>Log Message</h3>
<pre>Start adding some strongly-typed accessors to common channel vars</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#freeswitchtrunksrcmodlanguagesmod_managedmanagedFreeSWITCHManagedcsproj">freeswitch/trunk/src/mod/languages/mod_managed/managed/FreeSWITCH.Managed.csproj</a></li>
<li><a href="#freeswitchtrunksrcmodlanguagesmod_managedmanagedManagedSessioncs">freeswitch/trunk/src/mod/languages/mod_managed/managed/ManagedSession.cs</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#freeswitchtrunksrcmodlanguagesmod_managedmanagedChannelVariablescs">freeswitch/trunk/src/mod/languages/mod_managed/managed/ChannelVariables.cs</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="freeswitchtrunksrcmodlanguagesmod_managedmanagedChannelVariablescs"></a>
<div class="addfile"><h4>Added: freeswitch/trunk/src/mod/languages/mod_managed/managed/ChannelVariables.cs (0 => 14774)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/src/mod/languages/mod_managed/managed/ChannelVariables.cs                                (rev 0)
+++ freeswitch/trunk/src/mod/languages/mod_managed/managed/ChannelVariables.cs        2009-09-05 17:34:41 UTC (rev 14774)
</span><span class="lines">@@ -0,0 +1,231 @@
</span><ins>+/* 
+ * FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application - mod_managed
+ * Copyright (C) 2008, Michael Giagnocavo &lt;mgg@giagnocavo.net&gt;
+ *
+ * Version: MPL 1.1
+ *
+ * The contents of this file are subject to the Mozilla Public License Version
+ * 1.1 (the &quot;License&quot;); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ * http://www.mozilla.org/MPL/
+ *
+ * Software distributed under the License is distributed on an &quot;AS IS&quot; basis,
+ * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+ * for the specific language governing rights and limitations under the
+ * License.
+ *
+ * The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application - mod_managed
+ *
+ * The Initial Developer of the Original Code is
+ * Michael Giagnocavo &lt;mgg@giagnocavo.net&gt;
+ * Portions created by the Initial Developer are Copyright (C)
+ * the Initial Developer. All Rights Reserved.
+ *
+ * Contributor(s):
+ * 
+ * Michael Giagnocavo &lt;mgg@giagnocavo.net&gt;
+ * 
+ * ChannelVariables.cs -- Strongly typed channel variables for ManagedSession
+ *
+ */
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Runtime.InteropServices;
+
+namespace FreeSWITCH.Native {
+    public partial class ManagedSession {
+
+        // Need to find a better place to put these - then make them public
+        static readonly DateTime epoch = new DateTime(1970, 1, 1);
+        static DateTime epochUsToDateTime(long us){
+            return us == 0 ? 
+                DateTime.MinValue :
+                epoch.AddMilliseconds((double)((decimal)us / 1000m));
+        }
+        static bool strToBool(string s) {
+            if (string.IsNullOrEmpty(s)) return false;
+            switch (s.ToLowerInvariant()) {
+                case &quot;true&quot;:
+                case &quot;yes&quot;:
+                case &quot;on&quot;:
+                case &quot;enable&quot;:
+                case &quot;enabled&quot;:
+                case &quot;active&quot;:
+                case &quot;allow&quot;:
+                    return true;
+                default:
+                    // Numbers are true
+                    long tmp;
+                    return long.TryParse(s, out tmp);
+            }
+        }
+        static string boolToStr(bool b) {
+            return b ? &quot;true&quot; : &quot;false&quot;;
+        }
+
+        ChannelVariables _variables; // Set on ManagedSession init
+        public ChannelVariables Variables {
+            get {
+                if (_variables == null) {
+                    _variables = new ChannelVariables(this);
+                }
+                return _variables;
+            }
+        }
+
+        /// &lt;summary&gt;Strongly typed access to common variables&lt;/summary&gt;
+        public class ChannelVariables {
+            readonly ManagedSession sess;
+            internal ChannelVariables(ManagedSession session) {
+                this.sess = session;
+            }
+
+            public IDictionary&lt;string, string&gt; GetAllVariables() {
+                var dic = new Dictionary&lt;string, string&gt;(StringComparer.OrdinalIgnoreCase);
+                var evt = Native.freeswitch.switch_channel_variable_first(sess.channel);
+                while(evt != null) {
+                    dic.Add(evt.name, evt.value);
+                    evt = evt.next;
+                }
+                Native.freeswitch.switch_channel_variable_last(sess.channel);
+                return dic;
+            }
+
+            /*** Settings ***/
+            const string bypass_media = &quot;bypass_media&quot;;
+            public bool BypassMedia {
+                get { return strToBool(sess.GetVariable(bypass_media)); }
+                set { sess.SetVariable(bypass_media, boolToStr(value)); }
+            }
+
+            /*** Times ***/
+            const string created_time = &quot;created_time&quot;;
+            const string answered_time = &quot;answered_time&quot;;
+            const string hangup_time = &quot;hangup_time&quot;;
+            const string progress_time = &quot;progress_time&quot;;
+            const string transfer_time = &quot;transfer_time&quot;;
+
+            public DateTime CreatedTime {
+                get { return epochUsToDateTime(long.Parse(sess.GetVariable(created_time))); }
+            }
+
+            DateTime? readUsecsDateTime(string varName) {
+                var v = sess.GetVariable(varName);
+                if (string.IsNullOrEmpty(v)) return null;
+                else {
+                    var d = epochUsToDateTime(long.Parse(v));
+                    if (d == DateTime.MinValue) return null;
+                    return d;
+                }
+            }
+
+            public DateTime? AnsweredTime {
+                get { return readUsecsDateTime(answered_time); }
+            }
+            public DateTime? HangupTime {
+                get { return readUsecsDateTime(hangup_time); }
+            }
+            public DateTime? ProgressTime {
+                get { return readUsecsDateTime(progress_time); }
+            }
+            public DateTime? TransferTime {
+                get { return readUsecsDateTime(transfer_time); }
+            }
+
+        
+            /*** SIP Variables ***/
+            const string sofia_profile_name = &quot;sofia_profile_name&quot;;
+            const string sip_received_ip = &quot;sip_received_ip&quot;;
+            const string sip_received_port = &quot;sip_received_port&quot;;
+            const string sip_via_protocol = &quot;sip_via_protocol&quot;;
+            const string sip_from_user = &quot;sip_from_user&quot;;
+            const string sip_from_uri = &quot;sip_from_uri&quot;;
+            const string sip_from_host = &quot;sip_from_host&quot;;
+            const string sip_req_user = &quot;sip_req_user&quot;;
+            const string sip_req_uri = &quot;sip_req_uri&quot;;
+            const string sip_req_host = &quot;sip_req_host&quot;;
+            const string sip_to_user = &quot;sip_to_user&quot;;
+            const string sip_to_uri = &quot;sip_to_uri&quot;;
+            const string sip_to_host = &quot;sip_to_host&quot;;
+            const string sip_contact_user = &quot;sip_contact_user&quot;;
+            const string sip_contact_port = &quot;sip_contact_port&quot;;
+            const string sip_contact_uri = &quot;sip_contact_uri&quot;;
+            const string sip_contact_host = &quot;sip_contact_host&quot;;
+            const string sip_call_id = &quot;sip_call_id&quot;;
+            const string sip_destination_url = &quot;sip_destination_url&quot;;
+            const string sip_term_status = &quot;sip_term_status&quot;;
+            const string sip_term_cause = &quot;sip_term_status&quot;;
+            const string switch_r_sdp = &quot;switch_r_sdp&quot;;
+            const string switch_m_sdp = &quot;switch_m_sdp&quot;;
+            const string sip_hangup_phrase = &quot;sip_hangup_phrase&quot;;
+
+
+            short? readShort(string varName) {
+                var s = sess.GetVariable(varName);
+                if (string.IsNullOrEmpty(s)) return null;
+                short res;
+                if (short.TryParse(s, out res)) return res;
+                else return null;
+            }
+            int? readInt(string varName) {
+                var s = sess.GetVariable(varName);
+                if (string.IsNullOrEmpty(s)) return null;
+                int res;
+                if (int.TryParse(s, out res)) return res;
+                else return null;
+            }
+
+            // String suffix is added when the var is better represented as 
+            // a different data type, but for now we return string
+
+            public string SofiaProfileName { get { return sess.GetVariable(sofia_profile_name); } }
+            public string SipCallID { get { return sess.GetVariable(sip_call_id); } }
+            public string SipReceivedIP { get { return sess.GetVariable(sip_received_ip); } }
+            public short? SipReceivedPort { get { return readShort(sip_received_port); } }
+            public string SipViaProtocolString { get { return sess.GetVariable(sip_via_protocol); } }
+            public string SipFromUser { get { return sess.GetVariable(sip_from_user); } }
+            public string SipFromUriString { get { return sess.GetVariable(sip_from_uri); } }
+            public string SipFromHost { get { return sess.GetVariable(sip_from_host); } }
+            public string SipReqUser { get { return sess.GetVariable(sip_req_user); } }
+            public string SipReqUriString { get { return sess.GetVariable(sip_req_uri); } }
+            public string SipReqHost { get { return sess.GetVariable(sip_req_host); } }
+            public string SipToUser { get { return sess.GetVariable(sip_to_user); } }
+            public string SipToUriString { get { return sess.GetVariable(sip_to_uri); } }
+            public string SipToHost { get { return sess.GetVariable(sip_to_host); } }
+            public string SipContactUser { get { return sess.GetVariable(sip_contact_user); } }
+            public string SipContactUriString { get { return sess.GetVariable(sip_contact_uri); } }
+            public string SipContactHost { get { return sess.GetVariable(sip_contact_host); } }
+            public short? SipContactPort { get { return readShort(sip_contact_port); } }
+            public string SipDestinationUrlString { get { return sess.GetVariable(sip_destination_url); } }
+            public int? SipTermStatus { get { return readInt(sip_term_status); } }
+            public int? SipTermCause { get { return readInt(sip_term_cause); } }
+            public string SwitchRSdp { get { return sess.GetVariable(switch_r_sdp); } }
+            public string SwitchMSdp { get { return sess.GetVariable(switch_m_sdp); } }
+            public string SipHangupPhrase { get { return sess.GetVariable(sip_hangup_phrase); } }
+
+            /*** Other ***/
+
+            const string proto_specific_hangup_cause = &quot;proto_specific_hangup_cause&quot;;
+            const string hangup_cause = &quot;hangup_cause&quot;;
+            const string hangup_cause_q850 = &quot;hangup_cause_q850&quot;;
+            const string originate_disposition = &quot;originate_disposition&quot;;
+            const string direction = &quot;direction&quot;;
+
+            public string ProtoSpecificHangupCause { get { return sess.GetVariable(proto_specific_hangup_cause); } }
+            public string HangupCauseString { get { return sess.GetVariable(hangup_cause); } }
+            public int? HangupCauseQ850 { get { return readInt(hangup_cause_q850); } }
+            public string OriginateDispositionString { get { return sess.GetVariable(originate_disposition); } }
+
+            public Native.switch_call_direction_t CallDirection {
+                get {
+                    var s = sess.GetVariable(direction);
+                    if (string.IsNullOrEmpty(s)) return switch_call_direction_t.SWITCH_CALL_DIRECTION_INBOUND; // I guess
+                    return s.ToLowerInvariant() == &quot;inbound&quot; ? switch_call_direction_t.SWITCH_CALL_DIRECTION_INBOUND : switch_call_direction_t.SWITCH_CALL_DIRECTION_OUTBOUND;
+                }
+
+            }
+        }
+    }
+}
</ins><span class="cx">\ No newline at end of file
</span></span></pre></div>
<a id="freeswitchtrunksrcmodlanguagesmod_managedmanagedFreeSWITCHManagedcsproj"></a>
<div class="modfile"><h4>Modified: freeswitch/trunk/src/mod/languages/mod_managed/managed/FreeSWITCH.Managed.csproj (14773 => 14774)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/src/mod/languages/mod_managed/managed/FreeSWITCH.Managed.csproj        2009-09-05 16:55:45 UTC (rev 14773)
+++ freeswitch/trunk/src/mod/languages/mod_managed/managed/FreeSWITCH.Managed.csproj        2009-09-05 17:34:41 UTC (rev 14774)
</span><span class="lines">@@ -48,6 +48,7 @@
</span><span class="cx">   &lt;/ItemGroup&gt;
</span><span class="cx">   &lt;ItemGroup&gt;
</span><span class="cx">     &lt;Compile Include=&quot;AssemblyInfo.cs&quot; /&gt;
</span><ins>+    &lt;Compile Include=&quot;ChannelVariables.cs&quot; /&gt;
</ins><span class="cx">     &lt;Compile Include=&quot;ManagedSession.cs&quot; /&gt;
</span><span class="cx">     &lt;Compile Include=&quot;Loader.cs&quot; /&gt;
</span><span class="cx">     &lt;Compile Include=&quot;Extensions.cs&quot; /&gt;
</span></span></pre></div>
<a id="freeswitchtrunksrcmodlanguagesmod_managedmanagedManagedSessioncs"></a>
<div class="modfile"><h4>Modified: freeswitch/trunk/src/mod/languages/mod_managed/managed/ManagedSession.cs (14773 => 14774)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/src/mod/languages/mod_managed/managed/ManagedSession.cs        2009-09-05 16:55:45 UTC (rev 14773)
+++ freeswitch/trunk/src/mod/languages/mod_managed/managed/ManagedSession.cs        2009-09-05 17:34:41 UTC (rev 14774)
</span><span class="lines">@@ -52,6 +52,9 @@
</span><span class="cx">         /// &lt;summary&gt;Initializes the native ManagedSession. Must be called after Originate.&lt;/summary&gt;
</span><span class="cx">         public void Initialize()
</span><span class="cx">         {
</span><ins>+            if (allocated == 0) {
+                Log.WriteLine(LogLevel.Critical, &quot;Cannot initialize a ManagedSession until it is allocated (originated successfully).&quot;);
+            }
</ins><span class="cx">             // P/Invoke generated function pointers stick around until the delegate is collected
</span><span class="cx">             // By sticking the delegates in fields, their lifetime won't be less than the session
</span><span class="cx">             // So we don't need to worry about GCHandles and all that....
</span><span class="lines">@@ -59,6 +62,7 @@
</span><span class="cx">             this._inputCallbackRef = inputCallback;
</span><span class="cx">             this._hangupCallbackRef = hangupCallback;
</span><span class="cx">             InitManagedSession(ManagedSession.getCPtr(this).Handle, this._inputCallbackRef, this._hangupCallbackRef);
</span><ins>+            this._variables = new ChannelVariables(this);
</ins><span class="cx">         }
</span><span class="cx">         DtmfCallback _inputCallbackRef;
</span><span class="cx">         CdeclAction _hangupCallbackRef;
</span><span class="lines">@@ -122,15 +126,9 @@
</span><span class="cx">             get { return this.Ready(); }
</span><span class="cx">         }
</span><span class="cx"> 
</span><del>-        Guid _uuid;
-        bool _uuidSet;
</del><span class="cx">         public Guid Uuid {
</span><span class="cx">             get {
</span><del>-                if (!_uuidSet) {
-                    _uuid = new Guid(this.GetUuid());
-                    _uuidSet = true;
-                }
-                return _uuid;
</del><ins>+                return new Guid(this.GetUuid());
</ins><span class="cx">             }
</span><span class="cx">         }
</span><span class="cx">     }
</span></span></pre>
</div>
</div>
<div id="footer">See you at ClueCon</div>

</body>
</html>