Yuta HIGUCHI

renamed Serializer -> (onlab.netty-layer) PayloadSerializer

- Added TODO memos to ClusterCommunicationService layer

Change-Id: I4c81a72d03cddd23637f9c6cbf102125ea448c01
...@@ -12,6 +12,7 @@ public class ClusterMessage { ...@@ -12,6 +12,7 @@ public class ClusterMessage {
12 private final NodeId sender; 12 private final NodeId sender;
13 private final MessageSubject subject; 13 private final MessageSubject subject;
14 private final Object payload; 14 private final Object payload;
15 + // TODO: add field specifying Serializer for payload
15 16
16 /** 17 /**
17 * Creates a cluster message. 18 * Creates a cluster message.
......
...@@ -12,8 +12,6 @@ import java.util.TimerTask; ...@@ -12,8 +12,6 @@ import java.util.TimerTask;
12 import org.apache.felix.scr.annotations.Activate; 12 import org.apache.felix.scr.annotations.Activate;
13 import org.apache.felix.scr.annotations.Component; 13 import org.apache.felix.scr.annotations.Component;
14 import org.apache.felix.scr.annotations.Deactivate; 14 import org.apache.felix.scr.annotations.Deactivate;
15 -import org.apache.felix.scr.annotations.Reference;
16 -import org.apache.felix.scr.annotations.ReferenceCardinality;
17 import org.apache.felix.scr.annotations.Service; 15 import org.apache.felix.scr.annotations.Service;
18 import org.onlab.onos.cluster.ControllerNode; 16 import org.onlab.onos.cluster.ControllerNode;
19 import org.onlab.onos.cluster.NodeId; 17 import org.onlab.onos.cluster.NodeId;
...@@ -46,16 +44,23 @@ public class ClusterCommunicationManager ...@@ -46,16 +44,23 @@ public class ClusterCommunicationManager
46 private final Timer timer = new Timer("onos-controller-heatbeats"); 44 private final Timer timer = new Timer("onos-controller-heatbeats");
47 public static final long HEART_BEAT_INTERVAL_MILLIS = 1000L; 45 public static final long HEART_BEAT_INTERVAL_MILLIS = 1000L;
48 46
49 - @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) 47 + // TODO: This probably should not be a OSGi service.
48 + //@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
50 private MessagingService messagingService; 49 private MessagingService messagingService;
51 50
52 @Activate 51 @Activate
53 public void activate() { 52 public void activate() {
53 + // TODO: initialize messagingService
54 + // TODO: setPayloadSerializer, which is capable of
55 + // (1) serialize ClusterMessage - ClusterMessage.payload
56 + // (2) serialize ClusterMessage.payload using user specified serializer
57 +// messagingService.setPayloadSerializer(...);
54 log.info("Started"); 58 log.info("Started");
55 } 59 }
56 60
57 @Deactivate 61 @Deactivate
58 public void deactivate() { 62 public void deactivate() {
63 + // TODO: cleanup messageingService if needed.
59 log.info("Stopped"); 64 log.info("Stopped");
60 } 65 }
61 66
......
1 +package org.onlab.onos.store.serializers;
2 +
3 +import org.onlab.onos.cluster.NodeId;
4 +import org.onlab.onos.store.cluster.messaging.ClusterMessage;
5 +import org.onlab.onos.store.cluster.messaging.MessageSubject;
6 +
7 +import com.esotericsoftware.kryo.Kryo;
8 +import com.esotericsoftware.kryo.Serializer;
9 +import com.esotericsoftware.kryo.io.Input;
10 +import com.esotericsoftware.kryo.io.Output;
11 +
12 +public final class ClusterMessageSerializer extends Serializer<ClusterMessage> {
13 +
14 + public ClusterMessageSerializer() {
15 + // does not accept null
16 + super(false);
17 + }
18 +
19 + @Override
20 + public void write(Kryo kryo, Output output, ClusterMessage object) {
21 + kryo.writeClassAndObject(output, object.sender());
22 + kryo.writeClassAndObject(output, object.subject());
23 + // TODO: write bytes serialized using ClusterMessage specified serializer
24 + // write serialized payload size
25 + //output.writeInt(...);
26 + // write serialized payload
27 + //output.writeBytes(...);
28 + }
29 +
30 + @Override
31 + public ClusterMessage read(Kryo kryo, Input input,
32 + Class<ClusterMessage> type) {
33 + // TODO Auto-generated method stub
34 + NodeId sender = (NodeId) kryo.readClassAndObject(input);
35 + MessageSubject subject = (MessageSubject) kryo.readClassAndObject(input);
36 + int size = input.readInt();
37 + byte[] payloadBytes = input.readBytes(size);
38 + // TODO: deserialize payload using ClusterMessage specified serializer
39 + Object payload = null;
40 + return new ClusterMessage(sender, subject, payload);
41 + }
42 +
43 +}
...@@ -9,7 +9,7 @@ import java.util.HashMap; ...@@ -9,7 +9,7 @@ import java.util.HashMap;
9 /** 9 /**
10 * Kryo Serializer. 10 * Kryo Serializer.
11 */ 11 */
12 -public class KryoSerializer implements Serializer { 12 +public class KryoSerializer implements PayloadSerializer {
13 13
14 private KryoPool serializerPool; 14 private KryoPool serializerPool;
15 15
......
...@@ -14,14 +14,14 @@ import java.util.List; ...@@ -14,14 +14,14 @@ import java.util.List;
14 public class MessageDecoder extends ReplayingDecoder<DecoderState> { 14 public class MessageDecoder extends ReplayingDecoder<DecoderState> {
15 15
16 private final NettyMessagingService messagingService; 16 private final NettyMessagingService messagingService;
17 - private final Serializer serializer; 17 + private final PayloadSerializer payloadSerializer;
18 18
19 private int contentLength; 19 private int contentLength;
20 20
21 - public MessageDecoder(NettyMessagingService messagingService, Serializer serializer) { 21 + public MessageDecoder(NettyMessagingService messagingService, PayloadSerializer payloadSerializer) {
22 super(DecoderState.READ_HEADER_VERSION); 22 super(DecoderState.READ_HEADER_VERSION);
23 this.messagingService = messagingService; 23 this.messagingService = messagingService;
24 - this.serializer = serializer; 24 + this.payloadSerializer = payloadSerializer;
25 } 25 }
26 26
27 @Override 27 @Override
...@@ -48,7 +48,7 @@ public class MessageDecoder extends ReplayingDecoder<DecoderState> { ...@@ -48,7 +48,7 @@ public class MessageDecoder extends ReplayingDecoder<DecoderState> {
48 checkState(serializerVersion == MessageEncoder.SERIALIZER_VERSION, "Unexpected serializer version"); 48 checkState(serializerVersion == MessageEncoder.SERIALIZER_VERSION, "Unexpected serializer version");
49 checkpoint(DecoderState.READ_CONTENT); 49 checkpoint(DecoderState.READ_CONTENT);
50 case READ_CONTENT: 50 case READ_CONTENT:
51 - InternalMessage message = serializer.decode(buffer.readBytes(contentLength).nioBuffer()); 51 + InternalMessage message = payloadSerializer.decode(buffer.readBytes(contentLength).nioBuffer());
52 message.setMessagingService(messagingService); 52 message.setMessagingService(messagingService);
53 out.add(message); 53 out.add(message);
54 checkpoint(DecoderState.READ_HEADER_VERSION); 54 checkpoint(DecoderState.READ_HEADER_VERSION);
......
...@@ -17,10 +17,10 @@ public class MessageEncoder extends MessageToByteEncoder<InternalMessage> { ...@@ -17,10 +17,10 @@ public class MessageEncoder extends MessageToByteEncoder<InternalMessage> {
17 public static final int SERIALIZER_VERSION = 1; 17 public static final int SERIALIZER_VERSION = 1;
18 18
19 19
20 - private final Serializer serializer; 20 + private final PayloadSerializer payloadSerializer;
21 21
22 - public MessageEncoder(Serializer serializer) { 22 + public MessageEncoder(PayloadSerializer payloadSerializer) {
23 - this.serializer = serializer; 23 + this.payloadSerializer = payloadSerializer;
24 } 24 }
25 25
26 @Override 26 @Override
...@@ -35,12 +35,12 @@ public class MessageEncoder extends MessageToByteEncoder<InternalMessage> { ...@@ -35,12 +35,12 @@ public class MessageEncoder extends MessageToByteEncoder<InternalMessage> {
35 // write preamble 35 // write preamble
36 out.writeBytes(PREAMBLE); 36 out.writeBytes(PREAMBLE);
37 37
38 - byte[] payload = serializer.encode(message); 38 + byte[] payload = payloadSerializer.encode(message);
39 39
40 // write payload length 40 // write payload length
41 out.writeInt(payload.length); 41 out.writeInt(payload.length);
42 42
43 - // write serializer version 43 + // write payloadSerializer version
44 out.writeInt(SERIALIZER_VERSION); 44 out.writeInt(SERIALIZER_VERSION);
45 45
46 // write payload. 46 // write payload.
......
...@@ -38,4 +38,11 @@ public interface MessagingService { ...@@ -38,4 +38,11 @@ public interface MessagingService {
38 * @param type message type 38 * @param type message type
39 */ 39 */
40 public void unregisterHandler(String type); 40 public void unregisterHandler(String type);
41 +
42 + /**
43 + * Specify the serializer to use for encoding/decoding payload.
44 + *
45 + * @param payloadSerializer payloadSerializer to use
46 + */
47 + public void setPayloadSerializer(PayloadSerializer payloadSerializer);
41 } 48 }
......
...@@ -52,7 +52,7 @@ public class NettyMessagingService implements MessagingService { ...@@ -52,7 +52,7 @@ public class NettyMessagingService implements MessagingService {
52 private final GenericKeyedObjectPool<Endpoint, Channel> channels 52 private final GenericKeyedObjectPool<Endpoint, Channel> channels
53 = new GenericKeyedObjectPool<Endpoint, Channel>(new OnosCommunicationChannelFactory()); 53 = new GenericKeyedObjectPool<Endpoint, Channel>(new OnosCommunicationChannelFactory());
54 54
55 - protected Serializer serializer; 55 + protected PayloadSerializer payloadSerializer;
56 56
57 public NettyMessagingService() { 57 public NettyMessagingService() {
58 // TODO: Default port should be configurable. 58 // TODO: Default port should be configurable.
...@@ -133,8 +133,9 @@ public class NettyMessagingService implements MessagingService { ...@@ -133,8 +133,9 @@ public class NettyMessagingService implements MessagingService {
133 handlers.remove(type); 133 handlers.remove(type);
134 } 134 }
135 135
136 - public void setSerializer(Serializer serializer) { 136 + @Override
137 - this.serializer = serializer; 137 + public void setPayloadSerializer(PayloadSerializer payloadSerializer) {
138 + this.payloadSerializer = payloadSerializer;
138 } 139 }
139 140
140 private MessageHandler getMessageHandler(String type) { 141 private MessageHandler getMessageHandler(String type) {
...@@ -201,13 +202,13 @@ public class NettyMessagingService implements MessagingService { ...@@ -201,13 +202,13 @@ public class NettyMessagingService implements MessagingService {
201 private class OnosCommunicationChannelInitializer extends ChannelInitializer<SocketChannel> { 202 private class OnosCommunicationChannelInitializer extends ChannelInitializer<SocketChannel> {
202 203
203 private final ChannelHandler dispatcher = new InboundMessageDispatcher(); 204 private final ChannelHandler dispatcher = new InboundMessageDispatcher();
204 - private final ChannelHandler encoder = new MessageEncoder(serializer); 205 + private final ChannelHandler encoder = new MessageEncoder(payloadSerializer);
205 206
206 @Override 207 @Override
207 protected void initChannel(SocketChannel channel) throws Exception { 208 protected void initChannel(SocketChannel channel) throws Exception {
208 channel.pipeline() 209 channel.pipeline()
209 .addLast("encoder", encoder) 210 .addLast("encoder", encoder)
210 - .addLast("decoder", new MessageDecoder(NettyMessagingService.this, serializer)) 211 + .addLast("decoder", new MessageDecoder(NettyMessagingService.this, payloadSerializer))
211 .addLast("handler", dispatcher); 212 .addLast("handler", dispatcher);
212 } 213 }
213 } 214 }
......
...@@ -5,7 +5,7 @@ import java.nio.ByteBuffer; ...@@ -5,7 +5,7 @@ import java.nio.ByteBuffer;
5 /** 5 /**
6 * Interface for encoding/decoding message payloads. 6 * Interface for encoding/decoding message payloads.
7 */ 7 */
8 -public interface Serializer { 8 +public interface PayloadSerializer {
9 9
10 /** 10 /**
11 * Decodes the specified byte array to a POJO. 11 * Decodes the specified byte array to a POJO.
......
...@@ -44,8 +44,8 @@ public final class SimpleClient { ...@@ -44,8 +44,8 @@ public final class SimpleClient {
44 public static class TestNettyMessagingService extends NettyMessagingService { 44 public static class TestNettyMessagingService extends NettyMessagingService {
45 public TestNettyMessagingService(int port) throws Exception { 45 public TestNettyMessagingService(int port) throws Exception {
46 super(port); 46 super(port);
47 - Serializer serializer = new KryoSerializer(); 47 + PayloadSerializer payloadSerializer = new KryoSerializer();
48 - this.serializer = serializer; 48 + this.payloadSerializer = payloadSerializer;
49 } 49 }
50 } 50 }
51 } 51 }
......
...@@ -6,7 +6,7 @@ public final class SimpleServer { ...@@ -6,7 +6,7 @@ public final class SimpleServer {
6 public static void main(String... args) throws Exception { 6 public static void main(String... args) throws Exception {
7 NettyMessagingService server = new NettyMessagingService(8080); 7 NettyMessagingService server = new NettyMessagingService(8080);
8 server.activate(); 8 server.activate();
9 - server.setSerializer(new KryoSerializer()); 9 + server.setPayloadSerializer(new KryoSerializer());
10 server.registerHandler("simple", new LoggingHandler()); 10 server.registerHandler("simple", new LoggingHandler());
11 server.registerHandler("echo", new EchoHandler()); 11 server.registerHandler("echo", new EchoHandler());
12 } 12 }
......