public static void main(String[] args) { int id = -1, port = -1; String host = ""; // Parse host and port of the middleware and id of the client. try { host = args[0]; port = Integer.parseInt(args[1]); id = Integer.parseInt(args[2]); } catch (NumberFormatException | ArrayIndexOutOfBoundsException e) { logger.error("Usage: java -jar Client <host> <port> <client-id>"); System.exit(1); } // Try to open a connection to the middleware try { socket = new Socket(host, port); oos = new ObjectOutputStream(new BufferedOutputStream(socket.getOutputStream())); oos.flush(); ois = new ObjectInputStream(new BufferedInputStream(socket.getInputStream())); } catch (IOException e) { logger.error("Error occurred while connecting to the middleware."); } // Initialize scanner to parse user input sc = new Scanner(System.in); // Initialize a map with different command handlers Map<String, CommandBuilder> commandsMap = initializeCommands(); while (sc.hasNextLine()) { try { // Build a command read from standard input String commandArgs[] = sc.nextLine().split("\\s+"); Command command = commandsMap.get(commandArgs[0] + commandArgs[1]).createCommand(sc, id, commandArgs); // Send the command to the middleware oos.writeUnshared(command); oos.flush(); // Read the response and calculate the elapsed time long start = System.currentTimeMillis(); String response = (String) ois.readUnshared(); long end = System.currentTimeMillis(); logger.info(response + " Responsetime: " + (end - start) + "ms"); } catch (ArrayIndexOutOfBoundsException | NumberFormatException | NullPointerException | NoSuchElementException | IllegalStateException e) { logger.error("Invalid command"); } catch (IOException | ClassNotFoundException e) { logger.error("Connection error"); } } close(); }
@Override public Object read(final ObjectDataInput in) throws IOException { final ObjectInputStream objectInputStream; final InputStream inputStream = (InputStream) in; if (gzipEnabled) { objectInputStream = newObjectInputStream(in.getClassLoader(), new GZIPInputStream(inputStream)); } else { objectInputStream = newObjectInputStream(in.getClassLoader(), inputStream); } final Object result; try { if (shared) { result = objectInputStream.readObject(); } else { result = objectInputStream.readUnshared(); } } catch (ClassNotFoundException e) { throw new HazelcastSerializationException(e); } return result; }
public synchronized void run() { int messageType; String buf; PaintMessage paint; int j, k, num, tmp; boolean state = true; try { readyState[id - 1] = 0; // 进入等待房间 broadcast(4, players); // 当前客户端叫什么名字 /*remoteOut.writeInt(12); remoteOut.flush(); remoteOut.writeUnshared(name);*/ // 大家的名字和等待状态 /*broadcast(4,players); broadcast(9,readyState);*/ while (state) { messageType = remoteIn.readInt(); switch (messageType) { case 1: // 画板上画的像素点 { paint = (PaintMessage) remoteIn.readUnshared(); broadcast(1, paint); break; } case 2: // 聊天框中内容 { buf = (String) remoteIn.readUnshared(); if (buf.equals(title.getKey() + "\n") && id != title.getId() && right[id - 1] == 0) { System.out.println(port + ":" + buf); buf = "(**回答正确**)\n"; right[id - 1] = 1; if (first == 0) { // 第一个人答对 scores[id - 1] += 2; scores[title.getId() - 1] += 3; first = 1; } else { // 非第一个答对的,画的与猜的各加1分 scores[id - 1] += 1; scores[title.getId() - 1] += 1; } // 要重发成绩 broadcast(11, scores); } else if (buf.equals(title.getKey() + "\n") && id != title.getId() && right[id - 1] == 1) buf = "(*@#$*/%(*!)\n"; if (buf.equals(title.getKey() + "\n") && id == title.getId()) buf = "(*@#$*/%(*!)\n"; SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss"); buf = "[" + df.format(new Date()) + "]" + players[id - 1] + ":" + buf; broadcast(2, buf); for (j = 0; j <= 5; j++) { if (readyState[j] != -1 && right[j] == 0 && j != (title.getId() - 1)) break; } if (j > 5) // 大家都已经答对了 countOver = true; break; } case 5: // 清屏 broadcast(5); break; case 7: // 等待界面,当前用户准备 { readyState[id - 1] = 1; // 检测是否能开始 for (j = 0, num = 0, k = 0; j <= 5; j++) { if (readyState[j] != -1) num++; // 统计等待房间内人数 if (readyState[j] == 0) k++; // 统计没准备的人数 } if (num >= 2 && k == 0) // 可以开始了 readyState[6] = 1; // 传一下准备状态 broadcast(9, readyState); if (readyState[6] == 1) { // 大家的名字 broadcast(4, players); System.out.println("游戏开始!"); CountDownSem.release(); // 开始倒计时 } break; } case 8: // 等待界面,当前用户退出 { readyState[id - 1] = -1; players[id - 1] = "虚位以待"; System.out.println("端口号" + port + "的玩家退出了房间"); state = false; broadcast(4, players); clients.removeElement(remoteOut); // 再检测一下是否符合开始条件 for (j = 0, num = 0, k = 0; j <= 5; j++) { if (readyState[j] != -1) num++; // 统计等待房间内人数 if (readyState[j] == 0) k++; // 统计没准备的人数 } if (num >= 2 && k == 0) // 可以开始了 readyState[6] = 1; // 传一下准备状态 broadcast(9, readyState); if (readyState[6] == 1) { // 大家的名字 broadcast(4, players); System.out.println("游戏开始!"); CountDownSem.release(); // 开始倒计时 } break; } case 12: // 传入用户输入的昵称 name = (String) remoteIn.readUnshared(); players[id - 1] = name; broadcast(4, players); broadcast(9, readyState); break; // case n+1 } } } catch (Exception e) { System.out.println(e.getMessage() + ": 端口为" + port + "的玩家退出了游戏。。"); players[id - 1] = "虚位以待"; readyState[id - 1] = -1; scores[id - 1] = 0; for (tmp = 0; tmp <= 5; tmp++) { // 判断是不是最后一个人 if (readyState[tmp] != -1) break; } if (tmp > 5) readyState[6] = 0; String s = name + "离开了房间。。。\n"; broadcast(2, s); broadcast(4, players); broadcast(11, scores); clients.removeElement(remoteOut); if (id == title.getId()) // 画的人退出了 countOver = true; } }
public Node read_from_file(String key, int end_int) throws IOException, ClassNotFoundException { String key_new = null; Random rn = new Random(); // System.out.println("I am here" + key); /* if(key.contains("check")){ key_new = "Node_" + Integer.toString(this.counter) + "check"; } if (key.contains("Maps/rec")){ //new_key = "Maps/rec1Node#" + end_int; key_new = key; //"Maps/rec1Node#10";// + Integer.toString(this.counter); } */ File f = new File(key); FileInputStream fs = new FileInputStream(f.getAbsolutePath()); ObjectInputStream is = new ObjectInputStream(fs); Node node = (Node) is.readUnshared(); fs.close(); is.close(); // System.out.println("Node ID from read" + node.getNode_id()); /* for(int i = 1;i < 15; i++){ String key_new; key_new = "Data/Node_" + Integer.toString(i) + "check"; File f = new File(key_new); System.out.println(f.getAbsolutePath()); BasicFileAttributes attr = null; Path path = Paths.get(f.getAbsolutePath()); attr = Files.readAttributes(path, BasicFileAttributes.class); Object fileKey = attr.fileKey(); String s = fileKey.toString(); String inode = s.substring(s.indexOf("ino=") + 4, s.indexOf(")")); System.out.println("Inode: " + inode); fs = new FileInputStream(f.getAbsolutePath()); System.out.println(fs.getFD()); is = new ObjectInputStream(fs); node[i] = (Node) is.readUnshared(); System.out.println("Node ID from read" + node[i].getNode_id()); fs.close(); is.close(); } */ is.close(); return node; }
private void readObject(java.io.ObjectInputStream in) throws java.io.IOException, ClassNotFoundException { apdu = (byte[]) in.readUnshared(); // initialize transient fields parse(); }