Mod managed

From FreeSWITCH Wiki

Jump to: navigation, search

mod_managed allows execution of Common Language Infrastructure code inside FreeSWITCH. It supports the Microsoft CLR (".NET") on Windows, as well as Mono on Windows and other platforms. Effectively, this allows use of any .NET language, such as F#, VB.NET, C#, IronRuby, IronPython, JScript.NET, and many others, to create FreeSWITCH modules, applications, and API functions. (mod_managed replaces mod_mono.)

Contents

Installation

mod_managed only supports the .NET Framework 3.5 or Mono 2.0.

To get mod_managed to build on Windows, you’ll need Visual Studio 2008 (C++ and C# to build the entire thing, although C# binaries are provided). (Insert Linux info here.)

Microsoft CLR Build

mod_managed should "just work" with the .NET Framework 3.5 and Visual Studio 2008 installed. In the build configuration manager inside Visual Studio, mod_managed has configurations for both Debug/Release CLR and Mono. Simply select Debug_CLR and build.

Mono Build

For the Mono version, the Windows build requires that Mono install be accessible at "C:\Program Files\Mono". If you installed somewhere else, symlink it:

Windows 6+:

 mklink /d "C:\Program Files\Mono" "C:\Program Files\Mono-2.0"

Earlier versions:

 Get Junction: http://technet.microsoft.com/en-us/sysinternals/bb896768.aspx
 junction -d "C:\Program Files\Mono" "C:\Program Files\Mono-2.0"

You’ll have to generate mono.lib for your compiler. Details here: http://www.mono-project.com/Embedding_Mono (Search for mono.def). A batch file is provided: monolibx86.cmd:

 C:\freeswitch\src\mod\languages\mod_managed>monolibx86
 C:\freeswitch\src\mod\languages\mod_managed>lib /nologo /def:mono.def /out:mono.lib /machine:x86
  Creating library mono.lib and object mono.exp

Make sure your PATH has the Mono bin folder in it (such as "C:\Program files\Mono\bin") or the mod will not load.

From there, simply compile the mod_managed project in the languages folder. This will build the unmanaged side and the managed loader and put them in your mod directory. If you get errors, the SWIG generated interface might be out of date. From the mod_managed directory, you can run "runswig.cmd" to regenerate the SWIG files. This batch file assumes SWIG is accessible at "\dev\swig".

Thanks to David Brazier, there are now Linux scripts and makefiles, so making it work under Linux should be pretty straightforward.

Loading Modules

Load mod_managed in the modules.conf.xml (in <FS install dir>\conf\autoload_configs) by making sure "<load module="mod_managed" />" is not commented out. Currently does not exist in modules.conf so must manually enter before starting FS.

mod_managed automatically scans the <FreeSWITCH mod dir>\managed for managed modules. (For example, this may be "C:\freeswitch\debug\mod\managed".) mod_managed will not load if the "managed" subdirectory under the modules directory does not exist.

If you compile debug, a few simple demos will compile and load (they are in the FreeSWITCH.Managed source (managed dir underneath mod_managed), Demo.cs). Be sure to create the managed directory.

Dynamic Loading

The "managedload" command takes a path to an assembly. The path can be absolute, or relative to <fsdir>\mod\managed. The assembly is loaded and scanned for AppFunctions and ApiFunctions. New types with the same names replace older types. So if you have an assembly with "Foo.DemoX" loaded, then load another assembly with a type "Foo.DemoX", it will replace the first assembly's definition when you call it from the dialplan. mod_managed and loaded assemblies can not be unloaded.

Native Interop

mod_managed exposes nearly the entire FreeSWITCH C API (courtesy of SWIG). This allows creation of not just API functions and call apps, but any type of module that FreeSWITCH supports (codecs, endpoints, etc.). The types are in the FreeSWITCH.Native namespace. FreeSWITCH.Native. The FreeSWITCH.Native.freeswitch type contains static members to access all the functions.

Examples

See Demo.cs in mod_managed\managed. There are two types of managed modules: AppFunction and ApiFunction. AppFunctions are called with a session, say, from the dial plan. ApiFunctions can be called anywhere.

The "managed" command executes an AppFunction or an ApiFunction synchronously. The "managedrun" command executes an ApiFunction in the background (it starts a new thread).

To specify a function, use the name of the type (such as AppDemo or ApiDemo). You can also fully qualify with the namespace and type name, ex: FreeSWITCH.Demo.AppDemo.

 2008-10-08 17:41:05 [INFO] mod_managed.cpp:310 mod_managed_load() Loading mod_managed (Common Language Infrastructure), Microsoft CLR Version
 2008-10-08 17:41:05 [DEBUG] mod_managed.cpp:299 findLoader() Found all FreeSWITCH.Loader functions.
 2008-10-08 17:41:05 [DEBUG] switch_cpp.cpp:1059 console_log() mod_managed_lib loader is starting with directory 'C:\freeswitch\Debug\mod\managed'.
 2008-10-08 17:41:05 [NOTICE] switch_cpp.cpp:1059 console_log() loadAssemblies: C:\freeswitch\Debug\mod\managed
 2008-10-08 17:41:05 [DEBUG] switch_cpp.cpp:1059 console_log() Loading 'C:\freeswitch\Debug\mod\managed\fsfsdemos.dll'.
 2008-10-08 17:41:05 [DEBUG] switch_cpp.cpp:1059 console_log() Loading 'C:\freeswitch\Debug\mod\managed\pizzacs.dll'.
 2008-10-08 17:41:05 [INFO] switch_cpp.cpp:1059 console_log() Inside AppDemo::Load.
 2008-10-08 17:41:05 [NOTICE] switch_cpp.cpp:1059 console_log() Function FreeSWITCH.Demo.AppDemo loaded.
 2008-10-08 17:41:05 [DEBUG] switch_cpp.cpp:1059 console_log() Inside ApiDemo::Load.
 2008-10-08 17:41:05 [NOTICE] switch_cpp.cpp:1059 console_log() Function FreeSWITCH.Demo.ApiDemo loaded.
 2008-10-08 17:41:05 [DEBUG] switch_cpp.cpp:1059 console_log() Trying to resolve assembly 'mod_managed_lib, Version=1.0.2.0, Culture=neutral, PublicKeyToken=null'.
 2008-10-08 17:41:05 [NOTICE] switch_cpp.cpp:1059 console_log() Function rss.RssReader loaded.
 2008-10-08 17:41:05 [DEBUG] switch_cpp.cpp:1059 console_log() Trying to resolve assembly 'mod_managed_lib, Version=1.0.2.0, Culture=neutral, PublicKeyToken=null'.
 2008-10-08 17:41:05 [NOTICE] switch_cpp.cpp:1059 console_log() Function pizzacs.PizzaApp loaded.
 2008-10-08 17:41:05 [DEBUG] mod_managed.cpp:328 mod_managed_load() Load completed successfully.
 2008-10-08 17:41:05 [DEBUG] mod_managed.cpp:354 mod_managed_load() Load completed successfully.
 
 > help
 ...
 managed,<module> [<args>],Run a module as an API function (Execute)
 managedload,<filename>,Load assembly
 managedrun,<module> [<args>],Run a module (ExecuteBackground)
 
 >managed apidemo a1 123
 API CALL [managed(apidemo a1 123)] output:
 ApiDemo executed with args 'a1 123' and event type API.
 
 >freeswitch@ODIN> managedrun apidemo a1 123
 API CALL [managedrun(apidemo a1 123)] output:
 +OK
 2008-10-08 17:44:25 [DEBUG] switch_cpp.cpp:1059 console_log() ApiDemo on a background thread #(3), with args 'a1 123'.
 2008-10-08 17:44:25 [DEBUG] switch_cpp.cpp:1059 console_log() ExecuteBackground in apidemo completed.


Dialplan and Example

You may point to your managed application by entring the routing into the dialplan as demonstrated below.

<extension name="testapp">
  <condition field="destination_number" expression="^(1024)">
     <action application="managed" data="yourclassname"/>
  </condition>
</extension>

AbortOnHangup

By setting this to return TRUE:

Once the user hangs up, or a disconnect occurs, the thread will ABORT and the managed app will stop running.

See Also

Mod event socket dotnet

Personal tools
Community
Support FreeSWITCH