From f22a237b44b58f469bac4ef41ddb5ea7015a0797 Mon Sep 17 00:00:00 2001
From: Michael Giagnocavo <mgg@giagnocavo.net>
Date: Sat, 5 Sep 2009 17:34:41 +0000
Subject: [PATCH] Start adding some strongly-typed accessors to common channel
 vars

git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@14774 d0543943-73ff-0310-b7d9-9358b9ac24b2
---
 .../mod_managed/managed/ChannelVariables.cs   | 231 ++++++++++++++++++
 .../managed/FreeSWITCH.Managed.csproj         |   1 +
 .../mod_managed/managed/ManagedSession.cs     |  12 +-
 3 files changed, 237 insertions(+), 7 deletions(-)
 create mode 100644 src/mod/languages/mod_managed/managed/ChannelVariables.cs

diff --git a/src/mod/languages/mod_managed/managed/ChannelVariables.cs b/src/mod/languages/mod_managed/managed/ChannelVariables.cs
new file mode 100644
index 0000000000..26c109fa89
--- /dev/null
+++ b/src/mod/languages/mod_managed/managed/ChannelVariables.cs
@@ -0,0 +1,231 @@
+/* 
+ * FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application - mod_managed
+ * Copyright (C) 2008, Michael Giagnocavo <mgg@giagnocavo.net>
+ *
+ * Version: MPL 1.1
+ *
+ * The contents of this file are subject to the Mozilla Public License Version
+ * 1.1 (the "License"); 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 "AS IS" 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 <mgg@giagnocavo.net>
+ * Portions created by the Initial Developer are Copyright (C)
+ * the Initial Developer. All Rights Reserved.
+ *
+ * Contributor(s):
+ * 
+ * Michael Giagnocavo <mgg@giagnocavo.net>
+ * 
+ * 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 "true":
+                case "yes":
+                case "on":
+                case "enable":
+                case "enabled":
+                case "active":
+                case "allow":
+                    return true;
+                default:
+                    // Numbers are true
+                    long tmp;
+                    return long.TryParse(s, out tmp);
+            }
+        }
+        static string boolToStr(bool b) {
+            return b ? "true" : "false";
+        }
+
+        ChannelVariables _variables; // Set on ManagedSession init
+        public ChannelVariables Variables {
+            get {
+                if (_variables == null) {
+                    _variables = new ChannelVariables(this);
+                }
+                return _variables;
+            }
+        }
+
+        /// <summary>Strongly typed access to common variables</summary>
+        public class ChannelVariables {
+            readonly ManagedSession sess;
+            internal ChannelVariables(ManagedSession session) {
+                this.sess = session;
+            }
+
+            public IDictionary<string, string> GetAllVariables() {
+                var dic = new Dictionary<string, string>(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 = "bypass_media";
+            public bool BypassMedia {
+                get { return strToBool(sess.GetVariable(bypass_media)); }
+                set { sess.SetVariable(bypass_media, boolToStr(value)); }
+            }
+
+            /*** Times ***/
+            const string created_time = "created_time";
+            const string answered_time = "answered_time";
+            const string hangup_time = "hangup_time";
+            const string progress_time = "progress_time";
+            const string transfer_time = "transfer_time";
+
+            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 = "sofia_profile_name";
+            const string sip_received_ip = "sip_received_ip";
+            const string sip_received_port = "sip_received_port";
+            const string sip_via_protocol = "sip_via_protocol";
+            const string sip_from_user = "sip_from_user";
+            const string sip_from_uri = "sip_from_uri";
+            const string sip_from_host = "sip_from_host";
+            const string sip_req_user = "sip_req_user";
+            const string sip_req_uri = "sip_req_uri";
+            const string sip_req_host = "sip_req_host";
+            const string sip_to_user = "sip_to_user";
+            const string sip_to_uri = "sip_to_uri";
+            const string sip_to_host = "sip_to_host";
+            const string sip_contact_user = "sip_contact_user";
+            const string sip_contact_port = "sip_contact_port";
+            const string sip_contact_uri = "sip_contact_uri";
+            const string sip_contact_host = "sip_contact_host";
+            const string sip_call_id = "sip_call_id";
+            const string sip_destination_url = "sip_destination_url";
+            const string sip_term_status = "sip_term_status";
+            const string sip_term_cause = "sip_term_status";
+            const string switch_r_sdp = "switch_r_sdp";
+            const string switch_m_sdp = "switch_m_sdp";
+            const string sip_hangup_phrase = "sip_hangup_phrase";
+
+
+            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 = "proto_specific_hangup_cause";
+            const string hangup_cause = "hangup_cause";
+            const string hangup_cause_q850 = "hangup_cause_q850";
+            const string originate_disposition = "originate_disposition";
+            const string direction = "direction";
+
+            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() == "inbound" ? switch_call_direction_t.SWITCH_CALL_DIRECTION_INBOUND : switch_call_direction_t.SWITCH_CALL_DIRECTION_OUTBOUND;
+                }
+
+            }
+        }
+    }
+}
\ No newline at end of file
diff --git a/src/mod/languages/mod_managed/managed/FreeSWITCH.Managed.csproj b/src/mod/languages/mod_managed/managed/FreeSWITCH.Managed.csproj
index d98e183a8d..33213a6caa 100644
--- a/src/mod/languages/mod_managed/managed/FreeSWITCH.Managed.csproj
+++ b/src/mod/languages/mod_managed/managed/FreeSWITCH.Managed.csproj
@@ -48,6 +48,7 @@
   </ItemGroup>
   <ItemGroup>
     <Compile Include="AssemblyInfo.cs" />
+    <Compile Include="ChannelVariables.cs" />
     <Compile Include="ManagedSession.cs" />
     <Compile Include="Loader.cs" />
     <Compile Include="Extensions.cs" />
diff --git a/src/mod/languages/mod_managed/managed/ManagedSession.cs b/src/mod/languages/mod_managed/managed/ManagedSession.cs
index c233da54f5..3ffdb189a9 100644
--- a/src/mod/languages/mod_managed/managed/ManagedSession.cs
+++ b/src/mod/languages/mod_managed/managed/ManagedSession.cs
@@ -52,6 +52,9 @@ namespace FreeSWITCH.Native
         /// <summary>Initializes the native ManagedSession. Must be called after Originate.</summary>
         public void Initialize()
         {
+            if (allocated == 0) {
+                Log.WriteLine(LogLevel.Critical, "Cannot initialize a ManagedSession until it is allocated (originated successfully).");
+            }
             // P/Invoke generated function pointers stick around until the delegate is collected
             // By sticking the delegates in fields, their lifetime won't be less than the session
             // So we don't need to worry about GCHandles and all that....
@@ -59,6 +62,7 @@ namespace FreeSWITCH.Native
             this._inputCallbackRef = inputCallback;
             this._hangupCallbackRef = hangupCallback;
             InitManagedSession(ManagedSession.getCPtr(this).Handle, this._inputCallbackRef, this._hangupCallbackRef);
+            this._variables = new ChannelVariables(this);
         }
         DtmfCallback _inputCallbackRef;
         CdeclAction _hangupCallbackRef;
@@ -122,15 +126,9 @@ namespace FreeSWITCH.Native
             get { return this.Ready(); }
         }
 
-        Guid _uuid;
-        bool _uuidSet;
         public Guid Uuid {
             get {
-                if (!_uuidSet) {
-                    _uuid = new Guid(this.GetUuid());
-                    _uuidSet = true;
-                }
-                return _uuid;
+                return new Guid(this.GetUuid());
             }
         }
     }