package ChatSystem;
import java.util.List;
import java.util.Iterator;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.red5.server.adapter.ApplicationAdapter ;
import org.red5.server.api.IClient;
import org.red5.server.api.IConnection;
import org.red5.server.api.IScope;
import org.red5.server.api.Red5;
import org.red5.server.api.service.IPendingServiceCall;
import org.red5.server.api.service.IPendingServiceCallback;
import org.red5.server.api.service.IServiceCapableConnection;
import org.red5.server.api.stream.IBroadcastStream;
import org.red5.server.api.stream.IPlayItem;
import org.red5.server.api.stream.IPlaylistSubscriberStream;
import org.red5.server.api.stream.IStreamAwareScopeHandler;
import org.red5.server.api.stream.ISubscriberStream;
import org.red5.server.api.so.ISharedObject ;
public class CSApplication extends ApplicationAdapter implements
IStreamAwareScopeHandler, IPendingServiceCallback {
public String adminUserName = ” Admin”;
public String adminUserPassword = “pwd”;
public String userName;
public String userPwd;
//private boolean persistent;
public String URL;
String[] clientsByID = new String[100];
private static final Log log = LogFactory.getLog (CSApplication.class);
public boolean appStart(IScope scope) {
// initiating the handler here
log.info(”Client is connected “);
log.info(”Creating Shared Objects “);
createSharedObject(scope, “adminStatus”, true);
ISharedObject adminStatus_so = getSharedObject(scope,
“adminStatus”);
adminStatus_so.removeAttributes();
adminStatus_so.setAttribute(”adminIsIn”, false);
createSharedObject(scope, “userList”, true);
//ISharedObject userList_so = getSharedObject(scope, “userList”);
//userList_so.setAttribute(”List”,list);
//userList_so.removeAttributes();
return true;
}
@Override
public boolean appConnect(IConnection conne, Object[] params) {
IServiceCapableConnection service = (IServiceCapableConnection)
conne;
log.info(”Client is connected ” + conne.getClient().getId() + ”
conne ” + conne);
log.info(”Setting stream id as : “+ getClients().size()); //
just a unique number
service.invoke(”setId”, new Object[]{conne.getClient().getId()},
this);
return true;
}
//@Override
public boolean appJoin(IClient client, IScope scope, String
userName, String userPwd) {
log.info(”Client joined app ” + client.getId());
IConnection conne = Red5.getConnectionLocal();
client.setAttribute(”userName”,userName);
client.setAttribute(”userPwd”,userPwd);
ISharedObject UserList = getSharedObject(scope,”UserList”);
if (client.getAttribute(userName)== “__mainPage__”){
//ISharedObject UserList = getSharedObject(scope,”UserList”);
UserList.setAttribute(client.getId(),userName);
}
else if(userName == adminUserName){
if(userPwd == adminUserPassword){
String IDofClient = conne.getClient ().getId();
removeFromList(IDofClient);
//URL notifications
URLNotify(IDofClient,URL);
//<- Find the Notification way
ISharedObject adminStatus_so = getSharedObject(scope,
“adminStatus”);
adminStatus_so.setAttribute(”adminIsIn”, true);
this.userName = userName;
}
rejectClient(conne.getClient());
}
return true;
}
public void putMeInLine(String userID, String userName) {
log.info(”putMeInLine:”+userID+”,”+userName);
//ISharedObject UserList_so = getSharedObject(scope,”UserList”);
clientsByID[1]= userID;
addToLine(userID, userName);
}
public String URLNotify(String clientID,String URL){
// write a Iterator to find the specied userId and then send him the URL
return URL;
}
public void addToLine(String userID,String userName){
ISharedObject UserList_so = getSharedObject(scope,”UserList”);
UserList_so.setAttribute(userID,userName);
}
public void removeMeFromLine(String userId){
ISharedObject UserList_so = getSharedObject(scope,”UserList”);
UserList_so.removeAttribute(userId);
}
public boolean removeFromList(String ID)
{
log.info(”removeFromList:”+ID);
ISharedObject userList_so = getSharedObject(scope, “userList”);
//list = userList_so.getAttributes(”list”);
Object[]list = new Object[100];
//list = userList_so.getData(ID);
for (int i=0; i<list.length; i++) {
list[i] = userList_so.getAttribute(ID);
}
if (userList_so.hasAttribute(ID)){
for (int i=0; i<list.length; i++) {
if (list[i] == userList_so.getAttribute(ID)) {
this.removeAttribute(ID);
}
}
return true;
}return false;
}
public String setName(String name){
log.info(”setName:”+name);
userName= name;
return “ack”;
}
public String setDomainName(String name) {
log.info(”setDomainName:”+name);
ISharedObject userList_so = getSharedObject(scope, “userList”);
userList_so.setAttribute(”userdomainname”,name);
return “ack”;
}
public boolean userBell(String userId){
IConnection current = Red5.getConnectionLocal();
Iterator<IConnection> itr = scope.getConnections();
while ( itr.hasNext()) {
IConnection conne = itr.next();
IServiceCapableConnection service = (IServiceCapableConnection)
conne;
if (conne.equals(current))
service.invoke(”userBell”);
}
return true;
}
public void streamPublishStart(IBroadcastStream stream) {
// Notify all the clients that the stream had been started
log.debug(”Starting the Stream Broadcast: “+stream.getPublishedName());
IConnection current = Red5.getConnectionLocal();
Iterator<IConnection> itr = scope.getConnections();
while (itr.hasNext()) {
IConnection conne = itr.next();
if (conne.equals (current))
// Current client Should not be Notified
continue;
if (conne instanceof IServiceCapableConnection) {
((IServiceCapableConnection) conne).invoke(”newStream”, new
Object[]{stream.getPublishedName()}, this);
log.debug(”Now sending notification to “+conne);
}
}
}
public void streamBroadcastStart(IBroadcastStream stream) {
}
public void streamBroadcastClose(IBroadcastStream stream) {
}
public void streamSubscriberStart(ISubscriberStream stream) {
}
public void streamPlaylistItemStop(IPlaylistSubscriberStream stream,
IPlayItem item) {
}
public void streamPlaylistItemPlay(IPlaylistSubscriberStream stream,
IPlayItem item, boolean isLive) {
}
public void streamPlaylistVODItemPause(IPlaylistSubscriberStream stream,
IPlayItem item, int position) {
}
public void streamPlaylistVODItemResume(IPlaylistSubscriberStream
stream, IPlayItem item, int position) {
}
public void streamPlaylistVODItemSeek(IPlaylistSubscriberStream stream,
IPlayItem item, int position) {
}
public void streamSubscriberClose(ISubscriberStream stream) {
}
/**
* Get streams. called from client
*
* @return iterator of broadcast stream names
*/
public List<String> getStreams(){
IConnection conne = Red5.getConnectionLocal();
return getBroadcastStreamNames(conne.getScope());
}
/**
* Handle callback from service call.
*/
public void resultReceived(IPendingServiceCall call) {
log.info(”Received result ” + call.getResult() + ” for ” +
call.getServiceMethodName());
}
public void appDisconnect(IConnection conne,IBroadcastStream
stream,IScope scope) {
log.info(”Application is disconnected “);
IClient client = conne.getClient();
if (client.equals(adminUserName)&& scope == conne.getScope()){
ISharedObject adminStatus_so = getSharedObject(scope,
“adminStatus”);
adminStatus_so.beginUpdate();
adminStatus_so.setAttribute(”adminIsIn”, false);
stream.close();
conne.close();
super.disconnect (conne,scope);
adminStatus_so.endUpdate();
}
else
{
ISharedObject userList_so = getSharedObject(scope, “userList”);
userList_so.beginUpdate();
removeFromList( conne.getClient().getId());
stream.close();
conne.getClient().disconnect();
super.disconnect(conne,scope);
userList_so.endUpdate();
}
}
}
import java.util.List;
import java.util.Iterator;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.red5.server.adapter.ApplicationAdapter ;
import org.red5.server.api.IClient;
import org.red5.server.api.IConnection;
import org.red5.server.api.IScope;
import org.red5.server.api.Red5;
import org.red5.server.api.service.IPendingServiceCall;
import org.red5.server.api.service.IPendingServiceCallback;
import org.red5.server.api.service.IServiceCapableConnection;
import org.red5.server.api.stream.IBroadcastStream;
import org.red5.server.api.stream.IPlayItem;
import org.red5.server.api.stream.IPlaylistSubscriberStream;
import org.red5.server.api.stream.IStreamAwareScopeHandler;
import org.red5.server.api.stream.ISubscriberStream;
import org.red5.server.api.so.ISharedObject ;
public class CSApplication extends ApplicationAdapter implements
IStreamAwareScopeHandler, IPendingServiceCallback {
public String adminUserName = ” Admin”;
public String adminUserPassword = “pwd”;
public String userName;
public String userPwd;
//private boolean persistent;
public String URL;
String[] clientsByID = new String[100];
private static final Log log = LogFactory.getLog (CSApplication.class);
public boolean appStart(IScope scope) {
// initiating the handler here
log.info(”Client is connected “);
log.info(”Creating Shared Objects “);
createSharedObject(scope, “adminStatus”, true);
ISharedObject adminStatus_so = getSharedObject(scope,
“adminStatus”);
adminStatus_so.removeAttributes();
adminStatus_so.setAttribute(”adminIsIn”, false);
createSharedObject(scope, “userList”, true);
//ISharedObject userList_so = getSharedObject(scope, “userList”);
//userList_so.setAttribute(”List”,list);
//userList_so.removeAttributes();
return true;
}
@Override
public boolean appConnect(IConnection conne, Object[] params) {
IServiceCapableConnection service = (IServiceCapableConnection)
conne;
log.info(”Client is connected ” + conne.getClient().getId() + ”
conne ” + conne);
log.info(”Setting stream id as : “+ getClients().size()); //
just a unique number
service.invoke(”setId”, new Object[]{conne.getClient().getId()},
this);
return true;
}
//@Override
public boolean appJoin(IClient client, IScope scope, String
userName, String userPwd) {
log.info(”Client joined app ” + client.getId());
IConnection conne = Red5.getConnectionLocal();
client.setAttribute(”userName”,userName);
client.setAttribute(”userPwd”,userPwd);
ISharedObject UserList = getSharedObject(scope,”UserList”);
if (client.getAttribute(userName)== “__mainPage__”){
//ISharedObject UserList = getSharedObject(scope,”UserList”);
UserList.setAttribute(client.getId(),userName);
}
else if(userName == adminUserName){
if(userPwd == adminUserPassword){
String IDofClient = conne.getClient ().getId();
removeFromList(IDofClient);
//URL notifications
URLNotify(IDofClient,URL);
//<- Find the Notification way
ISharedObject adminStatus_so = getSharedObject(scope,
“adminStatus”);
adminStatus_so.setAttribute(”adminIsIn”, true);
this.userName = userName;
}
rejectClient(conne.getClient());
}
return true;
}
public void putMeInLine(String userID, String userName) {
log.info(”putMeInLine:”+userID+”,”+userName);
//ISharedObject UserList_so = getSharedObject(scope,”UserList”);
clientsByID[1]= userID;
addToLine(userID, userName);
}
public String URLNotify(String clientID,String URL){
// write a Iterator to find the specied userId and then send him the URL
return URL;
}
public void addToLine(String userID,String userName){
ISharedObject UserList_so = getSharedObject(scope,”UserList”);
UserList_so.setAttribute(userID,userName);
}
public void removeMeFromLine(String userId){
ISharedObject UserList_so = getSharedObject(scope,”UserList”);
UserList_so.removeAttribute(userId);
}
public boolean removeFromList(String ID)
{
log.info(”removeFromList:”+ID);
ISharedObject userList_so = getSharedObject(scope, “userList”);
//list = userList_so.getAttributes(”list”);
Object[]list = new Object[100];
//list = userList_so.getData(ID);
for (int i=0; i<list.length; i++) {
list[i] = userList_so.getAttribute(ID);
}
if (userList_so.hasAttribute(ID)){
for (int i=0; i<list.length; i++) {
if (list[i] == userList_so.getAttribute(ID)) {
this.removeAttribute(ID);
}
}
return true;
}return false;
}
public String setName(String name){
log.info(”setName:”+name);
userName= name;
return “ack”;
}
public String setDomainName(String name) {
log.info(”setDomainName:”+name);
ISharedObject userList_so = getSharedObject(scope, “userList”);
userList_so.setAttribute(”userdomainname”,name);
return “ack”;
}
public boolean userBell(String userId){
IConnection current = Red5.getConnectionLocal();
Iterator<IConnection> itr = scope.getConnections();
while ( itr.hasNext()) {
IConnection conne = itr.next();
IServiceCapableConnection service = (IServiceCapableConnection)
conne;
if (conne.equals(current))
service.invoke(”userBell”);
}
return true;
}
public void streamPublishStart(IBroadcastStream stream) {
// Notify all the clients that the stream had been started
log.debug(”Starting the Stream Broadcast: “+stream.getPublishedName());
IConnection current = Red5.getConnectionLocal();
Iterator<IConnection> itr = scope.getConnections();
while (itr.hasNext()) {
IConnection conne = itr.next();
if (conne.equals (current))
// Current client Should not be Notified
continue;
if (conne instanceof IServiceCapableConnection) {
((IServiceCapableConnection) conne).invoke(”newStream”, new
Object[]{stream.getPublishedName()}, this);
log.debug(”Now sending notification to “+conne);
}
}
}
public void streamBroadcastStart(IBroadcastStream stream) {
}
public void streamBroadcastClose(IBroadcastStream stream) {
}
public void streamSubscriberStart(ISubscriberStream stream) {
}
public void streamPlaylistItemStop(IPlaylistSubscriberStream stream,
IPlayItem item) {
}
public void streamPlaylistItemPlay(IPlaylistSubscriberStream stream,
IPlayItem item, boolean isLive) {
}
public void streamPlaylistVODItemPause(IPlaylistSubscriberStream stream,
IPlayItem item, int position) {
}
public void streamPlaylistVODItemResume(IPlaylistSubscriberStream
stream, IPlayItem item, int position) {
}
public void streamPlaylistVODItemSeek(IPlaylistSubscriberStream stream,
IPlayItem item, int position) {
}
public void streamSubscriberClose(ISubscriberStream stream) {
}
/**
* Get streams. called from client
*
* @return iterator of broadcast stream names
*/
public List<String> getStreams(){
IConnection conne = Red5.getConnectionLocal();
return getBroadcastStreamNames(conne.getScope());
}
/**
* Handle callback from service call.
*/
public void resultReceived(IPendingServiceCall call) {
log.info(”Received result ” + call.getResult() + ” for ” +
call.getServiceMethodName());
}
public void appDisconnect(IConnection conne,IBroadcastStream
stream,IScope scope) {
log.info(”Application is disconnected “);
IClient client = conne.getClient();
if (client.equals(adminUserName)&& scope == conne.getScope()){
ISharedObject adminStatus_so = getSharedObject(scope,
“adminStatus”);
adminStatus_so.beginUpdate();
adminStatus_so.setAttribute(”adminIsIn”, false);
stream.close();
conne.close();
super.disconnect (conne,scope);
adminStatus_so.endUpdate();
}
else
{
ISharedObject userList_so = getSharedObject(scope, “userList”);
userList_so.beginUpdate();
removeFromList( conne.getClient().getId());
stream.close();
conne.getClient().disconnect();
super.disconnect(conne,scope);
userList_so.endUpdate();
}
}
}