Skip to content

Instantly share code, notes, and snippets.

@frsyuki
Created May 6, 2011 04:27
Show Gist options
  • Save frsyuki/958445 to your computer and use it in GitHub Desktop.
Save frsyuki/958445 to your computer and use it in GitHub Desktop.
Dispatch sample of versioned RPC
package com.example.server;
import java.util.Map;
import java.util.HashMap;
import java.io.IOException;
import org.msgpack.MessageTypeException;
import org.msgpack.rpc.dispatcher.Dispatcher;
import org.msgpack.rpc.Request;
public class MyApp implements Dispatcher {
private KeyValueService_0 kv_0;
private CacheService_0 cache_0;
public KeyValueService_0 kv_0() {
return this.kv_0;
}
public CacheService_0 cache_0() {
return this.cache_0;
}
private class Default_KeyValueService_0_get implements KeyValueService_0.Iget {
public byte[] get(com.example.KeyValueService_0.Aget args) {
throw new RPCError(".NotImplemented"); // TODO
}
}
private class Default_KeyValueService_0_set implements KeyValueService_0.Iset {
public void set(com.example.KeyValueService_0.Aset args) {
throw new RPCError(".NotImplemented"); // TODO
}
}
private class Default_CacheService_0_get implements CacheService_0.Iget {
public byte[] get(com.example.CacheService_0.Aget args) {
throw new RPCError(".NotImplemented"); // TODO
}
}
private class Default_CacheService_0_set implements CacheService_0.Iset {
public void set(com.example.CacheService_0.Aset args) {
throw new RPCError(".NotImplemented"); // TODO
}
}
private HashMap<String, Dispatcher> scopeDispatchTable;
public MyApp() {
this.scopeDispatchTable = new HashMap<String, Dispatcher>();
this.scopeDispatchTable.put("kv:0", this.kv_0);
this.scopeDispatchTable.put("kv", this.kv_0);
this.scopeDispatchTable.put("cache:0", this.cache_0);
this.scopeDispatchTable.put("cache", this.cache_0);
this.scopeDispatchTable.put("", this.kv_0);
this.scopeDispatchTable.put("0", this.kv_0);
this.kv_0.setGet(new Default_KeyValueService_0_get());
this.kv_0.setSet(new Default_KeyValueService_0_set());
this.cache_0.setGet(new Default_CacheService_0_get());
this.cache_0.setSet(new Default_CacheService_0_set());
}
public void dispatch(Request request) throws Exception {
String methodName = request.getMethodName();
int ic = methodName.indexOf(':');
if(ic < 0) {
this.kv_0.dispatch(methodName, request);
return;
}
String scope = methodName.substring(ic+1);
methodName = methodName.substring(0, ic);
Dispatcher dp = this.scopeDispatchTable.get(scope);
if(dp == null) {
throw new MessageTypeException("FIXME");
}
dp.dispatch(methodName, request);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment