1package muduo.rpc; 2 3import java.util.Collections; 4import java.util.Map; 5import java.util.concurrent.ConcurrentHashMap; 6 7import org.jboss.netty.channel.Channel; 8 9import com.google.protobuf.Service; 10 11public abstract class RpcPeer { 12 13 protected NewChannelCallback newChannelCallback; 14 protected Map<String, Service> services = new ConcurrentHashMap<String, Service>(); 15 16 public void registerService(Service service) { 17 services.put(service.getDescriptorForType().getFullName(), service); 18 } 19 20 public void setNewChannelCallback(NewChannelCallback newChannelCallback) { 21 this.newChannelCallback = newChannelCallback; 22 } 23 24 protected void setupNewChannel(RpcChannel rpcChannel) { 25 Channel channel = rpcChannel.getChannel(); 26 RpcMessageHandler handler = (RpcMessageHandler) channel.getPipeline().get("handler"); 27 handler.setChannel(rpcChannel); 28 rpcChannel.setServiceMap(Collections.unmodifiableMap(services)); 29 if (newChannelCallback != null) { 30 newChannelCallback.run(rpcChannel); 31 } 32 } 33 34 public abstract void channelConnected(Channel channel); 35} 36