[Freeswitch-users] Subscribing to events in managed C# / .NET
Mathieu Rene
mrene_lists at avgs.ca
Tue Sep 8 22:51:48 PDT 2009
"Is the trypop method returning without unlocking the mutex?"
No, since the condition returns if a lock could NOT be acquired.
(hence the try).
Mathieu Rene
Avant-Garde Solutions Inc
Office: + 1 (514) 664-1044 x100
Cell: +1 (514) 664-1044 x200
mrene at avgs.ca
On 8-Sep-09, at 10:46 PM, Josh Rivers wrote:
> I confirmed that pop(1) works for me. since it's spinning in it's
> own thread, that's probably the right solution. These are the
> relevant pieces of core code, though:
>
>
> APU_DECLARE(apr_status_t) apr_queue_trypop(apr_queue_t *queue, void
> **data)
> {
> apr_status_t rv;
>
> if (queue->terminated) {
> return APR_EOF; /* no more elements ever again */
> }
>
> rv = apr_thread_mutex_lock(queue->one_big_mutex);
> if (rv != APR_SUCCESS) {
> return rv;
> }
>
> if (apr_queue_empty(queue)) {
> rv = apr_thread_mutex_unlock(queue->one_big_mutex);
> return APR_EAGAIN;
> }
>
> *data = queue->data[queue->out];
> queue->nelts--;
>
> queue->out = (queue->out + 1) % queue->bounds;
> if (queue->full_waiters) {
> Q_DBG("signal !full", queue);
> rv = apr_thread_cond_signal(queue->not_full);
> if (rv != APR_SUCCESS) {
> apr_thread_mutex_unlock(queue->one_big_mutex);
> return rv;
> }
> }
>
> rv = apr_thread_mutex_unlock(queue->one_big_mutex);
> return rv;
> }
>
> AND
>
> APU_DECLARE(apr_status_t) apr_queue_pop(apr_queue_t *queue, void
> **data)
> {
> apr_status_t rv;
>
> if (queue->terminated) {
> return APR_EOF; /* no more elements ever again */
> }
>
> rv = apr_thread_mutex_lock(queue->one_big_mutex);
> if (rv != APR_SUCCESS) {
> return rv;
> }
>
> /* Keep waiting until we wake up and find that the queue is not
> empty. */
> if (apr_queue_empty(queue)) {
> if (!queue->terminated) {
> queue->empty_waiters++;
> rv = apr_thread_cond_wait(queue->not_empty, queue-
> >one_big_mutex);
> queue->empty_waiters--;
> if (rv != APR_SUCCESS) {
> apr_thread_mutex_unlock(queue->one_big_mutex);
> return rv;
> }
> }
> /* If we wake up and it's still empty, then we were
> interrupted */
> if (apr_queue_empty(queue)) {
> Q_DBG("queue empty (intr)", queue);
> rv = apr_thread_mutex_unlock(queue->one_big_mutex);
> if (rv != APR_SUCCESS) {
> return rv;
> }
> if (queue->terminated) {
> return APR_EOF; /* no more elements ever again */
> }
> else {
> return APR_EINTR;
> }
> }
> }
>
> *data = queue->data[queue->out];
> queue->nelts--;
>
> queue->out = (queue->out + 1) % queue->bounds;
> if (queue->full_waiters) {
> Q_DBG("signal !full", queue);
> rv = apr_thread_cond_signal(queue->not_full);
> if (rv != APR_SUCCESS) {
> apr_thread_mutex_unlock(queue->one_big_mutex);
> return rv;
> }
> }
>
> rv = apr_thread_mutex_unlock(queue->one_big_mutex);
> return rv;
> }
>
> Is the trypop method returning without unlocking the mutex?
>
> rv = apr_thread_mutex_lock(queue->one_big_mutex);
> if (rv != APR_SUCCESS) {
> return rv;
> }
>
> On Tue, Sep 8, 2009 at 7:06 PM, Michael Giagnocavo
> <mgg at giagnocavo.net> wrote:
> I’m not sure how EventConsumer is supposed to work – maybe one of
> the real devs can explain how pop works and if it should fail on pop
> 0 or not.
>
>
> From: freeswitch-users-bounces at lists.freeswitch.org [mailto:freeswitch-users-bounces at lists.freeswitch.org
> ] On Behalf Of Phillip Jones
> Sent: Tuesday, September 08, 2009 3:50 PM
>
>
> To: freeswitch-users at lists.freeswitch.org
> Subject: Re: [Freeswitch-users] Subscribing to events in managed
> C# / .NET
>
>
> I build this out.
>
> This seems to work fine:
>
> ThreadPool.QueueUserWorkItem((o) =>
> {
> Log.WriteLine(LogLevel.Notice, "Thread Starting. ");
> EventConsumer con = new EventConsumer("all", "");
> while (true)
> {
> Event ev = con.pop(1);
> Log.WriteLine(LogLevel.Alert, "Event: " +
> ev.GetHeader("Event-Name"));
> //freeswitch.msleep(100);
> }
> });
>
> With
> Event ev = con.pop(0) however FS crashes with a
> System.NullReferenceException (attached)
>
>
>
>
>
>
>
>
>
> On Tue, Sep 8, 2009 at 5:20 PM, Raffaele P. Guidi <raffaele.p.guidi at gmail.com
> > wrote:
>
> Oh, I see... all those years wasted doing java stuff! :D
>
>
> On Tue, Sep 8, 2009 at 22:46, Michael Giagnocavo
> <mgg at giagnocavo.net> wrote:
>
> “ ThreadPool.QueueUserWorkItem((o) =>”
>
> That starts a lambda, which is compiled to a delegate, same as
> anonymous methods.
>
> Guess I’ll wait for him to respond on the crash he gets.
>
>
>
> From: freeswitch-users-bounces at lists.freeswitch.org [mailto:freeswitch-users-bounces at lists.freeswitch.org
> ] On Behalf Of Raffaele P. Guidi
> Sent: Tuesday, September 08, 2009 12:22 PM
>
>
> To: freeswitch-users at lists.freeswitch.org
> Subject: Re: [Freeswitch-users] Subscribing to events in managed
> C# / .NET
>
>
> Well, I can't see any delegate in josh sample, just a
> ThreadPool.QueueUserWorkItem. Here is an example that, at least on
> my system (I reached my home pc in the meanwhile), works fine.
>
>
> public class LoadPluginDemo : ILoadNotificationPlugin {
>
> delegate void Listener();
>
> private void EventListener() {
>
> EventConsumer con = new EventConsumer("all", null);
>
> while (true){
>
> Event ev = con.pop(1);
>
> Log.WriteLine(LogLevel.Notice, "Got event " +
> ev.GetHeader("Event-Name"));
>
> }
>
> }
>
> public bool Load() {
>
> Log.WriteLine(LogLevel.Notice, "LoadDemo running.");
>
> new Listener(EventListener).BeginInvoke(null,null);
>
> return true;
>
> }
>
> }
>
>
>
>
> On Tue, Sep 8, 2009 at 18:43, Michael Giagnocavo
> <mgg at giagnocavo.net> wrote:
>
> That’s what his sample does, but he says it crashes.
>
>
> From: freeswitch-users-bounces at lists.freeswitch.org [mailto:freeswitch-users-bounces at lists.freeswitch.org
> ] On Behalf Of Raffaele P. Guidi
> Sent: Tuesday, September 08, 2009 10:08 AM
>
>
> To: freeswitch-users at lists.freeswitch.org
> Subject: Re: [Freeswitch-users] Subscribing to events in managed
> C# / .NET
>
>
> Hi, you just have to use delegates to asynchronously call the
> function containing the loop and return back the control to the
> calling thread. Here an example (don't have my code at hand, hope it
> doesn't contain typos).
>
>
> Regards,
>
> Raffaele
>
>
> public class TestLoop : ILoadNotificationPlugin
>
> {
>
>
> Delegate void DoStuffDelegate();
>
>
> public void doStuff()
>
> {
>
> EventConsumer con = new EventConsumer("all", "");
>
> while (true)
>
> {
>
> Event ev = con.pop(0);
>
> Log.WriteLine(LogLevel.Notice, "Event: " +
> ev.serialized_string);
>
> freeswitch.msleep(100);
>
> }
>
> }
>
> public bool Load()
>
> {
>
> DoStuffDelegate dsdlg = new DoStuffDelegate(doStuff);
>
> dsdlg.BeginInvoke();
>
> }
>
> }
>
> On Tue, Sep 8, 2009 at 08:21, Josh Rivers <josh at radianttiger.com>
> wrote:
>
> Thanks for the response!
>
>
> I have tried putting a long-running loop here, but then it blocks
> anything else managed from happening:
>
>
> public class TestLoop : ILoadNotificationPlugin
>
> {
>
> public bool Load()
>
> {
>
> EventConsumer con = new EventConsumer("all", "");
>
> while (true)
>
> {
>
> Event ev = con.pop(0);
>
> Log.WriteLine(LogLevel.Notice, "Event: " +
> ev.serialized_string);
>
> freeswitch.msleep(100);
>
> }
>
> }
>
> }
>
>
> However, if I fork off a thread here, freeswitch crashes:
>
> public class TestLoop : ILoadNotificationPlugin
>
> {
>
> public bool Load()
>
> {
>
> ThreadPool.QueueUserWorkItem((o) =>
>
> {
>
> Log.WriteLine(LogLevel.Notice, "Thread Starting. ");
>
> EventConsumer con = new EventConsumer("all", "");
>
> while (true)
>
> {
>
> Event ev = con.pop(0);
>
> Log.WriteLine(LogLevel.Notice, "Event: " +
> ev.serialized_string);
>
> freeswitch.msleep(100);
>
> }
>
> });
>
> return true;
>
> }
>
> }
>
>
> It doesn't look like this is a good place to start a long-running
> process?
>
>
> Thanks!
>
> Josh
>
>
> On Mon, Sep 7, 2009 at 11:05 PM, Raffaele P. Guidi <raffaele.p.guidi at gmail.com
> > wrote:
>
> Yes!
>
>
> public class LoadDemo : ILoadNotificationPlugin {
>
> public bool Load() {
>
> Log.WriteLine(LogLevel.Notice, "LoadDemo running.");
>
> return true;
>
> }
>
> }
>
>
> this example is from Michael Giagnocavo's Demo.csx which you can
> find into the mod_managed svn.
>
>
> And let me add that works like a charm :)
>
>
> Ciao,
>
> Raffaele
>
>
> On Sun, Sep 6, 2009 at 22:50, Josh Rivers <josh at radianttiger.com>
> wrote:
>
> Is there a way to start this when FreeSWITCH starts? The lua and
> perl modules have a 'startup-script' configuration preference. Is
> there something similar in mod_managed? Or is there a way to have an
> api command executed at a startup?
>
>
> <quote author="Phillip Jones">
>
> Exactly what I was after - thank you!
>
>
> On Thu, Sep 3, 2009 at 1:54 PM, Jeff Lenk <jlenk at frontiernet.net>
> wrote:
>
>
> >
>
> > try something like this
>
> >
>
> > EventConsumer con = new EventConsumer("all", "");
>
> > Event ev = con.pop(0);
>
> >
>
> > see lua sample -
>
> > http://wiki.freeswitch.org/wiki/Lua#freeswitch.EventConsumer
>
> >
>
> >
>
> > Phillip Jones-2 wrote:
>
> > >
>
> > > Hi there,
>
> > >
>
> > > mod_managed exposes EventReceivedFunction such that:
>
> > >
>
> > > Session.EventReceivedFunction = (e) =>
>
> > > {
>
> > > Log.WriteLine(LogLevel.Alert, "Received Event {0}",
> e.ToString());
>
> > > return "";
>
> > > };
>
> > >
>
> > > should trap all events to which i subscribe.
>
> > >
>
> > >
>
> > > But how do I subscribe to events? What is the .NET / managed
> equivalent
>
> > > of:
>
> > >
>
> > > switch_event_bind(const char *id, switch_event_types_t event,
> const char
>
> > > *subclass_name, switch_event_callback_t callback, void
> *user_data);
>
> > >
>
> > >
>
> > >
>
> > > Thank you!
>
> > >
>
> > >
>
> > >
>
>
> _______________________________________________
> FreeSWITCH-users mailing list
> FreeSWITCH-users at lists.freeswitch.org
> http://lists.freeswitch.org/mailman/listinfo/freeswitch-users
> UNSUBSCRIBE:http://lists.freeswitch.org/mailman/options/freeswitch-users
> http://www.freeswitch.org
>
>
>
> _______________________________________________
> FreeSWITCH-users mailing list
> FreeSWITCH-users at lists.freeswitch.org
> http://lists.freeswitch.org/mailman/listinfo/freeswitch-users
> UNSUBSCRIBE:http://lists.freeswitch.org/mailman/options/freeswitch-users
> http://www.freeswitch.org
>
>
>
> _______________________________________________
> FreeSWITCH-users mailing list
> FreeSWITCH-users at lists.freeswitch.org
> http://lists.freeswitch.org/mailman/listinfo/freeswitch-users
> UNSUBSCRIBE:http://lists.freeswitch.org/mailman/options/freeswitch-users
> http://www.freeswitch.org
>
>
>
> _______________________________________________
> FreeSWITCH-users mailing list
> FreeSWITCH-users at lists.freeswitch.org
> http://lists.freeswitch.org/mailman/listinfo/freeswitch-users
> UNSUBSCRIBE:http://lists.freeswitch.org/mailman/options/freeswitch-users
> http://www.freeswitch.org
>
>
>
> _______________________________________________
> FreeSWITCH-users mailing list
> FreeSWITCH-users at lists.freeswitch.org
> http://lists.freeswitch.org/mailman/listinfo/freeswitch-users
> UNSUBSCRIBE:http://lists.freeswitch.org/mailman/options/freeswitch-users
> http://www.freeswitch.org
>
>
>
> _______________________________________________
> FreeSWITCH-users mailing list
> FreeSWITCH-users at lists.freeswitch.org
> http://lists.freeswitch.org/mailman/listinfo/freeswitch-users
> UNSUBSCRIBE:http://lists.freeswitch.org/mailman/options/freeswitch-users
> http://www.freeswitch.org
>
>
>
> _______________________________________________
> FreeSWITCH-users mailing list
> FreeSWITCH-users at lists.freeswitch.org
> http://lists.freeswitch.org/mailman/listinfo/freeswitch-users
> UNSUBSCRIBE:http://lists.freeswitch.org/mailman/options/freeswitch-users
> http://www.freeswitch.org
>
>
> _______________________________________________
> FreeSWITCH-users mailing list
> FreeSWITCH-users at lists.freeswitch.org
> http://lists.freeswitch.org/mailman/listinfo/freeswitch-users
> UNSUBSCRIBE:http://lists.freeswitch.org/mailman/options/freeswitch-users
> http://www.freeswitch.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.freeswitch.org/pipermail/freeswitch-users/attachments/20090908/e042fdfa/attachment-0002.html
More information about the FreeSWITCH-users
mailing list