Revision
14405
Author
michaelgg
Date
2009-07-28 21:08:47 -0500 (Tue, 28 Jul 2009)

Log Message

More robust loading; make demo more clear

Modified Paths

Added Paths

Removed Paths

Diff

Deleted: freeswitch/trunk/src/mod/languages/mod_managed/managed/Demo.cs

Added: freeswitch/trunk/src/mod/languages/mod_managed/managed/Demo.csx (0 => 14405)


--- freeswitch/trunk/src/mod/languages/mod_managed/managed/Demo.csx	                        (rev 0)
+++ freeswitch/trunk/src/mod/languages/mod_managed/managed/Demo.csx	2009-07-29 02:08:47 UTC (rev 14405)
@@ -0,0 +1,110 @@
+/* 
+ * 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>
+ * 
+ * Demo.cs -- mod_mono demo classes
+ *
+ */
+
+// How to test the demo (in the mod/managed directory):
+// -- Compile to dll for "normal" loading
+// -- Compile to exe for script EXE loading
+// -- Copy to managed directory for dynamic compilation
+
+using System;
+using FreeSWITCH;
+using FreeSWITCH.Native;
+
+public class AppDemo : IAppPlugin {
+
+    ManagedSession Session;
+    public void Run(AppContext context) {
+        Session = context.Session;
+        Session.HangupFunction = hangupHook;
+        Session.Answer();
+        Session.DtmfReceivedFunction = (d, t) => {
+            Log.WriteLine(LogLevel.Notice, "Received {0} for {1}.", d, t);
+            return "";
+        };
+        Log.WriteLine(LogLevel.Notice, "Inside AppDemo.Run (args '{0}'); HookState is {1}. Now will collect digits.", context.Arguments, Session.HookState);
+        Session.CollectDigits(5000); // Hanging up here will cause an abort and the next line won't be written
+        Log.WriteLine(LogLevel.Notice, "AppDemo is finishing its run and will now hang up.");
+        Session.Hangup("USER_BUSY");
+    }
+
+    void hangupHook() {
+        Log.WriteLine(LogLevel.Notice, "AppDemo hanging up, UUID: {0}.", this.Session.Uuid);
+    }
+
+}
+
+public class ApiDemo : IApiPlugin {
+
+    public void Execute(ApiContext context) {
+        context.Stream.Write(string.Format("ApiDemo executed with args '{0}' and event type {1}.",
+            context.Arguments, context.Event == null ? "<none>" : context.Event.GetEventType()));
+    }
+
+    public void ExecuteBackground(ApiBackgroundContext context) {
+        Log.WriteLine(LogLevel.Notice, "ApiDemo on a background thread #({0}), with args '{1}'.",
+            System.Threading.Thread.CurrentThread.ManagedThreadId,
+            context.Arguments);
+    }
+
+}
+
+public class LoadDemo : ILoadNotificationPlugin {
+    public bool Load() {
+        Log.WriteLine(LogLevel.Notice, "LoadDemo running.");
+        return true;
+    }
+}
+
+public class ScriptDemo {
+
+    public static void Main() {
+        switch (FreeSWITCH.Script.ContextType) {
+            case ScriptContextType.Api: {
+                    var ctx = FreeSWITCH.Script.GetApiContext();
+                    ctx.Stream.Write("Script executing as API with args: " + ctx.Arguments);
+                    break;
+                }
+            case ScriptContextType.ApiBackground: {
+                    var ctx = FreeSWITCH.Script.GetApiBackgroundContext();
+                    Log.WriteLine(LogLevel.Notice, "Executing as APIBackground with args: " + ctx.Arguments);
+                    break;
+                }
+            case ScriptContextType.App: {
+                    var ctx = FreeSWITCH.Script.GetAppContext();
+                    Log.WriteLine(LogLevel.Notice, "Executing as App with args: " + ctx.Arguments);
+                    break;
+                }
+        }
+
+    }
+
+}
+

Modified: freeswitch/trunk/src/mod/languages/mod_managed/managed/FreeSWITCH.Managed.csproj (14404 => 14405)


--- freeswitch/trunk/src/mod/languages/mod_managed/managed/FreeSWITCH.Managed.csproj	2009-07-28 23:54:18 UTC (rev 14404)
+++ freeswitch/trunk/src/mod/languages/mod_managed/managed/FreeSWITCH.Managed.csproj	2009-07-29 02:08:47 UTC (rev 14405)
@@ -52,7 +52,7 @@
     <Compile Include="Loader.cs" />
     <Compile Include="Extensions.cs" />
     <Compile Include="Log.cs" />
-    <None Include="Demo.cs" />
+    <None Include="Demo.csx" />
     <Compile Include="PluginInterfaces.cs" />
     <Compile Include="PluginManager.cs" />
     <Compile Include="ScriptPluginManager.cs" />

Modified: freeswitch/trunk/src/mod/languages/mod_managed/managed/Loader.cs (14404 => 14405)


--- freeswitch/trunk/src/mod/languages/mod_managed/managed/Loader.cs	2009-07-28 23:54:18 UTC (rev 14404)
+++ freeswitch/trunk/src/mod/languages/mod_managed/managed/Loader.cs	2009-07-29 02:08:47 UTC (rev 14405)
@@ -219,15 +219,24 @@
             setup.ApplicationName = Path.GetFileName(fileName) + "_" + appDomainCount;
             var domain = AppDomain.CreateDomain(setup.ApplicationName, null, setup);
                 
-            var pm = (PluginManager)domain.CreateInstanceAndUnwrap(pmType.Assembly.FullName, pmType.FullName, null);
-            if (!pm.Load(fileName)) {
+            PluginManager pm;
+            try {
+                pm = (PluginManager)domain.CreateInstanceAndUnwrap(pmType.Assembly.FullName, pmType.FullName, null);
+                if (!pm.Load(fileName)) {
+                    AppDomain.Unload(domain);
+                    unloadFile(fileName);
+                    return;
+                }
+            } catch (Exception ex) {
+                // On an exception, we will unload the current file so an old copy doesnt stay active
+                Log.WriteLine(LogLevel.Alert, "Exception loading {0}: {1}", fileName, ex.ToString());
                 AppDomain.Unload(domain);
                 unloadFile(fileName);
                 return;
             }
 
+            // Update dictionaries atomically
             lock (loaderLock) {
-                // Update dictionaries atomically
                 unloadFile(fileName);
 
                 var pi = new PluginInfo { FileName = fileName, Domain = domain, Manager = pm };
@@ -279,7 +288,7 @@
                         AppDomain.Unload(d);
                         Log.WriteLine(LogLevel.Info, "Unloaded {0}, domain {1}.", pi.FileName, friendlyName);
                     } catch (Exception ex) {
-                        Log.WriteLine(LogLevel.Critical, "Could not unload {0}, domain {1}: {2}", pi.FileName, friendlyName, ex.ToString());
+                        Log.WriteLine(LogLevel.Alert, "Could not unload {0}, domain {1}: {2}", pi.FileName, friendlyName, ex.ToString());
                     }
                 });
                 t.Priority = System.Threading.ThreadPriority.BelowNormal;