Permalink
Browse files

API change in AtmosphereGwtHandler

the getBroadcaster function that is needed to make the server broadcast event possible now gets a GwtAtmosphereResource as an argument so you are more flexible in determining the broadcaster for your application.
  • Loading branch information...
1 parent 54f6661 commit c7e145d7c0c029758a8f62f07f1681d03feaa05e pierreh committed Feb 11, 2011
View
1 .gitignore
@@ -16,3 +16,4 @@ nbproject
target
test-output
nbactions.xml
+samples/gwt-demo/src/main/webapp/WEB-INF/classes/
View
64 ...t/atmosphere-gwt-server/src/main/java/org/atmosphere/gwt/server/AtmosphereGwtHandler.java
@@ -92,15 +92,13 @@ public SerializationPolicy getSerializationPolicy(String moduleBaseURL, String s
};
public int doComet(GwtAtmosphereResource resource) throws ServletException, IOException {
- Broadcaster broadcaster = getBroadcaster();
- if (broadcaster == null) {
- try {
- broadcaster = BroadcasterFactory.getDefault().get(DefaultBroadcaster.class, GWT_BROADCASTER_ID);
- } catch (IllegalAccessException ex) {
- logger.error("Failed to get broadcaster", ex);
- } catch (InstantiationException ex) {
- logger.error("Failed to get broadcaster", ex);
- }
+ Broadcaster broadcaster = BroadcasterFactory.getDefault().lookup(Broadcaster.class, GWT_BROADCASTER_ID);
+ try {
+ broadcaster = BroadcasterFactory.getDefault().get(DefaultBroadcaster.class, GWT_BROADCASTER_ID);
+ } catch (IllegalAccessException ex) {
+ logger.error("Failed to get broadcaster", ex);
+ } catch (InstantiationException ex) {
+ logger.error("Failed to get broadcaster", ex);
}
resource.getAtmosphereResource().setBroadcaster(broadcaster);
return NO_TIMEOUT;
@@ -123,8 +121,12 @@ public void doPost(List<Serializable> messages, GwtAtmosphereResource r) {
}
}
- protected Broadcaster getBroadcaster() {
- return BroadcasterFactory.getDefault().lookup(Broadcaster.class, GWT_BROADCASTER_ID);
+ protected Broadcaster getBroadcaster(GwtAtmosphereResource resource) {
+ if (resource == null) {
+ return BroadcasterFactory.getDefault().lookup(Broadcaster.class, GWT_BROADCASTER_ID);
+ } else {
+ return resource.getBroadcaster();
+ }
}
/**
@@ -223,8 +225,14 @@ public void onRequest(AtmosphereResource<HttpServletRequest, HttpServletResponse
}
}
+ /// --- server message handlers
+
protected void doServerMessage(BufferedReader data, int connectionID) {
List<Serializable> postMessages = new ArrayList<Serializable>();
+ GwtAtmosphereResource resource = lookupResource(connectionID);
+ if (resource == null) {
+ return;
+ }
try {
while (true) {
String event = data.readLine();
@@ -247,7 +255,7 @@ protected void doServerMessage(BufferedReader data, int connectionID) {
}
} else if (messageData.charAt(0) == 'b') {
Serializable message = deserialize(messageData.substring(1));
- broadcast(message);
+ broadcast(message, resource);
}
} else if (event.equals("s")) {
@@ -257,13 +265,13 @@ protected void doServerMessage(BufferedReader data, int connectionID) {
postMessages.add(message);
} else if (messageData.charAt(0) == 'b') {
Serializable message = messageData.substring(1);
- broadcast(message);
+ broadcast(message, resource);
}
} else if (event.equals("c")) {
if (messageData.equals("d")) {
- disconnect(connectionID);
+ disconnect(resource);
}
}
}
@@ -272,7 +280,7 @@ protected void doServerMessage(BufferedReader data, int connectionID) {
}
if (postMessages.size() > 0) {
- post(postMessages, connectionID);
+ post(postMessages, resource);
}
}
// protected void writePostResponse(HttpServletRequest request,
@@ -302,37 +310,37 @@ protected Serializable deserialize(String data) {
// return streamWriter.toString();
// }
- public void post(List<Serializable> messages, int connectionID) {
+ final public void post(List<Serializable> messages, GwtAtmosphereResource resource) {
if (messages == null) {
return;
}
- GwtAtmosphereResource r = lookupResource(connectionID);
- if (r != null) {
- doPost(messages, r);
+ if (resource != null) {
+ doPost(messages, resource);
}
}
- public void broadcast(Serializable message) {
+ public void broadcast(Serializable message, GwtAtmosphereResource resource) {
if (message == null) {
return;
}
- getBroadcaster().broadcast(message);
+ getBroadcaster(resource).broadcast(message);
}
- public void broadcast(List<Serializable> messages) {
+ public void broadcast(List<Serializable> messages, GwtAtmosphereResource resource) {
if (messages == null) {
return;
}
- getBroadcaster().broadcast(messages);
+ getBroadcaster(resource).broadcast(messages);
}
- public void disconnect(int connectionID) {
- GwtAtmosphereResource r = lookupResource(connectionID);
- if (r != null) {
- logger.debug("Resuming connection["+connectionID+"] after client disconnect message");
- r.getAtmosphereResource().resume();
+ public void disconnect(GwtAtmosphereResource resource) {
+ if (resource != null) {
+ logger.debug("Resuming connection["+resource.getConnectionID()+"] after client disconnect message");
+ resource.getAtmosphereResource().resume();
}
}
+
+ /// --- end server message handlers
/**
* Execute a task in a seperate thread, the thread pool will grow and shrink depending on demand
View
9 samples/gwt-demo/src/main/java/org/atmosphere/samples/server/AtmosphereHandler.java
@@ -43,8 +43,6 @@
import java.util.logging.Logger;
import javax.servlet.ServletConfig;
import javax.servlet.http.HttpSession;
-import org.atmosphere.cpr.Broadcaster;
-import org.atmosphere.cpr.BroadcasterFactory;
import org.atmosphere.gwt.server.GwtAtmosphereResource;
import org.atmosphere.gwt.server.AtmosphereGwtHandler;
@@ -63,11 +61,6 @@ public void init(ServletConfig servletConfig) throws ServletException {
}
@Override
- protected Broadcaster getBroadcaster() {
- return BroadcasterFactory.getDefault().lookup(Broadcaster.class, "GWT_COMET");
- }
-
- @Override
public int doComet(GwtAtmosphereResource resource) throws ServletException, IOException {
resource.getBroadcaster().setID("GWT_COMET");
HttpSession session = resource.getAtmosphereResource().getRequest().getSession(false);
@@ -81,7 +74,7 @@ public int doComet(GwtAtmosphereResource resource) throws ServletException, IOEx
logger.debug("Url: " + resource.getAtmosphereResource().getRequest().getRequestURL()
+ "?" + resource.getAtmosphereResource().getRequest().getQueryString());
}
- return super.doComet(resource);
+ return NO_TIMEOUT;
}
@Override

0 comments on commit c7e145d

Please sign in to comment.