void timerExpired() {
   // should not happen, but if it does,
   // fabricate a permanent error and forward up
   DatagramRejectedMessage msg =
       new DatagramRejectedMessage(message.getDestNodeID(), message.getSourceNodeID(), 0x0100);
   System.out.println(
       "Never received reply for datagram "
           + (message != null ? message.toString() : " == null"));
   handleDatagramRejected(msg, null);
 }
    /** Handle "Datagram Acknowledged" message */
    @Override
    public void handleDatagramAcknowledged(DatagramAcknowledgedMessage msg, Connection sender) {
      // check if this is from right source & to us
      if (!(msg.getDestNodeID() != null
          && msg.getSourceNodeID() != null
          && msg.getDestNodeID().equals(message.getSourceNodeID())
          && message.getDestNodeID().equals(msg.getSourceNodeID()))) {
        // not for us, just forward
        toUpstream.put(msg, toUpstream);
        return;
      }
      endTimeout();
      // forward message upstream
      toUpstream.put(msg, toUpstream);

      // and allow sending another
      new Thread(new Consumer(queue)).start();
    }
 /** Handle "Datagram Rejected" message */
 @Override
 public void handleDatagramRejected(DatagramRejectedMessage msg, Connection sender) {
   // check if this is from right source & to us
   if (!(msg.getDestNodeID() != null
       && msg.getSourceNodeID() != null
       && msg.getDestNodeID().equals(message.getSourceNodeID())
       && message.getDestNodeID().equals(msg.getSourceNodeID()))) {
     // not for us, just forward
     toUpstream.put(msg, toUpstream);
     return;
   }
   endTimeout();
   // check if resend permitted
   if (msg.canResend()) {
     forwardDownstream();
   } else {
     // forward upstream to originator and let them sort it out
     toUpstream.put(msg, toUpstream);
     // and allow sending another
     new Thread(new Consumer(queue)).start();
   }
 }
示例#4
0
 public static void main(String[] args) {
   int serverPort = 13; // default port
   if (args.length == 1) serverPort = Integer.parseInt(args[0]);
   try {
     // instantiates a datagram socket for both sending
     // and receiving data
     MyServerDatagramSocket mySocket = new MyServerDatagramSocket(serverPort);
     System.out.println("Daytime server ready.");
     while (true) { // forever loop
       DatagramMessage request = mySocket.receiveMessageAndSender();
       System.out.println("Request received");
       // The message received is unimportant; it is the sender's
       // address that we need in order to reply.
       // Now obtain the timestamp from the local system.
       Date timestamp = new Date();
       System.out.println("timestamp sent: " + timestamp.toString());
       // Now send the reply to the requestor
       mySocket.sendMessage(request.getAddress(), request.getPort(), timestamp.toString());
     } // end while
   } // end try
   catch (Exception ex) {
     System.out.println("There is a problem: " + ex);
   } // end catch
 } // end main
 void endTimeout() {
   if (timer != null) timer.cancel();
   else
     System.out.println(
         "Found timer null for datagram " + (message != null ? message.toString() : " == null"));
 }