PageRenderTime 32ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/src/org/gwt_websocketrpc/client/WebSocketController.java

http://gwt-websocketrpc.googlecode.com/
Java | 133 lines | 98 code | 27 blank | 8 comment | 12 complexity | 926dfb141bbea2e6c9f542c4ca02d11b MD5 | raw file
  1. package org.gwt_websocketrpc.client;
  2. import java.util.HashMap;
  3. import java.util.HashSet;
  4. import org.gwt_websocketrpc.client.websocket.WebSocketImpl;
  5. import org.gwt_websocketrpc.client.websocket.WebSocket.ReadyState;
  6. import org.gwt_websocketrpc.client.websocket.WebSocket.StringOutbound;
  7. import org.gwt_websocketrpc.client.websocket.WebSocket.StringSocketHandler;
  8. import org.gwt_websocketrpc.shared.WsRpcConstants;
  9. import com.google.gwt.core.client.GWT;
  10. import com.google.gwt.event.shared.HandlerRegistration;
  11. import com.google.gwt.http.client.Header;
  12. import com.google.gwt.http.client.RequestCallback;
  13. import com.google.gwt.http.client.Response;
  14. public class WebSocketController {
  15. private static final HashMap<String, WebSocketController> urlWsMap = new HashMap<String,WebSocketController>();
  16. public static WebSocketController createOrGetWebSocketController(String url){
  17. WebSocketController ws = urlWsMap.get(url);
  18. if(ws == null || !ws.isOpen()){
  19. ws = new WebSocketController(url);
  20. urlWsMap.put(url, ws);
  21. }
  22. return ws;
  23. }
  24. private final HashMap<Integer,RequestCallback> reqCallbackMap = new HashMap<Integer,RequestCallback>();
  25. private final HashSet<Integer> handlerRequests = new HashSet<Integer>();
  26. private final WebSocketImpl ws;
  27. private StringOutbound so =null;
  28. private String bufData = null;
  29. public WebSocketController(String url) {
  30. this.ws = new WebSocketImpl(url);
  31. ws.attachHandler(socketHandler);
  32. }
  33. protected boolean isOpen(){
  34. return ws.getReadyState() == ReadyState.Open;
  35. }
  36. private final StringSocketHandler socketHandler = new StringSocketHandler(){
  37. public void onConnect(StringOutbound out) {
  38. so = out;
  39. // Send initialization data:
  40. // 1) Strong Name
  41. // 2) Module Base Path
  42. so.send(GWT.getPermutationStrongName() + "!" + GWT.getModuleBaseURL());
  43. // Send any buffered requests
  44. if(bufData != null){
  45. so.send(bufData);
  46. bufData = null;
  47. }
  48. }
  49. public void onDisconnect() {
  50. so = null;
  51. urlWsMap.remove(ws.getURL());
  52. }
  53. public void onMessage(final String message) {
  54. // Parse Request ID
  55. int controlCharId = message.indexOf(WsRpcConstants.WsRpcControlString);
  56. int rid = Integer.parseInt(
  57. message.substring(
  58. 0,
  59. controlCharId),
  60. 16);
  61. final String actualMessage = message.substring(controlCharId+1);
  62. RequestCallback callback = reqCallbackMap.get(rid);
  63. if(callback != null){
  64. // If this is a response to a regular request (not a handler),
  65. // then remove callback from the callback map.
  66. if(!handlerRequests.contains(rid))
  67. reqCallbackMap.remove(rid);
  68. callback.onResponseReceived(null, new Response(){
  69. public String getHeader(String header) {return null;}
  70. public Header[] getHeaders() {return null;}
  71. public String getHeadersAsString() {return null;}
  72. public int getStatusCode() {
  73. return Response.SC_OK;
  74. }
  75. public String getStatusText() {return null;}
  76. public String getText() {return actualMessage;}
  77. });
  78. }else{
  79. GWT.log("[WebSocketRPC WebSocketController.onMessage] Request id="+rid, new Throwable());
  80. }
  81. }
  82. };
  83. public class RequestCallbackRegistration implements HandlerRegistration {
  84. private final int reqid;
  85. RequestCallbackRegistration(int reqid) {
  86. this.reqid = reqid;
  87. }
  88. public void removeHandler() {
  89. reqCallbackMap.remove(reqid);
  90. handlerRequests.remove(reqid);
  91. }
  92. public boolean isPending(){
  93. return reqCallbackMap.containsKey(reqid);
  94. }
  95. }
  96. public RequestCallbackRegistration sendRequest(boolean isHandler, final int reqid, String s, RequestCallback cb){
  97. reqCallbackMap.put(reqid, cb);
  98. if(isHandler)
  99. handlerRequests.add(reqid);
  100. // Add reqid;
  101. final String data = Integer.toHexString(reqid)+WsRpcConstants.WsRpcControlString+s;
  102. if(so!=null) so.send(data);
  103. else bufData = data;
  104. return new RequestCallbackRegistration(reqid);
  105. }
  106. }