@Override protected void onFullTextMessage(WebSocketChannel channel, BufferedTextMessage message) throws IOException { String payload = message.getData(); KMessage msg = KMessageLoader.load(payload); switch (msg.type()) { case KMessageLoader.GET_RES_TYPE: { GetResult getResult = (GetResult) msg; Object callbackRegistered = _callbacks.get(getResult.id); if (callbackRegistered != null) { ((KCallback) callbackRegistered).on(getResult.values); } else { System.err.println(); } _callbacks.remove(getResult.id); } break; case KMessageLoader.PUT_RES_TYPE: { PutResult putResult = (PutResult) msg; Object callbackRegistered = _callbacks.get(putResult.id); if (callbackRegistered != null) { ((KCallback) callbackRegistered).on(null); } else { System.err.println(); } } break; case KMessageLoader.ATOMIC_GET_INC_RESULT_TYPE: { AtomicGetIncrementResult atomicGetResult = (AtomicGetIncrementResult) msg; Object callbackRegistered = _callbacks.get(atomicGetResult.id); if (callbackRegistered != null) { ((KCallback) callbackRegistered).on(atomicGetResult.value); } else { System.err.println(); } } break; case KMessageLoader.EVENTS_TYPE: { Events eventsMessage = (Events) msg; this._manager.reload( eventsMessage.allKeys(), new KCallback<Throwable>() { @Override public void on(Throwable throwable) { WebSocketClient.this._localEventListeners.dispatch(eventsMessage); } }); } break; default: { System.err.println("MessageType not supported:" + msg.type()); } } }
@Override public void connect(KCallback<Throwable> callback) { // noop if (callback != null) { callback.on(null); } }
@Override public void get(long[] keys, KCallback<String[]> callback) { int nbKeys = keys.length / 3; String[] flatKeys = new String[nbKeys]; for (int i = 0; i < nbKeys; i++) { flatKeys[i] = KContentKey.toString(keys, i); } List<String> values = jedis.mget(flatKeys); if (callback != null) { callback.on(values.toArray(new String[values.size()])); } }
@Override public synchronized void put( long[] p_keys, String[] p_values, KCallback<Throwable> p_callback, int excludeListener) { int nbKeys = p_keys.length / 3; String[] elems = new String[nbKeys * 2]; for (int i = 0; i < nbKeys; i++) { elems[(i * 2)] = KContentKey.toString(p_keys, i); elems[(i * 2) + 1] = p_values[i]; } if (jedis != null) { jedis.mset(elems); } KMessage events = new Message(); events.setType(Message.EVENTS_TYPE); events.setKeys(p_keys); jedis.publish("kmf", events.save()); if (p_callback != null) { p_callback.on(null); } }
@Override public void atomicGetIncrement(long[] key, KCallback<Short> cb) { String result = jedis.get(KContentKey.toString(key, 0)); short nextV; short previousV; if (result != null) { try { previousV = Short.parseShort(result); } catch (Exception e) { e.printStackTrace(); previousV = Short.MIN_VALUE; } } else { previousV = 0; } if (previousV == Short.MAX_VALUE) { nextV = Short.MIN_VALUE; } else { nextV = (short) (previousV + 1); } // TODO use the nativeInc method jedis.set(key.toString(), "" + nextV); cb.on(previousV); }
@Override public void sendToPeer(String peer, KMessage message, KCallback<KMessage> callback) { if (callback != null) { callback.on(null); } }
@Override public void close(KCallback<Throwable> callback) { _client.close(); callback.on(null); }
@Override public void connect(KCallback<Throwable> callback) { _client.connect(this); _atomicInteger = new AtomicInteger(); callback.on(null); }