1 package org.opendaylight.controller.sal.connector.zeromq;
4 import org.codehaus.jackson.map.ObjectMapper;
5 import org.opendaylight.controller.sal.connector.api.RpcRouter;
8 import java.util.Arrays;
10 public class Message implements Serializable {
12 public enum MessageType {
18 private final byte type;
20 MessageType(byte type) {
24 public byte getType(){
29 private MessageType type;
30 private String sender;
31 private RpcRouter.RouteIdentifier route;
32 private Object payload;
34 public MessageType getType() {
38 public void setType(MessageType type) {
42 public String getSender() {
46 public void setSender(String sender) {
50 public RpcRouter.RouteIdentifier getRoute() {
54 public void setRoute(RpcRouter.RouteIdentifier route) {
58 public Object getPayload() {
62 public void setPayload(Object payload) {
63 this.payload = payload;
67 public String toString() {
70 ", sender='" + sender + '\'' +
72 ", payload=" + payload +
77 * Converts any {@link Serializable} object to byte[]
83 public static byte[] serialize(Object obj) throws IOException {
84 ByteArrayOutputStream b = new ByteArrayOutputStream();
85 ObjectOutputStream o = new ObjectOutputStream(b);
87 return b.toByteArray();
91 * Converts byte[] to a java object
96 * @throws ClassNotFoundException
98 public static Object deserialize(byte[] bytes) throws IOException, ClassNotFoundException {
99 ByteArrayInputStream b = new ByteArrayInputStream(bytes);
100 ObjectInputStream o = new ObjectInputStream(b);
101 return o.readObject();
104 public static byte[] toJsonBytes(Message m){
105 ObjectMapper o = new ObjectMapper();
107 System.out.println(o.writeValueAsString(m));
108 return o.writeValueAsBytes(m);
109 } catch (IOException e) {
110 e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
115 public static Message fromJsonBytes(byte [] bytes){
117 ObjectMapper o = new ObjectMapper();
120 m = o.readValue(bytes, Message.class);
121 } catch (IOException e) {
122 e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
127 public static class Response extends Message implements RpcRouter.RpcReply {
128 private ResponseCode code; // response code
130 public static enum ResponseCode {
131 SUCCESS(200), BADREQUEST(400), TIMEOUT(408), GONE(410), SERVERERROR(500), SERVICEUNAVAILABLE(503);
135 ResponseCode(int code) {
140 public ResponseCode getCode() {
144 public void setCode(ResponseCode code) {