Beispiel #1
0
 public static void sendBinary(
     Socket output,
     ZFrame routingId,
     int sequence,
     byte[] flags,
     byte[] public_key,
     UUID identifier,
     ZFrame address,
     ZMsg content) {
   ZprotoExample self = new ZprotoExample(ZprotoExample.BINARY);
   if (routingId != null) {
     self.setRoutingId(routingId);
   }
   self.setSequence(sequence);
   self.setFlags(flags);
   self.setPublic_Key(public_key);
   self.setIdentifier(identifier);
   self.setAddress(address.duplicate());
   self.setContent(content.duplicate());
   self.send(output);
 }
Beispiel #2
0
 public static void sendStructures(
     Socket output,
     ZFrame routingId,
     int sequence,
     List<String> aliases,
     Map<String, String> headers) {
   ZprotoExample self = new ZprotoExample(ZprotoExample.STRUCTURES);
   if (routingId != null) {
     self.setRoutingId(routingId);
   }
   self.setSequence(sequence);
   self.setAliases(new ArrayList<String>(aliases));
   self.setHeaders(new HashMap<String, String>(headers));
   self.send(output);
 }
Beispiel #3
0
 public ZprotoExample dup() {
   ZprotoExample copy = new ZprotoExample(this.id);
   if (this.routingId != null) copy.routingId = this.routingId.duplicate();
   switch (this.id) {
     case LOG:
       {
         copy.sequence = this.sequence;
         copy.version = this.version;
         copy.level = this.level;
         copy.event = this.event;
         copy.node = this.node;
         copy.peer = this.peer;
         copy.time = this.time;
         copy.host = this.host;
         copy.data = this.data;
       }
       break;
     case STRUCTURES:
       {
         copy.sequence = this.sequence;
         copy.aliases = new ArrayList<String>(this.aliases);
         copy.headers = new HashMap<String, String>(this.headers);
       }
       break;
     case BINARY:
       {
         copy.sequence = this.sequence;
         System.arraycopy(copy.flags, 0, this.flags, 0, 4);
         copy.public_key = this.public_key;
         copy.address = this.address.duplicate();
       }
       break;
     case TYPES:
       {
         copy.sequence = this.sequence;
         copy.client_forename = this.client_forename;
         copy.client_surname = this.client_surname;
         copy.client_mobile = this.client_mobile;
         copy.client_email = this.client_email;
         copy.supplier_forename = this.supplier_forename;
         copy.supplier_surname = this.supplier_surname;
         copy.supplier_mobile = this.supplier_mobile;
         copy.supplier_email = this.supplier_email;
       }
       break;
   }
   return copy;
 }
Beispiel #4
0
 public static void sendTypes(
     Socket output,
     ZFrame routingId,
     int sequence,
     String client_forename,
     String client_surname,
     String client_mobile,
     String client_email,
     String supplier_forename,
     String supplier_surname,
     String supplier_mobile,
     String supplier_email) {
   ZprotoExample self = new ZprotoExample(ZprotoExample.TYPES);
   if (routingId != null) {
     self.setRoutingId(routingId);
   }
   self.setSequence(sequence);
   self.setClient_Forename(client_forename);
   self.setClient_Surname(client_surname);
   self.setClient_Mobile(client_mobile);
   self.setClient_Email(client_email);
   self.setSupplier_Forename(supplier_forename);
   self.setSupplier_Surname(supplier_surname);
   self.setSupplier_Mobile(supplier_mobile);
   self.setSupplier_Email(supplier_email);
   self.send(output);
 }
Beispiel #5
0
 public static void sendLog(
     Socket output,
     ZFrame routingId,
     int sequence,
     int level,
     int event,
     int node,
     int peer,
     long time,
     String host,
     String data) {
   ZprotoExample self = new ZprotoExample(ZprotoExample.LOG);
   if (routingId != null) {
     self.setRoutingId(routingId);
   }
   self.setSequence(sequence);
   self.setLevel(level);
   self.setEvent(event);
   self.setNode(node);
   self.setPeer(peer);
   self.setTime(time);
   self.setHost(host);
   self.setData(data);
   self.send(output);
 }
