Permalink
Browse files

Add kv simple file.

  • Loading branch information...
1 parent 89ebc8f commit 6f6555c4a00161ce1f4f6d825b952aa88a9f299c @dshrestha06 dshrestha06 committed Mar 8, 2013
Showing with 90 additions and 0 deletions.
  1. +90 −0 examples/Java/kvsimple.java
@@ -0,0 +1,90 @@
+import java.nio.ByteBuffer;
+import java.util.Arrays;
+
+import org.zeromq.ZMQ;
+import org.zeromq.ZMQ.Socket;
+/**
+ *
+ * A simple key value message class
+ * @author Danish Shrestha <dshrestha06@gmail.com>
+ *
+ */
+public class kvsimple {
+ private final String key;
+ private final long sequence;
+ private final byte[] body;
+
+ public kvsimple(String key, long sequence, byte[] body) {
+ this.key = key;
+ this.sequence = sequence;
+ this.body = body; // clone if needed
+ }
+
+ public String getKey() {
+ return key;
+ }
+
+ public long getSequence() {
+ return sequence;
+ }
+
+ public byte[] getBody() {
+ return body;
+ }
+
+ public void send(Socket publisher) {
+
+ publisher.send(key.getBytes(), ZMQ.SNDMORE);
+
+ ByteBuffer bb = ByteBuffer.allocate(8);
+ bb.asLongBuffer().put(sequence);
+ publisher.send(bb.array(), ZMQ.SNDMORE);
+
+ publisher.send(body, 0);
+ }
+
+ public static kvsimple recv(Socket updates) {
+ String key = new String(updates.recv(0));
+ Long sequence = ByteBuffer.wrap(updates.recv(0)).getLong();
+ byte[] body = updates.recv(0);
+
+ return new kvsimple(key, sequence, body);
+ }
+
+ @Override
+ public String toString() {
+ return "kvsimple [key=" + key + ", sequence=" + sequence + ", body=" + Arrays.toString(body) + "]";
+ }
+
+ @Override
+ public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + Arrays.hashCode(body);
+ result = prime * result + ((key == null) ? 0 : key.hashCode());
+ result = prime * result + (int) (sequence ^ (sequence >>> 32));
+ return result;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj)
+ return true;
+ if (obj == null)
+ return false;
+ if (getClass() != obj.getClass())
+ return false;
+ kvsimple other = (kvsimple) obj;
+ if (!Arrays.equals(body, other.body))
+ return false;
+ if (key == null) {
+ if (other.key != null)
+ return false;
+ } else if (!key.equals(other.key))
+ return false;
+ if (sequence != other.sequence)
+ return false;
+ return true;
+ }
+
+}

0 comments on commit 6f6555c

Please sign in to comment.