[Red5] number of connections

Walter Tak walter at waltertak.com
Fri Jun 27 15:13:58 PDT 2008


Forgive me for being ignorant on this topic but why would this variable ( public int num_connected     = 0; ) have multiple instances inside the application adapter ?

If I understood you correct the content of [num_connected] could become inaccurate due to the non-thread-safe access to it while using the MultiThreadedApplicationAdapter but the OP
even used the old way ( public class Application extends ApplicationAdapter { )  ... 

I was assuming this was the ordinary 'thread-safe' approach anyway.

Besides the difference between "Application extends MultiThreadedApplicationAdapter " and "Application extends ApplicationAdapter" I always assumed 
that a declared variable in the constructor of the Application would be the same variable for all clients connecting to that particular application for the lifetime of the app ... Right ?

Walter

  ----- Original Message ----- 
  From: Mondain 
  To: red5 at osflash.org 
  Sent: Friday, 27 June 2008 19:52
  Subject: Re: [Red5] number of connections


  Besides the fact that the counter var was not static and he may have ended up with multiple versions of it. :)


  On Fri, Jun 27, 2008 at 10:50 AM, Mondain <mondain at gmail.com> wrote:

    There wont be any exceptions, he will most likely just get inaccurate counts depending on which thread hits the -- or ++. 

    Paul



    On Fri, Jun 27, 2008 at 9:48 AM, Walter Tak <walter at waltertak.com> wrote:

      Wouldn't he see exceptions in the log when 2 treads try to access the non-thread-safe integer in his original example ?

      Or would java/red5 fail silent on the num_connected--; operation ?

      Regards,
      Walter

        ----- Original Message ----- 
        From: Mondain 
        To: red5 at osflash.org 
        Sent: Friday, June 27, 2008 6:07 PM
        Subject: Re: [Red5] number of connections


        I would also like to add that the application adapter is multi-threaded so you must use a thread-safe counter. Java provides a set of Atomic objects that may be used for this sort of application. Below you will find your updated code:

        package org.red5.server;

        import java.util.concurrent.atomic.AtomicInteger;

        import org.apache.commons.logging.Log;
        import org.apache.commons.logging.LogFactory;

        import org.red5.server.api.IConnection;
        import org.red5.server.adapter.ApplicationAdapter;

        public class Application extends ApplicationAdapter {

            public static AtomicInteger num_connected = new AtomicInteger();

            public boolean appStart() {
               log.info( "Red5First.appStart" );
               return true;
            }

            public void appStop() {
               log.info( "Red5First.appStop" );
            }

            public boolean appConnect(IConnection conn, Object[] params) {
               log.info( "Red5First.appConnect {}", conn.getClient().getId() );
               num_connected.getAndIncrement();
               boolean accept = (Boolean) params[0];
               if ( !accept ) rejectClient( "you passed false..." );
               return true;
            }

            public void appDisconnect(IConnection conn, Object[] params) {
               num_connected.getAndDecrement();
               log.info( "Red5First.appDisconnect {}", conn.getClient().getId() );
            }

            public int numberofusers() {
               return num_connected.get();
            }

        }

        Javadoc: http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/atomic/AtomicInteger.html


        Paul


        On Fri, Jun 27, 2008 at 8:49 AM, Walter Tak <walter at waltertak.com> wrote:

          Which version of Red5 are you using
          Where did you get it from (binary 0.6.3 , 0.7.0 + hotfix, SVN)
          What browser are you using

          Tried firefox when you are using IE and vice versa ?

          Some versions IE have a problem ; appDisconnect does not fire even if you
          close the TAB which holds the SWF.

          If you'd close the browser then the event fires (IE keeps the connection
          open to R5 even when the SWF was unloaded).

          Also some specific version of Red5 had some problems with appDisconnect so
          you might want to test with at least 2 different versions of R5, like the
          old 0.6.3 and one of the latest trunk versions.

          You can run them all on 1 server ; just install Red5 in a different
          directory, do not install the service on Windows and have Red5 listen on
          different ports and have your client connect to the right port to test a
          specific Red5-instance.

          Walter




          ----- Original Message -----
          From: "Access-Dev ML" <ml at access-dev.com>
          To: "red5" <red5 at osflash.org>
          Sent: Friday, June 27, 2008 5:25 PM
          Subject: [Red5] number of connections


          > Hello,
          >
          > I m begging red5 dev and try to build an application that maintain a count
          > of the numbers of simultaneous
          > connected users.
          >
          > Maybe there is classes for that but i try the simple way and it s not
          > working, the number of users keeping growing even if
          > i disconnect flash client.
          >
          > Maybe red5 is unable to find the the flash client has closed the window
          >
          > here is the code:
          >
          > package org.red5.server;
          >
          > import org.apache.commons.logging.Log;
          > import org.apache.commons.logging.LogFactory;
          >
          > import org.red5.server.api.IConnection;
          > import org.red5.server.adapter.ApplicationAdapter;
          >
          > public class Application extends ApplicationAdapter
          > {
          >    private static final Log log = LogFactory.getLog( Application.class );
          >    public int num_connected     = 0;
          >
          >    public boolean appStart ( )
          >    {
          >        log.info( "Red5First.appStart" );
          >        num_connected = 0;
          >        return true;
          >    }
          >
          >
          >    public void appStop ( )
          >    {
          >        log.info( "Red5First.appStop" );
          >    }
          >
          >    public boolean appConnect( IConnection conn , Object[] params )
          >    {
          >        log.info( "Red5First.appConnect " + conn.getClient().getId() );
          >        num_connected++;
          >        boolean accept = (Boolean)params[0];
          >        if ( !accept ) rejectClient( "you passed false..." );
          >        return true;
          >    }
          >
          >    public void appDisconnect( IConnection conn , Object[] params )
          >    {
          >    num_connected--;
          >        log.info( "Red5First.appDisconnect " + conn.getClient().getId() );
          >    }
          >
          >    public int numberofusers() {
          >    return num_connected;
          >
          >    }
          >
          >
          > on the flash side :
          >
          > package
          > {
          >
          >    import flash.net.NetConnection;
          >    import flash.net.ObjectEncoding;
          > import flash.net.Responder;
          >    import flash.events.NetStatusEvent;
          >    import flash.display.Sprite;
          >
          >    public class Red5FirstClient extends Sprite
          >    {
          >        public var nc:NetConnection;
          >        public function Red5FirstClient()
          >        {
          >            this.nc = new NetConnection( );
          > this.nc.client = this;
          >            this.nc.objectEncoding = ObjectEncoding.AMF0;
          >            this.nc.addEventListener( NetStatusEvent.NET_STATUS ,
          > netStatus );
          >            this.nc.connect( "rtmp://localhost/firstapp" , true );
          >
          >        }
          >
          > public function numberofusers():void {
          >
          > trace("this nc " + this.nc);
          > this.nc.call("numberofusers",new Responder(this.callbackusers));
          >
          >
          > }
          >
          > public function callbackusers(result:int) {
          > trace("num users " + result);
          > }
          >
          >        private function netStatus ( event:NetStatusEvent ):void
          >        {
          >            trace( event.info.code );
          >            if ( event.info.code == "NetConnection.Connect.Rejected" ){
          >                trace( event.info.application );
          >            }
          >        } // end netStatus
          >
          >
          >    } // end Red5FirstClient
          >
          > } // end package
          > }
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://osflash.org/pipermail/red5_osflash.org/attachments/20080628/1e439228/attachment-0001.html 


More information about the Red5 mailing list