Created
May 26, 2013 08:44
-
-
Save sebastienblanc/5652114 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.jboss.aerogear.simplepush.protocol.MessageType | |
import org.jboss.aerogear.simplepush.protocol.impl.json.JsonUtil | |
import org.jboss.aerogear.simplepush.protocol.impl.HandshakeMessageImpl | |
import org.jboss.aerogear.simplepush.protocol.HandshakeResponse | |
import org.jboss.aerogear.simplepush.server.SimplePushServer | |
import org.jboss.aerogear.simplepush.server.DefaultSimplePushServer | |
import org.jboss.aerogear.simplepush.protocol.RegisterResponse | |
import org.jboss.aerogear.simplepush.protocol.impl.RegisterImpl | |
import org.jboss.aerogear.simplepush.server.datastore.InMemoryDataStore | |
import org.jboss.aerogear.simplepush.protocol.NotificationMessage | |
import static org.jboss.aerogear.simplepush.protocol.impl.json.JsonUtil.toJson | |
def currentWS | |
def uaid | |
SimplePushServer simplePushServer = new DefaultSimplePushServer(new InMemoryDataStore()) | |
def userAgents = [:] | |
boolean checkHandshakeCompleted(final UUID uaid) { | |
if (uaid == null) { | |
return false; | |
} | |
/* if (!userAgents.containsKey(uaid)) { | |
logger.debug("UserAgent ["+ uaid + "] was cleaned up due to unactivity for " + config.reaperTimeout() + "ms"); | |
this.uaid = null; | |
return false; | |
}*/ | |
return true; | |
} | |
vertx.createHttpServer().websocketHandler { ws -> | |
ws.dataHandler { data -> | |
currentWS = ws | |
MessageType messageType = JsonUtil.parseFrame(data.toString()) | |
switch (messageType.getMessageType()) { | |
case MessageType.Type.HELLO: | |
if (!checkHandshakeCompleted(uaid)) { | |
HandshakeResponse response = simplePushServer.handleHandshake(JsonUtil.fromJson(data.toString(), HandshakeMessageImpl.class)) | |
ws.writeTextFrame(JsonUtil.toJson(response)) | |
uaid = response.getUAID(); | |
println "storing ${uaid}" | |
userAgents[uaid] = ws | |
println("UserAgent [" + uaid + "] handshake done"); | |
} | |
/* userAgents.put(uaid, new UserAgent(uaid, ctx, System.currentTimeMillis())); | |
processUnacked(uaid, ctx, 0); | |
logger.info("UserAgent [" + uaid + "] handshake done"); | |
} */ | |
break; | |
case MessageType.Type.REGISTER: | |
if (checkHandshakeCompleted(uaid)) { | |
final RegisterResponse response = simplePushServer.handleRegister(JsonUtil.fromJson(data.toString(), RegisterImpl.class), uaid) | |
ws.writeTextFrame(JsonUtil.toJson(response)) | |
println("UserAgent [" + uaid + "] Registered[" + response.getChannelId() + "]"); | |
} | |
break; | |
/* | |
case UNREGISTER: | |
if (checkHandshakeCompleted(uaid)) { | |
final UnregisterMessage unregister = fromJson(frame.text(), UnregisterMessageImpl.class); | |
final UnregisterResponse response = simplePushServer.handleUnregister(unregister, uaid); | |
writeJsonResponse(toJson(response), ctx.channel()); | |
logger.info("UserAgent [" + uaid + "] Unregistered[" + response.getChannelId() + "]"); | |
} | |
break; | |
case ACK: | |
if (checkHandshakeCompleted(uaid)) { | |
final AckMessage ack = fromJson(frame.text(), AckMessageImpl.class); | |
simplePushServer.handleAcknowledgement(ack, uaid); | |
processUnacked(uaid, ctx, config.ackInterval()); | |
} | |
break;*/ | |
} | |
//ws.writeTextFrame("popopo") | |
} | |
}.requestHandler { req -> | |
req.bodyHandler { body -> | |
final String channelId = req.uri.substring(req.uri.lastIndexOf('/') + 1); | |
println "in put channel id is ${channelId}" | |
final UUID myuaid = simplePushServer.fromChannel(channelId); | |
final String payload = body | |
println "${body}" | |
final NotificationMessage notification = simplePushServer.handleNotification(channelId, uaid, payload); | |
userAgents.get(myuaid).writeTextFrame(JsonUtil.toJson(notification)); | |
} | |
req.response.with { | |
statusCode = 200 | |
end() | |
} | |
}.listen(7777) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment