- put all redis classes into Messaging Service

- wee need to be able to mock the messaging service to make it unit testable
This commit is contained in:
Richard Alam 2011-05-29 02:32:13 +00:00
parent ad9a4895b8
commit db6f7b49f3
6 changed files with 233 additions and 1 deletions

View File

@ -0,0 +1,8 @@
package org.bigbluebutton.api.messaging;
public interface MessageListener {
void meetingStarted(String meetingId);
void meetingEnded(String meetingId);
void userJoined(String meetingId, String userId, String name, String role);
void userLeft(String meetingId, String userId);
}

View File

@ -0,0 +1,13 @@
package org.bigbluebutton.api.messaging;
import java.util.Map;
public interface MessagingService {
void start();
void stop();
void recordMeetingInfo(String meetingId, Map<String, String> info);
void recordMeetingMetadata(String meetingId, Map<String, String> metadata);
void send(String channel, String message);
void addListener(MessageListener listener);
void removeListener(MessageListener listener);
}

View File

@ -0,0 +1,176 @@
package org.bigbluebutton.api.messaging;
import java.io.IOException;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPubSub;
public class RedisMessagingService implements MessagingService {
private Jedis jedis;
private final String host;
private final int port;
private final Set<MessageListener> listeners = new HashSet<MessageListener>();
private final Executor exec = Executors.newSingleThreadExecutor();
private Runnable pubsubListener;
private volatile boolean listen = false;
public RedisMessagingService(String host, int port) {
this.host = host;
this.port = port;
}
@Override
public void addListener(MessageListener listener) {
listeners.add(listener);
}
@Override
public void removeListener(MessageListener listener) {
listeners.remove(listener);
}
@Override
public void recordMeetingInfo(String meetingId, Map<String, String> info) {
// TODO Auto-generated method stub
}
@Override
public void recordMeetingMetadata(String meetingId,
Map<String, String> metadata) {
// TODO Auto-generated method stub
}
@Override
public void send(String channel, String message) {
// TODO Auto-generated method stub
}
@Override
public void start() {
jedis = new Jedis(host, port, 0);
if (jedis != null) {
// listen = true;
// pubsubListener = new Runnable() {
// public void run() {
jedis.psubscribe(new PubSubListener(), "bigbluebutton:conference:*");
// }
// };
// exec.execute(pubsubListener);
}
}
@Override
public void stop() {
try {
jedis.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
/*
* Pubsub channels:
* - bigbluebutton:conference:status --> value: "<confid>:started" or "<confid>:ended"
* - bigbluebutton:conference:join --> value: "<confid>:<userid>:<fullname>:<role>"
* - bigbluebutton:conference:remove --> value: "<confid>:<userid>"
*
*
**/
private class PubSubListener extends JedisPubSub {
private final String PATTERN_CONFERENCE="bigbluebutton:conference:*";
private final String CHANNEL_STATUS="bigbluebutton:conference:status";
private final String CHANNEL_JOIN="bigbluebutton:conference:join";
private final String CHANNEL_LEAVE="bigbluebutton:conference:remove";
private final String COLON=":";
public PubSubListener() {
super();
}
@Override
public void onMessage(String channel, String message) {
}
@Override
public void onPMessage(String pattern, String channel,
String message) {
System.out.println("redis message received. pattern:"+pattern+" channel:"+channel+" message:"+message);
if(pattern.equalsIgnoreCase(PATTERN_CONFERENCE)){
String[] args = message.split(COLON);
if(channel.equalsIgnoreCase(CHANNEL_STATUS)){
//params extract
String meetingId = args[0];
String status = args[1];
for (MessageListener listener : listeners) {
if(status.equalsIgnoreCase("started")) {
listener.meetingStarted(meetingId);
} else if(status.equalsIgnoreCase("ended")) {
listener.meetingStarted(meetingId);
}
}
} else if(channel.equalsIgnoreCase(CHANNEL_JOIN)){
//params extract
String meetingId = args[0];
String userId = args[1];
String name = args[2];
String role = args[3];
for (MessageListener listener : listeners) {
listener.userJoined(meetingId, userId, name, role);
}
} else if(channel.equalsIgnoreCase(CHANNEL_LEAVE)){
String meetingId = args[0];
String userId = args[1];
for (MessageListener listener : listeners) {
listener.userLeft(meetingId, userId);
}
}
}
}
@Override
public void onPSubscribe(String pattern, int subscribedChannels) {
// TODO Auto-generated method stub
}
@Override
public void onPUnsubscribe(String pattern, int subscribedChannels) {
// TODO Auto-generated method stub
}
@Override
public void onSubscribe(String channel, int subscribedChannels) {
// TODO Auto-generated method stub
}
@Override
public void onUnsubscribe(String channel, int subscribedChannels) {
// TODO Auto-generated method stub
}
}
}

View File

@ -5,6 +5,7 @@ import org.slf4j.LoggerFactory;
public class RedisListener extends RedisServer implements Runnable{
private static Logger log = LoggerFactory.getLogger(RedisListener.class);
private PubsubListener pubsubListener;
public RedisListener(String server, int port) {
@ -26,6 +27,7 @@ public class RedisListener extends RedisServer implements Runnable{
public PubsubListener getPubsubListener() {
return pubsubListener;
}
public void setPubsubListener(PubsubListener pubsubListener) {
System.out.println("setting pubsub");
this.pubsubListener = pubsubListener;

View File

@ -8,7 +8,7 @@ public class RedisServer {
protected Jedis jedis;
public RedisServer(String server, int port) {
jedis = new Jedis(server, port,0);
jedis = new Jedis(server, port, 0);
jedis.set("foo", "bar");
}

View File

@ -0,0 +1,33 @@
package org.bigbluebutton.web.controllers
import grails.test.*
import org.bigbluebutton.web.services.DynamicConferenceService;
class ApiControllerTests extends ControllerUnitTestCase {
protected void setUp() {
super.setUp()
}
protected void tearDown() {
super.tearDown()
}
void testIndex() {
ApiController controller = new ApiController()
def service = mockFor(DynamicConferenceService)
service.demand.apiVersion(1..1) { -> return 1 }
controller.setDynamicConferenceService(service.createMock())
controller.index()
println "controller response = " + controller.response.contentAsString
}
void testCreateAPI() {
ApiController controller = new ApiController()
def service = mockFor(DynamicConferenceService)
service.demand.apiVersion(1..1) { -> return 1 }
controller.setDynamicConferenceService(service.createMock())
controller.create()
println "controller response = " + controller.response.contentAsString
}
}