public void commandFromConsole(String command) { if (command.isEmpty()) return; String[] cmdArray = command.split(", "); if (cmdArray.length < 1) return; CommandType type = CommandType.None; try { type = CommandType.valueOf(cmdArray[0]); } catch (NullPointerException | IllegalArgumentException e) { LOG.error("unknown command : {}", e.getMessage()); } switch (type) { case GetDeviceList: parent.setCommandFromUnit(new GetDeviceListCommand()); break; case Help: viewHelp(); break; case Exit: parent.setCommandFromUnit(new ExitCommand()); break; case SetDeviceParameter: setDeviceParameter(cmdArray); break; default: } }
public static void main(String[] args) { if (args.length < 4) { System.err.println( "Bad parameters: Paramters(s): <server (name or Ip address)> <port> <Op> <Payload>"); return; } InetAddress server; int port; JackMessageOpType op; String payload; try { server = InetAddress.getByName(args[0]); port = Integer.parseInt(args[1]); if (!NamePortPair.isValidPort(port)) { System.err.println("Bad parameters: port must be an integer 1-65535"); return; } if (args[2].length() != 1) { System.err.println("Bad parameters: op must be a single character"); return; } op = JackMessageOpType.getTypeByOpCode(args[2].charAt(0)); if (op == null) { System.err.println("Bad parameters: " + args[2].charAt(0) + " is not a valid op"); return; } payload = args[3]; } catch (NumberFormatException e) { System.err.println("Bad parameters: could not parse port"); return; } catch (UnknownHostException e) { System.err.println("Communication problem: unable to find server"); return; } DatagramSocket clientSocket; try { clientSocket = new DatagramSocket(); clientSocket.connect(server, port); clientSocket.setSoTimeout(RESPONSE_WAIT_TIMEOUT); } catch (IOException e) { System.err.println( "Communication problem: could connect to " + server.getHostAddress() + ":" + port); e.printStackTrace(); return; } Message msgToSend; DatagramPacket toSend; try { String msgString = (op.getOpCode() + " " + payload); msgToSend = Message.decode(msgString.getBytes(StandardCharsets.US_ASCII)); toSend = new DatagramPacket(msgToSend.encode(), msgToSend.encode().length); clientSocket.send(toSend); } catch (NullPointerException | IllegalArgumentException e) { System.err.println("Bad parameters: invalid payload " + e.getMessage()); terminateSocket(clientSocket); return; } catch (IOException e) { System.err.println("Communication problem: send"); terminateSocket(clientSocket); return; } int trasmitAttemptCount = 0; boolean recievedResponse = false; DatagramPacket recievedPacket = new DatagramPacket(new byte[MAX_PACKET_LENGTH], MAX_PACKET_LENGTH); do { trasmitAttemptCount++; try { clientSocket.receive(recievedPacket); if (!recievedPacket.getAddress().equals(server)) { throw new IOException("Received packet from an unknown source"); } } catch (SocketTimeoutException e) { try { clientSocket.send(toSend); } catch (IOException e1) { System.err.println("Communication problem: send"); terminateSocket(clientSocket); return; } continue; } catch (IOException e) { System.err.println("Communication problem: unknown"); terminateSocket(clientSocket); return; } Message recievedMessage; try { recievedMessage = Message.decode( Arrays.copyOfRange(recievedPacket.getData(), 0, recievedPacket.getLength())); } catch (NullPointerException | IllegalArgumentException e) { System.err.println("Invalid message: decode receive " + e.getMessage()); continue; } switch (recievedMessage.getOp()) { case QUERY: System.err.println("Unexpected message type"); continue; case NEW: System.err.println("Unexpected message type"); continue; case ERROR: System.err.println( "Error: " + ((bettercom.jack.serialization.Error) recievedMessage).getErrorMessage()); terminateSocket(clientSocket); return; case RESPONSE: if (msgToSend.getOp() == JackMessageOpType.QUERY) { System.out.println("Recieved: " + recievedMessage); recievedResponse = true; break; } else { System.err.println("Unexpected message type"); continue; } case ACK_NEW: if (msgToSend.getOp() == JackMessageOpType.NEW) { if (msgToSend.generatePayload().equals(recievedMessage.generatePayload())) { System.out.println("Recieved: " + recievedMessage); recievedResponse = true; break; } else { System.err.println("Unexpected ACK"); continue; } } else { System.err.println("Unexpected message type"); continue; } default: System.err.println("Invalid message: switch op"); continue; } } while ((!recievedResponse) && (trasmitAttemptCount < MAX_RETRY)); if (trasmitAttemptCount >= MAX_RETRY) { System.err.println("No response after 3 retransmissions"); } terminateSocket(clientSocket); }