• Module to limit the number of connections to an application (ModuleLimitConnectionsToApplication)

    This article describes how to use a module to limit the number of connections to an application.

    Note: This module has been removed from the Wowza Module Collection because the same functionality has been added to Wowza Streaming Engine™ software. For more information about how to access the functionality of this module in Wowza Streaming Engine, see Maximum Connections. The source code has been retained here for reference.
    package com.wowza.wms.example.module;
    
    import com.wowza.wms.amf.*;
    import com.wowza.wms.application.*;
    import com.wowza.wms.client.*;
    import com.wowza.wms.httpstreamer.model.IHTTPStreamerSession;
    import com.wowza.wms.request.*;
    import com.wowza.wms.rtp.model.*;
    import com.wowza.wms.module.*;
    
    public class ModuleLimitConnectionsToApplication extends ModuleBase
    {
    	static final public int MAXCONNECTIONS = 200;
    	
    	private int maxApplicationConnections = MAXCONNECTIONS;
    	
    	public void onAppStart(IApplicationInstance appInstance)
    	{
    		this.maxApplicationConnections = appInstance.getProperties().getPropertyInt("maxApplicationConnections", maxApplicationConnections);
    		getLogger().info("ModuleLimitConnectionsToApplication limit: " + maxApplicationConnections);
    	}
    	
    	public void changeLimit(IClient client, RequestFunction function,
    			AMFDataList params) {
    		client.getAppInstance().broadcastMsg("handlerName");
    		Integer newLimit = params.getInt(PARAM1);
    		this.maxApplicationConnections = newLimit;
    	}
    	
    	public void onConnect(IClient client, RequestFunction function, AMFDataList params)
    	{	 
    		IApplicationInstance appInstance = client.getAppInstance();
    		IApplication app = appInstance.getApplication();
    		long count = app.getConnectionCounter().getCurrent(); 
    		
    		String flashver = client.getFlashVer();
    		getLogger().info("Flashver: " + flashver);
    		
    		Boolean isPublisher = false;
    		try
    		{
    		isPublisher = flashver.startsWith(client.getAppInstance().getProperties().getPropertyStr("AllowEncoder"));
    		}
    		catch(Exception ex) 
    		{
    		}
    		
    		getLogger().info("ModuleLimitConnectionsToApplication count: " + (count + 1));
    		
    		if ((count+1) > this.maxApplicationConnections && !isPublisher)
    		{
    			client.rejectConnection("Over application connection limit ["+app.getName()+"/"+appInstance.getName()+"]: Limit is: "+this.maxApplicationConnections);
    			getLogger().info("ModuleLimitConnectionsToApplication Flash connection rejected ");
    		}
    	}
    	
    	public void onHTTPSessionCreate(IHTTPStreamerSession httpSession)
    	{
    		IApplicationInstance appInstance = httpSession.getAppInstance();
    		IApplication app = appInstance.getApplication();
    		long count = app.getConnectionCounter().getCurrent();
    		
    		getLogger().info("ModuleLimitConnectionsToApplication count: " + (count + 1));
    		
    		if ((count+1) > this.maxApplicationConnections)
    		{
    			httpSession.rejectSession();
    			getLogger().info("ModuleLimitConnectionsToApplication Smooth connection rejected ");
    		}		
    	}
    
    
    	public void onRTPSessionCreate(RTPSession rtpSession)
    	{
    		IApplicationInstance appInstance = rtpSession.getAppInstance();
    		IApplication app = appInstance.getApplication();
    		long count = app.getConnectionCounter().getCurrent();
    
    		getLogger().info("ModuleLimitConnectionsToApplication count: " + (count + 1));
    		
    		if ((count+1) > this.maxApplicationConnections)
    		{
    			rtpSession.rejectSession();
    			getLogger().info("ModuleLimitConnectionsToApplication RTSP connection rejected ");
    		}
    	}	
    }
    Add this Module last in the Modules list at /conf/[app-name]/Application.xml.

    <Module> 
    	<Name>ModuleLimitStreamViewers</Name> 
    	<Description>Limit connects to an Application</Description> 
    	<Class>com.wowza.wms.example.module.ModuleLimitConnectionsToApplication</Class> 
    </Module>
    You can set the connection limit by setting the following property in the <Properties> section at the bottom of [install-dir]/conf/[application]/Application.xml (where [application] is the name of your application).

    <Property>
    	<Name>maxApplicationConnections</Name>
    	<Value>200</Value>
    	<Type>Integer</Type>
    </Property>
    Logging

    The logs will show the limit and the current number of connections. In this example, the limit is 200 connections.
    comment	server	INFO	200	-	ModuleLimitConnectionsToApplication limit: 200
    The logs will report the number of connections. In this example, there is 1 current connection.
    comment	server	INFO	200	-	ModuleLimitConnectionsToApplication count: 01
    When you have exceeded the number of connections allowed, you will see a comment in the log similar to the following:
    comment	server	INFO   	200	-	ModuleLimitConnectionsToApplication Flash connection rejected
    comment	server	INFO	        200	-	ModuleLimitConnectionsToApplication Cupertino connection rejected
    comment   server       WARN	200	-	HTTPStreamerAdapterCupertinoStreamer.onPlaylist[vod/mp4:sample.mp4/playlist.m3u8]: Over license limit
    comment	server	WARN	200	-	HTTPStreamerAdapterSanJoseStreamer.onPlaylist: Over license limit
    Notes:
    If you are you using a live encoder, also add this property:
    <Property>
     <Name>AllowEncoder</Name>
     <Value>Wirecast</Value> <!--FM, Wirecast-->
    </Property>
    If you are using a Telestream Wirecast encoder, set the value to Wirecast.

    If you are using an RTMP live encoder that uses the Flash Media version, set this value to FM.

    Wowza media server software and all components, including modules, source code, and other related items offered on this page, are copyrighted (c) 2006-2014 by Wowza Media Systems, LLC, all rights reserved, and are licensed pursuant to the Wowza Media Software End User License Agreement.
    Updated: For Wowza Streaming Engine 4.0 on 02-26-2014.

    If you're having problems or want to discuss this article, post in our forum.