Exemple #1
0
 public boolean Send(Payload p) {
   try {
     int identity = rand.nextInt(Integer.MAX_VALUE);
     while (identifierPool.contains(identity)) identity = rand.nextInt(Integer.MAX_VALUE);
     p.identity = identity;
     identifierPool.add(identity);
     socket.send(p.getPacket(socket));
   } catch (IOException e) {
     return false;
   }
   return true;
 }
Exemple #2
0
  public ResponsePayload SendAndReceive(final Payload p) throws IOException {
    try {
      int identity = rand.nextInt(Integer.MAX_VALUE);
      while (identifierPool.contains(identity)) identity = rand.nextInt(Integer.MAX_VALUE);
      p.identity = identity;
      identifierPool.add(identity);
      pendingRes.put(p.identity, null);
      socket.send(p.getPacket(socket));
    } catch (IOException e) {
      pendingRes.remove(p.identity);
      return null;
    }
    Thread t =
        new Thread(
            new Runnable() {

              @Override
              public void run() {
                try {
                  Thread.sleep(10000);
                  ResponsePayload timeout = new ResponsePayload();
                  timeout.status = "failed";
                  timeout.message = "Connection timeout!";
                  pendingRes.put(p.identity, timeout);
                } catch (InterruptedException e) {
                  e.printStackTrace();
                }
              }
            });
    t.start();
    while (pendingRes.get(p.identity) == null && pendingRes.containsKey(p.identity)) {
      try {
        Thread.sleep(500);
      } catch (InterruptedException e) {
        return pendingRes.remove(p.identity);
      }
    }
    t.interrupt();
    return pendingRes.remove(p.identity);
  }