public void newConnection(NIOSocket nioSocket) {
    nioSocket.setPacketReader(new AsciiLinePacketReader());
    nioSocket.setPacketWriter(new RawPacketWriter());

    nioSocket.listen(
        new SocketObserverAdapter() {
          public void packetReceived(NIOSocket socket, byte[] packet) {}
        });
  }
Exemple #2
0
  public static void main(String[] args) {
    try {
      // Start up the service.
      NIOService service = new NIOService();

      // Open our socket.
      socket = service.openSocket("localhost", 5217);

      // Use regular 1 byte header reader/writer
      socket.setPacketReader(new AsciiLinePacketReader());
      socket.setPacketWriter(new AsciiLinePacketWriter());

      // Start listening to the socket.
      socket.listen(
          new SocketObserver() {
            @Override
            public void connectionOpened(NIOSocket nioSocket) {
              System.out.println("connection opened");
            }

            @Override
            public void packetSent(NIOSocket socket, Object tag) {
              System.out.println("Packet sent");
            }

            @Override
            public void packetReceived(NIOSocket socket, byte[] packet) {
              try {
                String message = new String(packet).trim();
                try {
                  JSONObject j = new JSONObject(message);
                  request = Request.valueOf(j.optString("currentRequest"));
                  if (!cards.containsAll(
                      mapper.readValue(j.optString("cards"), new TypeReference<List<Card>>() {}))) {
                    cards =
                        mapper.readValue(j.optString("cards"), new TypeReference<List<Card>>() {});
                    System.out.println("Cards : " + cards);
                  }
                  switch (request) {
                    case Card:
                      System.out.println("Enter Card (1-4):");
                      requestNeeded();
                      break;
                    case Bid:
                      System.out.println("Enter Bid :");
                      requestNeeded();
                      break;
                    case Cards:
                      System.out.println("Enter Cards (1-12),(1-12),(1-12),(1-12):");
                      requestNeeded();
                      break;
                    case Null:
                      System.out.println(j.optString("currentMessage"));
                      break;
                    case Trump:
                      System.out.println("Enter Trump (1-4):");
                      requestNeeded();
                      break;
                    default:
                      break;
                  }
                } catch (Exception e) {
                  System.out.println(message);
                }
              } catch (Exception e) {
                e.printStackTrace();
              }
            }

            @Override
            public void connectionBroken(NIOSocket nioSocket, Exception exception) {
              System.out.println("Connection failed.");
              // Exit the program.
              System.exit(-1);
            }
          });
      // Read IO until process exits.
      while (true) {
        service.selectNonBlocking();
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }