コード例 #1
0
  /*
   * (non-Javadoc)
   *
   * @see de.fhg.fokus.magic.http.IHTTPClient#sendRequestAndWaitForResponse(de.fhg.fokus.magic.http.HTTPMessageObject)
   */
  public void sendRequestAndWaitForResponse(HTTPMessageObject request) {
    if (request == null) {
      logger.error("no message to be sent");
      return;
    }

    // cancel request if too many active clients
    if (activeClients > HTTPDefaults.MAX_ACTIVE_CLIENTS) {
      return;
    }
    activeClients++;

    int numberOfRetries = 0;
    boolean success = false;

    while (!success && !terminateClient && numberOfRetries < HTTPDefaults.MAX_CLIENT_FAILURES) {
      try {
        if (socket == null) {
          socket = new DatagramSocket();
        }
        SocketHelper.sendBinaryMessage(request.toBinaryMessage(), socket);

        // try to receive response for 2 seconds
        BinaryMessageObject binaryResponse = SocketHelper.readBinaryMessage(null, socket, 2000);

        if (binaryResponse != null) {
          responseMessage = binaryResponse.toHTTPMessageObject();
          responseMessage.setDestinationAddress(binaryResponse.getSourceAddress());
          responseMessage.setSourceAddress(request.getDestinationAddress());
        }
        success = true;
      } catch (Exception e) {
        numberOfRetries++;
      }
    }
    tryClose();
    activeClients--;

    if (debug) {
      System.out.println("HTTPClient response header:" + responseMessage.getHeader());
      System.out.println(
          "HTTPClient response body:"
              + StringHelper.byteArrayToUTF8String(responseMessage.getBody()));
    }
  }
コード例 #2
0
  /** Reads messages */
  public void run() {
    while (!terminateThread) {
      for (int i = 0; i < socketStructureManagement.getSocketStructureCount(); i++) {
        BinaryDeviceHostAddressSocketStructure currentSocketStructure =
            (BinaryDeviceHostAddressSocketStructure)
                socketStructureManagement.getSocketStructure(i);

        BinaryMessageObject message = null;
        // read search messages
        do {
          message =
              SocketHelper.readBinaryMessage(null, currentSocketStructure.getDiscoverySocket(), 10);
          if (message != null) {
            // Portable.println("Discovery: Received multicast discovery message");
            binaryDevice.processDiscoveryMessage(message, currentSocketStructure);
          }
        } while (message != null);
        // read search reply messages
        do {
          message =
              SocketHelper.readBinaryMessage(
                  null, currentSocketStructure.getDescriptionSocket(), 10);
          if (message != null) {
            Portable.println(
                "Description: Received description request from "
                    + IPHelper.toString(message.getSourceAddress())
                    + " with "
                    + BinaryUPnPConstants.toDebugString(message.getBody()));
            binaryDevice.processDescriptionMessage(message, currentSocketStructure);
          }
        } while (message != null);
        do {
          // read control messages
          message =
              SocketHelper.readBinaryMessage(null, currentSocketStructure.getControlSocket(), 10);
          if (message != null) {
            binaryDevice.processControlMessage(message, currentSocketStructure);
          }
        } while (message != null);
      }
      ThreadHelper.sleep(50);
    }
    terminated = true;
  }
コード例 #3
0
 /**
  * Sends a request which is not answered by the server. This method can be used for time-critical
  * data
  */
 public void sendRequest(HTTPMessageObject request) {
   if (request == null) {
     logger.error("no message to be sent");
     return;
   }
   try {
     if (socket == null) {
       socket = new DatagramSocket();
     }
     SocketHelper.sendBinaryMessage(request.toBinaryMessage(), socket);
   } catch (Exception e) {
   } finally {
     tryClose();
   }
 }
コード例 #4
0
  /** Reads SSDP messages from the multicast sockets of all registered modules */
  public void run() {
    // System.out.println(" Start ExternalSSDPManagement thread, wait for NOTIFY and M-SEARCH
    // packets...");

    while (!terminateThread) {
      synchronized (listLock) {
        for (int i = 0; i < socketList.size(); i++) {
          IDatagramSocket socket = (IDatagramSocket) socketList.elementAt(i);

          boolean readUDP = true;
          while (readUDP) {
            BinaryMessageObject binaryMessage =
                SocketHelper.readBinaryMessage(udpPacketManager, socket, 20);
            if (binaryMessage != null) {
              // System.out.println("Received message from " +
              // IPAddress.toString(binaryMessage.getSourceAddress()));
              HTTPMessageObject httpMessage =
                  new HTTPMessageObject(
                      binaryMessage.getBodyAsString(), binaryMessage.getSourceAddress());

              // allow message processing by registered handler
              ((ISSDPMessageHandler) messageHandlerList.elementAt(i)).processMessage(httpMessage);
            } else {
              readUDP = false;
            }
          }
        }
      }
      if (udpPacketManager != null) {
        udpPacketManager.triggerEvents();
      }

      ThreadHelper.sleep(50);
    }
    System.out.println("  ExternalSSDPManagement thread was shut down");
    // close sockets
    terminated = true;
  }