public void actionPerformed(ObjectOutputStream oos, Message msg) throws EventHandleException { try { /* get the handler of the ServerPeer */ ServerPeer serverpeer = (ServerPeer) gui.peer(); /* get the message body */ SPLeaveReplacementBody body = (SPLeaveReplacementBody) msg.getBody(); TreeNode treeNode = body.getTreeNode(); treeNode.setContent(body.getContent()); SPGeneralAction.saveData(body.getContent().getData()); treeNode.setStatus(TreeNode.ACTIVE); treeNode.setRole(TreeNode.MASTER); treeNode.addCoOwnerList(body.getPhysicalSender()); serverpeer.addListItem(treeNode); SPGeneralAction.updateRotateRoutingTable(serverpeer, treeNode); serverpeer.setActivateStablePosition( new ActivateStablePosition(serverpeer, treeNode, ServerPeer.TIME_TO_STABLE_POSITION)); ((ServerGUI) gui).updatePane(treeNode); } catch (Exception e) { e.printStackTrace(); throw new EventHandleException( "Replace a super peer's position failure when it leaves network", e); } }
/* * to broadcast a message to all Clients */ private synchronized void broadcast(String message) { // add HH:mm:ss and \n to the message String time = sdf.format(new Date()); String messageLf = time + " " + message + "\n"; // display message on console or GUI if (sg == null) System.out.print(messageLf); else sg.appendRoom(messageLf); // append in the room window // we loop in reverse order in case we would have to remove a Client // because it has disconnected for (int i = al.size(); --i >= 0; ) { ClientThread ct = al.get(i); // try to write to the Client if it fails remove it from the list if (!ct.writeMsg(messageLf)) { al.remove(i); display("Disconnected Client " + ct.username + " removed from list."); } } }
/* * Display an event (not a message) to the console or the GUI */ private void display(String msg) { String time = sdf.format(new Date()) + " " + msg; if (sg == null) System.out.println(time); else sg.appendEvent(time + "\n"); }