From 690d63f8483bad179adacb9b73042666462a6f54 Mon Sep 17 00:00:00 2001
From: Paul-Christian Volkmer <paul-christian.volkmer@mni.thm.de>
Date: Mon, 10 Jun 2013 15:31:08 +0200
Subject: [PATCH] Use event publisher to send websocket events

With this patch, websocket feedback data will be send without using
UserService arrays within ARSnovaSocketIOServer except
reportDeletedFeedback().
---
 .../thm/arsnova/services/FeedbackService.java |  2 +
 .../arsnova/socket/ARSnovaSocketIOServer.java | 52 +++++++++----------
 2 files changed, 27 insertions(+), 27 deletions(-)

diff --git a/src/main/java/de/thm/arsnova/services/FeedbackService.java b/src/main/java/de/thm/arsnova/services/FeedbackService.java
index 3523b64c..0a808082 100644
--- a/src/main/java/de/thm/arsnova/services/FeedbackService.java
+++ b/src/main/java/de/thm/arsnova/services/FeedbackService.java
@@ -33,6 +33,8 @@ import org.springframework.stereotype.Service;
 import de.thm.arsnova.dao.IDatabaseDao;
 import de.thm.arsnova.entities.Feedback;
 import de.thm.arsnova.entities.User;
+import de.thm.arsnova.events.ARSnovaEvent;
+import de.thm.arsnova.events.Publisher;
 import de.thm.arsnova.exceptions.NoContentException;
 import de.thm.arsnova.socket.ARSnovaSocketIOServer;
 
diff --git a/src/main/java/de/thm/arsnova/socket/ARSnovaSocketIOServer.java b/src/main/java/de/thm/arsnova/socket/ARSnovaSocketIOServer.java
index d4132f35..f3bc164a 100644
--- a/src/main/java/de/thm/arsnova/socket/ARSnovaSocketIOServer.java
+++ b/src/main/java/de/thm/arsnova/socket/ARSnovaSocketIOServer.java
@@ -24,6 +24,7 @@ import com.corundumstudio.socketio.listener.DisconnectListener;
 
 import de.thm.arsnova.entities.User;
 import de.thm.arsnova.events.ARSnovaEvent;
+import de.thm.arsnova.events.Publisher;
 import de.thm.arsnova.exceptions.NoContentException;
 import de.thm.arsnova.services.IFeedbackService;
 import de.thm.arsnova.services.IQuestionService;
