Ejemplo n.º 1
0
  @Override
  public void run() {
    PortMapping desiredMapping =
        new PortMapping(8888, SocketUtil.getLocalIP(), PortMapping.Protocol.UDP, "My Port Mapping");

    UpnpService upnpService = new UpnpServiceImpl(new PortMappingListener(desiredMapping));

    upnpService.getControlPoint().search();

    boolean disconnected = false;

    Response r = null;
    while (!disconnected) {
      try {
        r = SocketUtil._waitForResponse();
      } catch (IOException e) {
        e.printStackTrace();
      }

      InetAddress ip = r.ip;
      int socket_index = 0;
      boolean found = false;
      System.out.println("Message: ");
      System.out.println(Arrays.toString(r.data));
      System.out.println("Existing IPs: ");
      System.out.println(SocketUtil.otherIPs.toString());
      for (; socket_index < SocketUtil.otherIPs.size(); socket_index++) {
        if (SocketUtil.otherIPs.get(socket_index).equals(ip)) {
          found = true;
          break;
        }
      }

      if (found) {
        SocketUtil.fireIncomingMessageEvent(socket_index, r.data);
      } else {
        // We don't have this ip yet, probably a new player
        if (r.data[0].equals("connect")) {
          // brand new player, add their ip and send them the list
          // of other players
          try {
            String other_ips_string = Arrays.toString(SocketUtil.otherIPs.toArray());
            other_ips_string.replaceAll("\\[\\]", "");
            System.out.println("Sending other ip string: " + other_ips_string);
            SocketUtil._sendMessage(other_ips_string, r.ip);
          } catch (IOException e) {
            e.printStackTrace();
          }
          SocketUtil.otherIPs.add(r.ip);
          SocketUtil.firePlayerJoinedEvent();
        } else if (r.data[0].equals("join")) {
          // the connecting player has already received a player list
          // from someone else, just need to add their ip to our list
          SocketUtil.otherIPs.add(r.ip);
          SocketUtil.firePlayerJoinedEvent();
        }
      }
    }
  }
Ejemplo n.º 2
0
  public void run() {
    UpnpService upnpService = new UpnpServiceImpl();
    // Add a listener for device registration events
    upnpService.getRegistry().addListener(createRegistryListener(upnpService));

    // Broadcast a search message for all devices
    upnpService.getControlPoint().search(new STAllHeader());
  }
Ejemplo n.º 3
0
  void executeAction(UpnpService upnpService, Service wanip) {
    ActionInvocation addPortMappingInvocation = new AddPortMappingActionInvocation(wanip);
    // Executes asynchronous in the background
    upnpService
        .getControlPoint()
        .execute(
            new ActionCallback(addPortMappingInvocation) {
              public void success(ActionInvocation actionInvocation) {
                assert actionInvocation.getOutput().getValues().length == 0;
                System.out.println("Successfully called action!");
              }

              public void failure(ActionInvocation actionInvocation, UpnpResponse operation) {
                System.out.println("Failed action");
                System.err.println(createDefaultFailureMessage(actionInvocation, operation));
              }
            });
  }