Exemple #1
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;
 }
Exemple #2
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();
    }
  }