Ejemplo n.º 1
0
    public void run() {
      try {
        Vector<String> res = new Vector<String>();

        System.out.println("Received answer from a service ");
        boolean read = true;
        boolean jump = false;
        // char temp=(char)buf[0];
        res.add("SOURCE: " + receivePacket.getSocketAddress());

        StringBuilder sb = new StringBuilder(buf.length);
        for (byte b : buf) {
          if (jump) // salta (char)0x0A
          jump = false;
          else {
            if (b == 0) {
              read = false;
              break;
            }
            if ((char) b != (char) (0x0D)) sb.append((char) b);
            else {
              jump = true;
              res.add(sb.toString());
              sb = new StringBuilder(buf.length);
            }
          }
        }

        Vector<String> mess;
        String serviceAdd = null;
        int servicePort = -1;
        int masterPort = -1;

        String uuid = null;
        BoundReceiveSocket masterSocket = null;

        for (String line : res) {
          if (line.contains("uuid")) {
            String[] peace = line.split("uuid:");
            uuid = peace[1].substring(0, 36);
          }
          if (line.contains("LOCATION")) {
            String[] peace;
            if (line.charAt(9) == (char) 0x20) peace = line.split("LOCATION: http://");
            else peace = line.split("LOCATION:http://");

            peace = peace[1].split("/");
            peace = peace[0].split(":");
            serviceAdd = peace[0];

            servicePort = Integer.parseInt(peace[1]);
          }
          if (uuid != null && serviceAdd != null && servicePort > -1) {

            break;
          }
        }
        UPnPSOAPLocalServiceHandler temp = null;
        synchronized (UPnPmanager.lockNewLocalService) {
          temp = UPnPmanager.getLocalService(uuid);
          if (temp == null) {
            try {
              masterSocket = E2EComm.bindPreReceive(E2EComm.TCP);
              masterPort = masterSocket.getLocalPort();
              // sendService(msg);
              UPnPSOAPLocalServiceHandler handler =
                  new UPnPSOAPLocalServiceHandler(uuid, masterSocket);
              UPnPmanager.addLocalServiceHandler(handler);
              handler.start();
            } catch (Exception e) {
              e.printStackTrace();
            }
          }
          if (temp != null) {
            masterSocket = temp.getMasterSocket();
            masterPort = masterSocket.getLocalPort();
          }
        }

        sendService(msg, traversedNode, serviceAdd, servicePort, masterPort, uuid, buf);

      } catch (Exception e) {
        e.printStackTrace();
      }
    }