Skip to content

Instantly share code, notes, and snippets.

@awartoft
Created July 1, 2013 13:55
Show Gist options
  • Save awartoft/5900980 to your computer and use it in GitHub Desktop.
Save awartoft/5900980 to your computer and use it in GitHub Desktop.
package com.cyant.chat;
import com.cyant.chat.event.operator.AddCustomerEvent;
import com.cyant.chat.user.CustomerUser;
import com.cyant.chat.user.OperatorUser;
import org.atmosphere.cpr.*;
import java.util.ArrayList;
import java.util.Collection;
public class Organisation
{
// metadata
private int appId;
private String appSecret;
// collection of users
private Collection<CustomerUser> customers = new ArrayList<CustomerUser>();
private Collection<OperatorUser> operators = new ArrayList<OperatorUser>();
// broadcaster utility
private Broadcaster operatorBroadcaster;
public Organisation(int appId, String appSecret)
{
this.appId = appId;
this.appSecret = appSecret;
operatorBroadcaster = BroadcasterFactory.getDefault().get(String.format("/organisation/%d/operators", appId));
operatorBroadcaster.addBroadcasterListener(new OperatorListener());
}
public int getAppId()
{
return appId;
}
public void setAppId(int appId)
{
this.appId = appId;
}
public String getAppSecret()
{
return appSecret;
}
public void setAppSecret(String appSecret)
{
this.appSecret = appSecret;
}
public void addOperator(OperatorUser operator)
{
operators.add(operator);
operatorBroadcaster.addAtmosphereResource(operator.getResource());
}
public void removeOperator(OperatorUser operator)
{
operators.remove(operator);
operatorBroadcaster.removeAtmosphereResource(operator.getResource());
}
public void addCustomer(CustomerUser user)
{
// add the customer
customers.add(user);
// notify all operators
for (OperatorUser o : operators) {
o.getResource().write(new AddCustomerEvent(user).toString());
}
}
public void removeCustomer(CustomerUser user)
{
customers.remove(user);
}
/**
* Listen to the events of
*/
private class OperatorListener extends BroadcasterListenerAdapter
{
@Override
public void onAddAtmosphereResource(Broadcaster b, AtmosphereResource r)
{
super.onAddAtmosphereResource(b, r);
for (CustomerUser u : customers) {
r.write(new AddCustomerEvent(u).toString());
}
}
@Override
public void onRemoveAtmosphereResource(Broadcaster b, AtmosphereResource r)
{
super.onRemoveAtmosphereResource(b, r);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment