Enhanced message handling
[controller.git] / opendaylight / md-sal / sal-remoterpc-connector / implementation / src / test / java / org / opendaylight / controller / sal / connector / remoterpc / utils / MessagingUtil.java
index 20cf4f636230952065b2770bbe189f17f1b9db4d..a68ee574f339526fa2c7a3f5b06e23a78ea13700 100644 (file)
@@ -78,6 +78,41 @@ public class MessagingUtil {
     };
   }
 
+  public static Runnable sendAMessage(final ZMQ.Context context, final String serverAddress, final Message msg)
+      throws IOException, ClassNotFoundException, InterruptedException {
+
+    return new Runnable() {
+      @Override
+      public void run() {
+        final ZMQ.Socket socket = context.socket(ZMQ.REQ);
+        try {
+
+          socket.connect(serverAddress);
+          System.out.println(Thread.currentThread().getName() + " Sending message");
+          try {
+            socket.send(Message.serialize(msg));
+          } catch (IOException e) {
+            e.printStackTrace();
+          }
+          byte[] bytes = socket.recv();
+          Message response = null;
+          try {
+            response = (Message) Message.deserialize(bytes);
+          } catch (IOException e) {
+            e.printStackTrace();
+          } catch (ClassNotFoundException e) {
+            e.printStackTrace();
+          }
+          System.out.println(Thread.currentThread().getName() + " Got response " + response);
+        } catch (Exception x) {
+          x.printStackTrace();
+        } finally {
+          socket.close();
+        }
+      }
+    };
+  }
+
   public static Runnable sendAnEmptyMessage(final ZMQ.Context context, final String serverAddress)
           throws IOException, ClassNotFoundException, InterruptedException {