@@ -46,6 +47,9 @@ public class ARSnovaSocketIOServer {
 	@Autowired
 	private ISessionService sessionService;
 
+	@Autowired
+	private Publisher publisher;
+
 	private static final Logger LOGGER = LoggerFactory.getLogger(ARSnovaSocketIOServer.class);
 
 	private int portNumber;
@@ -231,12 +235,15 @@ public class ARSnovaSocketIOServer {
 
 	public void reportUpdatedFeedbackForSession(String sessionKey) {
 		de.thm.arsnova.entities.Feedback fb = feedbackService.getFeedback(sessionKey);
-		broadcastInSession(sessionKey, "feedbackData", fb.getValues());
+		publisher.publish(new ARSnovaEvent(this, sessionKey, "feedbackData", fb.getValues()));
+		//broadcastInSession(sessionKey, "feedbackData", fb.getValues());
 		try {
 			long averageFeedback = feedbackService.getAverageFeedbackRounded(sessionKey);
-			broadcastInSession(sessionKey, "feedbackDataRoundedAverage", averageFeedback);
+			publisher.publish(new ARSnovaEvent(this, sessionKey, "feedbackDataRoundedAverage", averageFeedback));
+			//broadcastInSession(sessionKey, "feedbackDataRoundedAverage", averageFeedback);
 		} catch (NoContentException e) {
-			broadcastInSession(sessionKey, "feedbackDataRoundedAverage", null);
+			publisher.publish(new ARSnovaEvent(this, sessionKey, "feedbackDataRoundedAverage", null));
+			//broadcastInSession(sessionKey, "feedbackDataRoundedAverage", null);
 		}
 	}
 
@@ -253,12 +260,15 @@ public class ARSnovaSocketIOServer {
 			return;
 		}
 
-		for (SocketIOClient client : server.getAllClients()) {
+		publisher.publish(new ARSnovaEvent(this, user, "feedbackData", fb.getValues()));
+		publisher.publish(new ARSnovaEvent(this, user, "feedbackDataRoundedAverage", averageFeedback));
+		
+		/*for (SocketIOClient client : server.getAllClients()) {
 			if (connectionIds.contains(client.getSessionId())) {
 				client.sendEvent("feedbackData", fb.getValues());
 				client.sendEvent("feedbackDataRoundedAverage", averageFeedback);
 			}
-		}
+		}*/
 	}
 
 	public void reportActiveUserCountForSession(String sessionKey) {
@@ -268,22 +278,26 @@ public class ARSnovaSocketIOServer {
 			return;
 		}
 		lastActiveUserCount = count;
-
-		broadcastInSession(sessionKey, "activeUserCountData", count);
+		
+		publisher.publish(new ARSnovaEvent(this, sessionKey, "activeUserCountData", count));
+		//broadcastInSession(sessionKey, "activeUserCountData", count);
 	}
 
 	public void reportAnswersToLecturerQuestionAvailable(String sessionKey, String lecturerQuestionId) {
-		broadcastInSession(sessionKey, "answersToLecQuestionAvail", lecturerQuestionId);
+		publisher.publish(new ARSnovaEvent(this, sessionKey, "answersToLecQuestionAvail", lecturerQuestionId));
+		//broadcastInSession(sessionKey, "answersToLecQuestionAvail", lecturerQuestionId);
 	}
 
 	public void reportAudienceQuestionAvailable(String sessionKey, String audienceQuestionId) {
 		/* TODO role handling implementation, send this only to users with role lecturer */
-		broadcastInSession(sessionKey, "audQuestionAvail", audienceQuestionId);
+		publisher.publish(new ARSnovaEvent(this, sessionKey, "audQuestionAvail", audienceQuestionId));
+		//broadcastInSession(sessionKey, "audQuestionAvail", audienceQuestionId);
 	}
 
 	public void reportLecturerQuestionAvailable(String sessionKey, String lecturerQuestionId) {
 		/* TODO role handling implementation, send this only to users with role audience */
-		broadcastInSession(sessionKey, "lecQuestionAvail", lecturerQuestionId);
+		publisher.publish(new ARSnovaEvent(this, sessionKey, "lecQuestionAvail", lecturerQuestionId));
+		//broadcastInSession(sessionKey, "lecQuestionAvail", lecturerQuestionId);
 	}
 
 	/** Sends event to a websocket connection identified by UUID 
@@ -294,25 +308,9 @@ public class ARSnovaSocketIOServer {
 	public void sendToClient(UUID sessionId, ARSnovaEvent event) {
 		for (SocketIOClient c : server.getAllClients()) {
 			if (c.getSessionId().equals(sessionId)) {
-				System.out.println(sessionId);
+				c.sendEvent(event.getEventName(), event.getData());
 				break;
 			}
 		}
 	}
-
-	public void broadcastInSession(String sessionKey, String eventName, Object data) {
-		/**
-		 * collect a list of users which are in the current session iterate over
-		 * all connected clients and if send feedback, if user is in current
-		 * session
-		 */
-		Set<User> users = userService.getUsersInSession(sessionKey);
-
-		for (SocketIOClient c : server.getAllClients()) {
-			User u = userService.getUser2SocketId(c.getSessionId());
-			if (u != null && users.contains(u)) {
-				c.sendEvent(eventName, data);
-			}
-		}
-	}
 }
-- 
GitLab