Beispiel #6
0
  public static ZprotoExample recv(Socket input) {
    assert (input != null);
    ZprotoExample self = new ZprotoExample(0);
    ZFrame frame = null;

    try {
      //  Read valid message frame from socket; we loop over any
      //  garbage data we might receive from badly-connected peers
      while (true) {
        //  If we're reading from a ROUTER socket, get routingId
        if (input.getType() == ZMQ.ROUTER) {
          self.routingId = ZFrame.recvFrame(input);
          if (self.routingId == null) return null; //  Interrupted
          if (!self.routingId.hasData()) return null; //  Empty Frame (eg recv-timeout)
          if (!input.hasReceiveMore()) throw new IllegalArgumentException();
        }
        //  Read and parse command in frame
        frame = ZFrame.recvFrame(input);
        if (frame == null) return null; //  Interrupted

        //  Get and check protocol signature
        self.needle = ByteBuffer.wrap(frame.getData());
        int signature = self.getNumber2();
        if (signature == (0xAAA0 | 0)) break; //  Valid signature

        //  Protocol assertion, drop message
        while (input.hasReceiveMore()) {
          frame.destroy();
          frame = ZFrame.recvFrame(input);
        }
        frame.destroy();
      }

      //  Get message id, which is first byte in frame
      self.id = self.getNumber1();
      int listSize;
      int hashSize;

      switch (self.id) {
        case LOG:
          {
            self.sequence = self.getNumber2();
            self.version = self.getNumber2();
            if (self.version != 3) throw new IllegalArgumentException();
            self.level = self.getNumber1();
            self.event = self.getNumber1();
            self.node = self.getNumber2();
            self.peer = self.getNumber2();
            self.time = self.getNumber8();
            self.host = self.getString();
            self.data = self.getLongString();
          }
          break;

        case STRUCTURES:
          {
            self.sequence = self.getNumber2();
            listSize = (int) self.getNumber4();
            self.aliases = new ArrayList<String>();
            while (listSize-- > 0) {
              String string = self.getLongString();
              self.aliases.add(string);
            }
            hashSize = (int) self.getNumber4();
            self.headers = new HashMap<String, String>();
            while (hashSize-- > 0) {
              String key = self.getString();
              String value = self.getLongString();

              self.headers.put(key, value);
            }
          }
          break;

        case BINARY:
          {
            self.sequence = self.getNumber2();
            self.flags = self.getBlock(4);
            self.public_key = self.getBlock((int) self.getNumber4());
            ByteBuffer bbIdentifier = ByteBuffer.wrap(self.getBlock(16));
            self.identifier = new UUID(bbIdentifier.getLong(), bbIdentifier.getLong());
            //  Get next frame, leave current untouched
            if (!input.hasReceiveMore()) throw new IllegalArgumentException();
            self.address = ZFrame.recvFrame(input);
            self.content = new ZMsg();
            if (input.hasReceiveMore()) self.content.add(ZFrame.recvFrame(input));
          }
          break;

        case TYPES:
          {
            self.sequence = self.getNumber2();
            self.client_forename = self.getString();
            self.client_surname = self.getString();
            self.client_mobile = self.getString();
            self.client_email = self.getString();
            self.supplier_forename = self.getString();
            self.supplier_surname = self.getString();
            self.supplier_mobile = self.getString();
            self.supplier_email = self.getString();
          }
          break;

        default:
          throw new IllegalArgumentException();
      }

      return self;

    } catch (Exception e) {
      //  Error returns
      System.out.printf("E: malformed message '%d'\n", self.id);
      self.destroy();
      return null;
    } finally {
      if (frame != null) frame.destroy();
    }
  }