Package dijjer.io.comm

Examples of dijjer.io.comm.Message$FieldNotSetException


    request.addField("short", Short.class);
    request.addField("int", Integer.class);
    request.addField("string", String.class);
    request.addField("buffer", Buffer.class);
    request.addLinkedListField("list", String.class);
    Message m1 = new Message(request);
    m1.set("boolean", true);
    m1.set("byte", (byte) 15);
    m1.set("short", (short) 32);
    m1.set("int", (int) 64);
    m1.set("string", "gnirts");
    Buffer b = new Buffer(new byte[10]);
    m1.set("buffer", b);
    LinkedList ll = new LinkedList();
    ll.add("one");
    ll.add("two");
    ll.add("three");
    m1.set("list", ll);
    System.out.println("Original: " + m1);
    DatagramPacket dp = m1.encodeToPacket();
    Message m2 = Message.decodeFromPacket(dp);
    System.out.println("Decoded: " + m2);
    MessageFilter filter1 = MessageFilter.create(1000, request).setField("string", "abacus");
    MessageFilter filter2 = MessageFilter.create(1000, request).setField("string", "gnirts");
    assertFalse(filter1.match(m2));
    assertTrue(filter2.match(m2));

    request.unregister();
   
    MessageType shortRequest = new MessageType("Request");
    shortRequest.addField("boolean", Boolean.class);
    shortRequest.addField("byte", Byte.class);
    shortRequest.addField("short", Short.class);
    shortRequest.addField("int", Integer.class);
   
    Message m3 = Message.decodeFromPacket(dp);
    System.out.println("Short decoded: "+m3);
   
    Message m4 = new Message(shortRequest);
    m4.set("boolean", true);
    m4.set("byte", (byte) 15);
    m4.set("short", (short) 32);
    m4.set("int", (int) 64);
   
    DatagramPacket dp2 = m4.encodeToPacket();
   
    shortRequest.unregister();
   
    request = new MessageType("Request");
    request.addField("boolean", Boolean.class);
    request.addField("byte", Byte.class);
    request.addField("short", Short.class);
    request.addField("int", Integer.class);
    request.addField("string", String.class);
    request.addField("buffer", Buffer.class);
    request.addLinkedListField("list", String.class);
   
    Message m5 = Message.decodeFromPacket(dp2);
   
    System.out.println("Long decoded: "+m5);
  }
