示例#1
0
 // Method for handling a client who just completed the handshake process
 private static void handleClient(HandshakeCompletedEvent event) {
   // Notify
   System.out.println(
       Color.BLUE
           + "NOTICE: "
           + Color.RESET
           + "HID Client connected from "
           + event.getSocket().toString());
   // Create output
   hidClient = event.getSocket();
   // Display
   Display.readyMessage = "HID Mode Active";
   Display.ready();
   // Try to beep a tune
   try {
     Buzzer.beep();
     Thread.sleep(20);
     Buzzer.beep();
   } catch (Exception e) {
   }
   // Try to get the output stream
   try {
     hidOut = hidClient.getOutputStream();
   } catch (Exception e) {
     // Something went wrong
     System.out.println("HID client exception: " + e.getMessage());
   }
   // Connected
   connected = true;
 }
示例#2
0
 // Server loop
 private static void serverLoop() throws Exception {
   // Check if client is connected
   if (hidOut == null && !busy) {
     // Busy
     busy = true;
     // Notify
     // System.out.println("Awaiting HID client...");
     // No client is connected - accept connection
     SSLSocket client = (SSLSocket) server.accept();
     // Require auth
     // client.setNeedClientAuth(true);
     // Enable ciphers
     client.setEnabledCipherSuites(client.getEnabledCipherSuites());
     // Add a handshake listener
     client.addHandshakeCompletedListener(
         new HandshakeCompletedListener() {
           @Override
           public void handshakeCompleted(HandshakeCompletedEvent event) {
             handleClient(event);
           }
         });
     // Start handshake
     client.startHandshake();
   } else {
     // Check client status
     try {
       // System.out.print("Checking HID client status...");
       if (!connected) {
         // System.out.println(Color.YELLOW + " Connecting..." +
         //		Color.RESET);
         return;
       }
       // Send a ping to the client
       hidOut.write(0x01);
       // System.out.println(Color.GREEN + " Connected!" + Color.RESET);
       Thread.sleep(1000);
     } catch (Exception e) {
       // System.out.println(Color.RED + " Not Connected!" + Color.RESET);
       // Reset variables
       hidClient = null;
       hidOut = null;
       busy = false;
       connected = false;
       // Reset display
       Display.readyMessage = Display.defaultReadyMessage;
       Display.ready();
       // Notify
       System.out.println(Color.BLUE + "NOTICE: " + Color.RESET + "HID client has disconnected");
     }
   }
 }