Ejemplo n.º 1
0
  static void marshal(Object obj) throws Exception {
    byte[] buf = Util.objectToByteBuffer(obj);
    assert buf != null;
    assert buf.length > 0;
    Object obj2 = Util.objectFromByteBuffer(buf);
    System.out.println(
        "obj="
            + obj
            + ", obj2="
            + obj2
            + " (type="
            + obj.getClass().getName()
            + ", length="
            + buf.length
            + " bytes)");
    Assert.assertEquals(obj, obj2);

    if (obj instanceof Integer) { // test compressed ints and longs
      buf = new byte[10];
      Bits.writeIntCompressed((int) obj, buf, 0);
      obj2 = Bits.readIntCompressed(buf, 0);
      assert obj.equals(obj2);
    }
    if (obj instanceof Long) { // test compressed ints and longs
      buf = new byte[10];
      Bits.writeLongCompressed((long) obj, buf, 0);
      obj2 = Bits.readLongCompressed(buf, 0);
      assert obj.equals(obj2);
    }
  }
Ejemplo n.º 2
0
 public static void testWriteString() throws Exception {
   String s1 = "Bela Ban", s2 = "Michelle Ban";
   ByteArrayOutputStream outstream = new ByteArrayOutputStream();
   DataOutputStream dos = new DataOutputStream(outstream);
   Bits.writeString(s1, dos);
   Bits.writeString(s2, dos);
   dos.close();
   byte[] buf = outstream.toByteArray();
   ByteArrayInputStream instream = new ByteArrayInputStream(buf);
   DataInputStream dis = new DataInputStream(instream);
   String s3 = Bits.readString(dis);
   String s4 = Bits.readString(dis);
   Assert.assertEquals(s1, s3);
   Assert.assertEquals(s2, s4);
 }
Ejemplo n.º 3
0
 @Override
 public void readFrom(DataInput in) throws Exception {
   type = in.readByte();
   messageID.readFrom(in);
   sequencerNumber = Bits.readLong(in);
   destinations = (Collection<Address>) Util.readAddresses(in, ArrayList.class);
 }
Ejemplo n.º 4
0
 @Override
 public void writeTo(DataOutput out) throws Exception {
   out.writeByte(type);
   messageID.writeTo(out);
   Bits.writeLong(sequencerNumber, out);
   Util.writeAddresses(destinations, out);
 }
Ejemplo n.º 5
0
 @Override
 public int size() {
   return (int)
       (Global.BYTE_SIZE
           + messageID.serializedSize()
           + Bits.size(sequencerNumber)
           + Util.size(destinations));
 }
Ejemplo n.º 6
0
 public void writeTo(DataOutput out) throws Exception {
   out.writeByte(type);
   switch (type) {
     case DATA:
       Bits.writeLong(seqno, out);
       out.writeShort(conn_id);
       out.writeBoolean(first);
       break;
     case ACK:
       Bits.writeLong(seqno, out);
       out.writeShort(conn_id);
       break;
     case SEND_FIRST_SEQNO:
       Bits.writeLong(seqno, out);
       break;
   }
 }
Ejemplo n.º 7
0
 public void readFrom(DataInput in) throws Exception {
   type = in.readByte();
   switch (type) {
     case DATA:
       seqno = Bits.readLong(in);
       conn_id = in.readShort();
       first = in.readBoolean();
       break;
     case ACK:
       seqno = Bits.readLong(in);
       conn_id = in.readShort();
       break;
     case SEND_FIRST_SEQNO:
       seqno = Bits.readLong(in);
       break;
   }
 }
Ejemplo n.º 8
0
 protected void getStateFrom(
     JChannel channel, Protocol prot, String stack, String ch, DataOutputStream out)
     throws Exception {
   ByteArrayDataOutputStream output = new ByteArrayDataOutputStream(1024);
   OutputStreamAdapter out_ad = new OutputStreamAdapter(output);
   Event evt = new Event(Event.STATE_TRANSFER_OUTPUTSTREAM, out_ad);
   if (channel != null) channel.up(evt);
   else prot.up(evt);
   int len = output.position();
   if (len > 0) {
     Bits.writeString(stack, out);
     Bits.writeString(ch, out);
     out.writeInt(len);
     out.write(output.buffer(), 0, len);
     log.trace("%s: fetched %d bytes from %s:%s", local_addr, len, stack, ch);
   }
 }
Ejemplo n.º 9
0
 public final int size() {
   int retval = Global.BYTE_SIZE; // type
   switch (type) {
     case DATA:
       retval +=
           Bits.size(seqno) // seqno
               + Global.SHORT_SIZE // conn_id
               + Global.BYTE_SIZE; // first
       break;
     case ACK:
       retval += Bits.size(seqno) + Global.SHORT_SIZE; // conn_id
       break;
     case SEND_FIRST_SEQNO:
       retval += Bits.size(seqno);
       break;
   }
   return retval;
 }
Ejemplo n.º 10
0
 protected void setStateInMainAndForkChannels(InputStream in) {
   try (DataInputStream input = new DataInputStream(in)) {
     for (; ; ) {
       String stack_name = Bits.readString(input);
       String ch_name = Bits.readString(input);
       int len = input.readInt();
       if (len > 0) {
         byte[] data = new byte[len];
         in.read(data, 0, len);
         ByteArrayInputStream tmp = new ByteArrayInputStream(data, 0, len);
         if (stack_name == null && ch_name == null)
           up_prot.up(new Event(Event.STATE_TRANSFER_INPUTSTREAM, tmp));
         else {
           Protocol prot = fork_stacks.get(stack_name);
           if (prot == null) {
             log.warn(
                 "%s: fork stack %s not found, dropping state for %s:%s",
                 local_addr, stack_name, stack_name, ch_name);
             continue;
           }
           ForkProtocolStack fork_stack = getForkStack(prot);
           JChannel fork_ch = fork_stack.get(ch_name);
           if (fork_ch == null) {
             log.warn(
                 "%s: fork channel %s not found, dropping state for %s:%s",
                 local_addr, ch_name, stack_name, ch_name);
             continue;
           }
           fork_ch.up(new Event(Event.STATE_TRANSFER_INPUTSTREAM, tmp));
         }
       }
     }
   } catch (EOFException eof) {
   } catch (Throwable ex) {
     log.error("%s: failed setting state in main channel", local_addr, ex);
   }
 }
Ejemplo n.º 11
0
 public int size() {
   return Global.BYTE_SIZE + Bits.size(seqno) + Global.BYTE_SIZE; // type + seqno + flush_ack
 }
Ejemplo n.º 12
0
 public void readFrom(DataInput in) throws Exception {
   type = in.readByte();
   seqno = Bits.readLong(in);
   flush_ack = in.readBoolean();
 }
Ejemplo n.º 13
0
 public void writeTo(DataOutput out) throws Exception {
   out.writeByte(type);
   Bits.writeLong(seqno, out);
   out.writeBoolean(flush_ack);
 }
Ejemplo n.º 14
0
 public void readFrom(DataInput in) throws Exception {
   fork_stack_id = Bits.readString(in);
   fork_channel_id = Bits.readString(in);
 }
Ejemplo n.º 15
0
 public void writeTo(DataOutput out) throws Exception {
   Bits.writeString(fork_stack_id, out);
   Bits.writeString(fork_channel_id, out);
 }