View Full Code Here


        // Sender
        Misc.startThread(new Runnable() {
            public void run() {
                int id = 0;
                for(int i=0;i<15;i++) {
                    Message p = new Message(ping);
                    p.set("id", id++);
                    p.set(DMT.EXTERNAL_ADDRESS, dest);
                    System.out.print("Sender: Ping sent... ");
                    sourceUSM.send(dest, p);
                    Message r = sourceUSM.waitFor(MessageFilter.create(10000, pong).setField("id",
                        new Integer(id - 1)));
                    if (r != null) {
                        _pongcount++;
                        synchronized (lock) {
                            lock.notifyAll();
                        }
                        System.out.println("Sender: Pong received.");
                    } else {
                        System.out.println("Timed out");
                    }
                }
            }
        });
        // Receiver
        for (int i = 0; i < 15; i++) {
            Message p = destUSM.waitFor(MessageFilter.create(10000, ping));
            if (p != null) {
                _pingcount++;
                System.err.println("Receiver: Ping " + p.getInt("id") + " received.  Sending pong");
                Message po = new Message(pong);
                po.set("id", p.getInt("id"));
                po.set(DMT.EXTERNAL_ADDRESS, source);
                destUSM.send(source, po);
            }
        }
        assertEquals(15, _pingcount);
        synchronized(lock) {
View Full Code Here

  protected void startReceiverThread() {
    (new Thread() {

      public void run() {
        while (!isFinished()) {
          Message msg = _usm.waitFor((MessageFilter.create(10000, DMT.missingPacketNotification)
              .addType(DMT.allReceived)).setField(DMT.UID, _uid));
          if (msg != null) {
            if (msg.getSpec().equals(DMT.missingPacketNotification)) {
              if (_prb.isAborted()) {
                _usm.send(_dest, DMT.createSendAborted(_prb.getAbortReason(), _prb
                    .getAbortDescription()));
              } else {
                LinkedList missing = (LinkedList) msg.getObject(DMT.MISSING);
                if (missing.size() > 50) {
                  Logger.warning("Received large missing packet notification from " + _dest
                      + " of size " + missing.size() + " having sent " + getNumSent());
                }
                _throttle.notifyOfPacketLoss(missing.size());
                for (Iterator i = missing.iterator(); i.hasNext();) {
                  Integer packetNo = (Integer) i.next();
                  if (_prb.isReceived(packetNo.intValue())) {
                    _sent.setBit(packetNo.intValue(), false);
                  }
                }
              }
            } else if (msg.getSpec().equals(DMT.allReceived)) {
              _allReceived = true;
            }
            synchronized (BlockTransmitter.this) {
              BlockTransmitter.this.notify();
            }
View Full Code Here

    MessageFilter f = MessageFilter.create(INITIAL_RECEIPT_TIMEOUT, DMT.packetTransmit)
              .setField(DMT.UID, _uid)
              .addType(DMT.allSent)
              .addType(DMT.sendAborted);
    while (!_prb.allReceived()) {
      Message m1 = _usm.waitFor(f);
      // Get ready for the next packet
      f = MessageFilter.create(RECEIPT_TIMEOUT, DMT.packetTransmit)
        .setField(DMT.UID, _uid)
        .addType(DMT.allSent)
        .addType(DMT.sendAborted);
      // The faster we finish the rest of this loop the better!
      if ((m1 != null) && (m1.getSpec().equals(DMT.packetTransmit))) {
        consecutiveMissingPacketReports = 0;
        // packetTransmit received
        int packetNo = m1.getInt(DMT.PACKET_NO);
        BitArray sent = (BitArray) m1.getObject(DMT.SENT);
        Buffer data = (Buffer) m1.getObject(DMT.DATA);
        _prb.addPacket(packetNo, data);
        // Remove it from rrmp if its in there
        _recentlyReportedMissingPackets.remove(new Integer(packetNo));
        // Skip checks except for 1 out of 16 packets until we get to the end
        if ((_prb.numReceived() < (_prb.getNumPackets() - 16))
          && (packetNo % 16 != 0))
          continue;
        // Check that we have what the sender thinks we have
        LinkedList missing = new LinkedList();
        for (int x = 0; x < sent.getSize(); x++) {
          if (sent.bitAt(x) && !_prb.isReceived(x)) {
            // Sender thinks we have a block which we don't, but have we already
            // re-requested it recently?
            Long resendTime = (Long) _recentlyReportedMissingPackets.get(new Integer(x));
            if ((resendTime == null) || (System.currentTimeMillis() > resendTime.longValue())) {
              // Make a note of the earliest time we should resend this,
              // based on the number of other packets we are already waiting for
              long resendWait = System.currentTimeMillis()
                  + (10*MAX_ROUND_TRIP_TIME + (_recentlyReportedMissingPackets.size() * MAX_SEND_INTERVAL));
              _recentlyReportedMissingPackets.put(new Integer(x), (new Long(resendWait)));
              missing.add(new Integer(x));
            }
          }
        }
        if (missing.size() > 0) {
          Message mn = DMT.createMissingPacketNotification(_uid, missing);
          _usm.send(_sender, mn);
          consecutiveMissingPacketReports++;
          if (missing.size() > 50) {
            Logger.warning("Excessive packet loss : " + mn);
          }
        }
        continue;
      }
      if ((m1 != null) && m1.getSpec().equals(DMT.sendAborted)) {
        _prb.abort(m1.getInt(DMT.REASON), m1.getString(DMT.DESCRIPTION));
        throw new RetrievalException(m1.getInt(DMT.REASON), m1.getString(DMT.DESCRIPTION));
      }
      if ((m1 == null) || (m1.getSpec().equals(DMT.allSent))) {
        if (consecutiveMissingPacketReports >= MAX_CONSECUTIVE_MISSING_PACKET_REPORTS) {
          _prb.abort(RetrievalException.SENDER_DIED, "Sender unresponsive to resend requests");
          LinkedList rem = new LinkedList();
          rem.add(_sender);
          // TODO: This is a stupid work around for BlockTransferTest and needs to be fixed
          if (RoutingTable.getRoutingTable() != null)
          RoutingTable.getRoutingTable().removePeers(rem, "Failed to send data after acking request");
          throw new RetrievalException(RetrievalException.SENDER_DIED,
              "Sender unresponsive to resend requests");
        }
        LinkedList missing = new LinkedList();
        for (int x = 0; x < _prb.getNumPackets(); x++) {
          if (!_prb.isReceived(x)) {
            missing.addFirst(new Integer(x));
          }
        }
        Message mn = DMT.createMissingPacketNotification(_uid, missing);
        _usm.send(_sender, mn);
        consecutiveMissingPacketReports++;
        if (missing.size() > 50) {
          Logger.warning("Sending large missingPacketNotification to " + _sender
              + " due to packet receiver timeout after " + RECEIPT_TIMEOUT + "ms having received "
View Full Code Here

      if (!forwarders.contains(_rt.getPeer())) {
        forwarders.add(_rt.getPeer());
      }
      _usm.send(best, DMT.createRequestData(uid, bi, forwarders, ttl - 1));
      // Now we expect an Ack, give it 4 seconds
      Message ar = _usm.waitFor(MessageFilter.create(4000, DMT.acknowledgeRequest).addType(DMT.rejectDueToLoop)
          .setField(DMT.UID, uid));
      if (ar == null) {
        // No response in 4 seconds, flag this guy to be recontacted and continue the while loop to find another
        // or possibly just download it ourselves
        if (best == last) exclude.add(best);
        _rt.recontactPeer(best);
        exclude.add(best);
        continue;
      }
      if (ar.getSpec().equals(DMT.rejectDueToLoop)) {
        Logger.warning("requestData (uid: " + uid + ") rejected by " + best + " due to loop");
        exclude.add(best);
        continue;
      }
      // Got the ack, we give it 5 minutes to account for the time it might take a peer to download it from
      // a slow server
      MessageFilter succFilt = MessageFilter.create(30000, DMT.requestSuccessful).addType(DMT.requestFailed)
          .setSource(best).setField(DMT.UID, uid);
      final Message resp = _usm.waitFor(succFilt);
      if ((resp == null) || (resp.getSpec().equals(DMT.requestFailed))) {
        if (resp == null) {
          if (requestor != null) {
            _usm.send(requestor, DMT.createRequestFailed(uid, RetrievalException.TIMED_OUT,
                "Timed out waiting for requestSuccessful"));
          }
          throw new RetrievalException(RetrievalException.TIMED_OUT,
              "Timed out waiting on requestSuccessful for block " + bi.getBlockNo());
        } else {
          if (requestor != null) {
            _usm.send(requestor, DMT.createRequestFailed(uid, (resp.isSet(DMT.REASON) ? resp
                .getInt(DMT.REASON) : RetrievalException.UNKNOWN), (resp.isSet(DMT.DESCRIPTION) ? resp
                .getString(DMT.DESCRIPTION) : "Unknown")));
          }
          throw new RetrievalException((resp.isSet(DMT.REASON)
              ? resp.getInt(DMT.REASON)
              : RetrievalException.UNKNOWN), (resp.isSet(DMT.DESCRIPTION) ? resp
              .getString(DMT.DESCRIPTION) : "Unknown"));
        }
      }
      // Ok, the data has been retrieved and should be on its way
      if (requestor != null) {
        (new Thread() {

          public void run() {
            forwardData(requestor, prb, uid, forwarders, resp.getBoolean(DMT.CACHED));
            if (prb.allReceived()) {
              try {
                _ds.addDataAsBlock(bi.getHashKey(), prb.getBlock());
              } catch (IOException e) {
                Logger.error("Error writing data to datastore", e);
              }
            }
          }
        }).start();
        // We add the new peer to our RT *after* the transfer as this is when it will start trying to contact us
        _rt.addPeer((Peer) resp.getObject(DMT.DATA_SOURCE));
        // Note that we only add the data to our DS if someone else requested it from us
      }
      BlockReceiver br = new BlockReceiver(_usm, best, uid, prb);
      br.receive();
      return resp.getBoolean(DMT.CACHED);
    }
  }
View Full Code Here

        _usm.send(jr.getSource(), DMT.createJoinResponse(uid, peers));
        break;
      }
      exclude.add(new Integer(RoutingTable.getRoutingTable().getPeer().hashCode()));
      _usm.send(best, DMT.createJoinRequest(uid, joiner, jr.getInt(DMT.TTL) - 1, exclude));
      Message jra = _usm.waitFor(MessageFilter.create(6000, DMT.joinRequestAck).addType(DMT.rejectDueToLoop)
          .setField(DMT.UID, uid));
      if (jra == null) {
        _rt.recontactPeer(best);
        continue;
      }
      if (jra.getSpec().equals(DMT.rejectDueToLoop)) {
        dontConsider.add(best);
        Logger.warning("joinRequest (uid: " + uid + ") rejected by " + jra.getSource() + " due to loop");
        continue;
      }
      Message jrs = _usm.waitFor(MessageFilter.create(1000 * (ttl + 3), DMT.joinResponse).setSource(best)
          .setField(DMT.UID, uid));
      if (jrs == null) {
        LinkedList peers = new LinkedList();
        peers.add(_rt.getPeer());
        _rt.addPeer(joiner);
        _usm.send(jr.getSource(), DMT.createJoinResponse(uid, peers));
        break;
      } else {
        LinkedList peers = (LinkedList) jrs.getObject(DMT.PEERS);
        if (_rt.getPeers().size() < _rt.getMaxSize()) {
          peers.add(_rt.getPeer());
          _rt.addPeer(joiner);
        }
        // If any are null assume they are the sender of the joinResponse
View Full Code Here

        _hs.put(bi.getHashHashKey(), dataHash);
        return dataHash;
      }
      // Ok, we are going to try forwarding it to the next closest peer
      _usm.send(best, DMT.createRequestHash(uid, bi, ttl - 1));
      Message requestHashAck = _usm.waitFor(MessageFilter.create(5000, DMT.requestHashAck).addType(
          DMT.rejectDueToLoop).setField(DMT.UID, uid));
      if (requestHashAck == null) {
        exclude.add(best);
        _rt.recontactPeer(best);
        continue;
      }
      if (requestHashAck.getSpec().equals(DMT.rejectDueToLoop)) {
        Logger.warning("requestHash (uid: " + uid + ") rejected by " + requestHashAck.getSource()
            + " due to loop");
        exclude.add(best);
        continue;
      }
      Message replyHash = _usm.waitFor(MessageFilter.create(30000, DMT.replyHash).setField(DMT.UID, uid));
      if (replyHash == null) {
        Logger.info("Timed out waiting for replyHash from " + best);
        ttl = 0;
        continue;
      }
      VeryLongInteger dataHash = (VeryLongInteger) replyHash.getObject(DMT.HASH);
      if (cacheResult) {
        _hs.put(bi.getHashHashKey(), dataHash);
      }
      return dataHash;
    }
View Full Code Here

TOP

Related Classes of dijjer.io.comm.Message$FieldNotSetException

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.