use of de.jackwhite20.japs.shared.net.OpCode in project JaPS by JackWhite20.
the class Connection method channelRead0.
@Override
protected void channelRead0(ChannelHandlerContext ctx, JSONObject jsonObject) throws Exception {
if (jsonObject.isNull("op")) {
return;
}
int op = ((Integer) jsonObject.remove("op"));
OpCode opCode = OpCode.of(op);
switch(opCode) {
case OP_BROADCAST:
if (!jsonObject.has("su")) {
// Broadcast it to all subscriber
server.broadcast(this, jsonObject.getString("ch"), jsonObject);
} else {
// Broadcast to specific subscriber
server.broadcastTo(this, jsonObject.getString("ch"), jsonObject, jsonObject.getString("su"));
}
break;
case OP_CACHE_GET:
String getKey = jsonObject.getString("key");
int getCallbackId = jsonObject.getInt("id");
Object getValue = server.cache().get(getKey);
JSONObject getResponse = new JSONObject().put("op", 5).put("id", getCallbackId).put("value", getValue);
send(getResponse);
LOGGER.log(Level.FINE, "[{0}] Got cache entry {1}={2} and a callback id of {3}", new Object[] { remoteAddress.toString(), getKey, getValue, getCallbackId });
break;
case OP_CACHE_ADD:
String key = jsonObject.getString("key");
Object value = jsonObject.get("value");
int expire = jsonObject.getInt("expire");
server.cache().put(key, value, expire);
server.clusterBroadcast(this, jsonObject.put("op", OpCode.OP_CACHE_ADD.getCode()));
LOGGER.log(Level.FINE, "[{0}] Added cache entry {1}={2} with an expire of {3}", new Object[] { remoteAddress.toString(), key, value, expire });
break;
case OP_CACHE_REMOVE:
String removeKey = jsonObject.getString("key");
server.cache().remove(removeKey);
server.clusterBroadcast(this, jsonObject.put("op", OpCode.OP_CACHE_REMOVE.getCode()));
LOGGER.log(Level.FINE, "[{0}] Removed cache entry with key {1}", new Object[] { remoteAddress.toString(), removeKey });
break;
case OP_CACHE_HAS:
boolean has = server.cache().has(jsonObject.getString("key"));
JSONObject hasResponse = new JSONObject().put("op", OpCode.OP_CACHE_HAS.getCode()).put("id", jsonObject.getInt("id")).put("has", has);
send(hasResponse);
break;
case OP_CACHE_SET_EXPIRE:
String expireKey = jsonObject.getString("key");
int expireSeconds = jsonObject.getInt("expire");
server.cache().expire(expireKey, expireSeconds);
server.clusterBroadcast(this, jsonObject.put("op", OpCode.OP_CACHE_SET_EXPIRE.getCode()));
LOGGER.log(Level.FINE, "[{0}] Set expire seconds for key {1} to {2} seconds", new Object[] { remoteAddress.toString(), expireKey, expireSeconds });
break;
case OP_CACHE_GET_EXPIRE:
String expireGetKey = jsonObject.getString("key");
int expireGetCallbackId = jsonObject.getInt("id");
int expireGetValue = ((int) server.cache().expire(expireGetKey));
JSONObject expireGetResponse = new JSONObject().put("op", 5).put("id", expireGetCallbackId).put("value", expireGetValue);
send(expireGetResponse);
LOGGER.log(Level.FINE, "[{0}] Got expire in time for key {1} which will expire in {2} seconds", new Object[] { remoteAddress.toString(), expireGetKey, expireGetValue });
break;
case OP_REGISTER_CHANNEL:
String channelToRegister = jsonObject.getString("ch");
server.subscribeChannel(channelToRegister, this);
channels.add(channelToRegister);
break;
case OP_UNREGISTER_CHANNEL:
String channelToRemove = jsonObject.getString("ch");
server.unsubscribeChannel(channelToRemove, this);
channels.remove(channelToRemove);
break;
case OP_SUBSCRIBER_SET_NAME:
name = jsonObject.getString("su");
LOGGER.log(Level.FINE, "[{0}] Subscriber name set to: {1}", new Object[] { remoteAddress.toString(), name });
break;
case OP_CLUSTER_INFO_SET:
host = jsonObject.getString("host");
port = jsonObject.getInt("port");
LOGGER.log(Level.FINE, "[{0}] Cluster info set to: {1}:{2}", new Object[] { remoteAddress.toString(), host, String.valueOf(port) });
break;
case OP_KEEP_ALIVE:
// LOGGER.log(Level.FINE, "[{0}] Keep alive time: {1}", new Object[]{remoteAddress.toString(), System.currentTimeMillis()});
break;
case OP_CACHE_SYNC_REQUEST:
LOGGER.log(Level.INFO, "Got cache sync request");
// We got the request to sync our cache to the one requesting it
send(server.cache().prepareCacheSync().put("op", OpCode.OP_CACHE_SYNC_RESPONSE.getCode()));
break;
case OP_UNKNOWN:
LOGGER.log(Level.WARNING, "[{0}] Unknown OP code received: {0}", new Object[] { remoteAddress.toString(), op });
break;
}
}
use of de.jackwhite20.japs.shared.net.OpCode in project JaPS by JackWhite20.
the class PubSubCacheImpl method received.
@SuppressWarnings("unchecked")
@Override
public void received(JSONObject jsonObject) {
Integer op = ((Integer) jsonObject.remove("op"));
OpCode opCode = OpCode.of(op);
switch(opCode) {
case OP_CACHE_GET:
int id = jsonObject.getInt("id");
Object value = (jsonObject.has("value")) ? jsonObject.get("value") : null;
try {
// Remove the consumer and accept it if it is not null
Consumer remove = callbacks.remove(id);
if (remove != null) {
remove.accept(value);
}
} catch (Exception e) {
e.printStackTrace();
}
break;
case OP_CACHE_HAS:
int hasId = jsonObject.getInt("id");
try {
// Remove the consumer and accept it if it is not null
Consumer remove = callbacks.remove(hasId);
if (remove != null) {
remove.accept(new JSONObject().put("has", jsonObject.getBoolean("has")));
}
} catch (Exception e) {
e.printStackTrace();
}
break;
}
}
Aggregations