RpcPeer.java revision 420c9859
1420c9859SShuo Chenpackage muduo.rpc;
2420c9859SShuo Chen
3420c9859SShuo Chenimport java.util.Collections;
4420c9859SShuo Chenimport java.util.Map;
5420c9859SShuo Chenimport java.util.concurrent.ConcurrentHashMap;
6420c9859SShuo Chen
7420c9859SShuo Chenimport org.jboss.netty.channel.Channel;
8420c9859SShuo Chen
9420c9859SShuo Chenimport com.google.protobuf.Service;
10420c9859SShuo Chen
11420c9859SShuo Chenpublic abstract class RpcPeer {
12420c9859SShuo Chen
13420c9859SShuo Chen    protected NewChannelCallback newChannelCallback;
14420c9859SShuo Chen    protected Map<String, Service> services = new ConcurrentHashMap<String, Service>();
15420c9859SShuo Chen
16420c9859SShuo Chen    public void registerService(Service service) {
17420c9859SShuo Chen        services.put(service.getDescriptorForType().getFullName(), service);
18420c9859SShuo Chen    }
19420c9859SShuo Chen
20420c9859SShuo Chen    public void setNewChannelCallback(NewChannelCallback newChannelCallback) {
21420c9859SShuo Chen        this.newChannelCallback = newChannelCallback;
22420c9859SShuo Chen    }
23420c9859SShuo Chen
24420c9859SShuo Chen    protected void setupNewChannel(RpcChannel rpcChannel) {
25420c9859SShuo Chen        Channel channel = rpcChannel.getChannel();
26420c9859SShuo Chen        RpcMessageHandler handler = (RpcMessageHandler) channel.getPipeline().get("handler");
27420c9859SShuo Chen        handler.setChannel(rpcChannel);
28420c9859SShuo Chen        rpcChannel.setServiceMap(Collections.unmodifiableMap(services));
29420c9859SShuo Chen        if (newChannelCallback != null) {
30420c9859SShuo Chen            newChannelCallback.run(rpcChannel);
31420c9859SShuo Chen        }
32420c9859SShuo Chen    }
33420c9859SShuo Chen
34420c9859SShuo Chen    public abstract void channelConnected(Channel channel);
35420c9859SShuo Chen}
36