public void run() { System.out.println("Server Thread " + ID + " running."); while (true) { try { server.handle(ID, streamIn.readUTF()); } catch (IOException ioe) { System.out.println(ID + " ERROR reading: " + ioe.getMessage()); server.remove(ID); stop(); } } }
/** * Runs the chat master server. * * @param args Command line arguments */ public static void main(String[] args) { ChatServer server = null; if (args.length > 0) { try { server = new ChatServerImpl(Integer.parseInt(args[0])); } catch (NumberFormatException e) { printHelp(); System.exit(1); } } else { server = new ChatServerImpl(DEFAULT_PORT); } server.startServer(); }
public void run() { try { // Create character streams for the socket. in = new BufferedReader(new InputStreamReader(socket.getInputStream())); out = new PrintWriter(socket.getOutputStream(), true); // goes until ok name while (true) { name = in.readLine(); if (name == null) { return; } if (name.startsWith("TESTLOG:")) { name = name.substring(8); synchronized (server) { if (!server.checkName(name)) { server.addClient(name, in, out); out.println("OK"); break; } else { out.println("FAIL"); } } } } // to UI out.println("Connected as: " + name); // switch to broadcasting mode while (true) { String input = in.readLine(); if (input == null) { return; } server.broadcastMessage(name, input); } } catch (IOException e) { System.out.println(e); } finally { try { socket.close(); } catch (IOException e) { } } }
public boolean execute(ChatClient sender, String[] args) { if (args.length < 2) { sender.serverChat(usage()); return true; } String oldName = ChatPrefs.getName(); if (args[1].compareTo(oldName) == 0) { sender.sendChat("That is already my name!"); return true; } ChatPrefs.setName(args[1]); List<ChatClient> clients = ChatServer.getClients(); synchronized (clients) { for (ChatClient cl : clients) { cl.sendNameChange(args[1]); } } return true; }
@Override public void run() { try { while (true) { clientMessage = this.nodeInput.readUTF(); if (clientMessage.equals("chatClient-hello")) { this.nodeOutput.writeUTF("chat-request_name_id"); this.nodeName = nodeInput.readUTF(); this.nodeID = nodeInput.readInt(); System.out.println("ChatClient: " + this.nodeName + " with ID: " + this.nodeID); } else if (clientMessage.equals("chatClient-sending_message")) { nodes = ChatServer.getNodes(); messageToBeSent = nodeInput.readUTF(); for (int i = 0; i < 4; i++) { this.nodes.get(i).nodeOutput.writeUTF("chat-sending_message"); this.nodes.get(i).nodeOutput.writeUTF(this.nodeName + " says: " + messageToBeSent); } System.out.println("Message has been sent: " + messageToBeSent); } } // while } catch (IOException ex) { System.err.println("Error in ChatNode"); ex.printStackTrace(); } } // run()
/** * @param sc * @param d * @throws IOException */ public ChatServerConnection(SocketChannel sc, Dispatcher d, CsPacketHandler csPacketHandler) throws IOException { super(sc, d, 8192 * 2, 8192 * 2); this.chatServer = ChatServer.getInstance(); this.csPacketHandler = csPacketHandler; state = State.CONNECTED; log.info("Connected to ChatServer!"); }
/** * Write all full messages in our buffer to the other clients * * @param client the client to read messages from */ @Override public void beforeRead(Client client) { // Check if we have any messages buffered and send them String message = client.nextMessage(); while (message != null) { chatServer.writeMessageToClients(client, message); message = client.nextMessage(); } }
public void send(String msg) { try { streamOut.writeUTF(msg); streamOut.flush(); } catch (IOException ioe) { System.out.println(ID + " ERROR sending: " + ioe.getMessage()); server.remove(ID); stop(); } }
/** * Bootstraps the chat server * * @param args Optional port number as command line argument */ public static void main(String[] args) { int port = DEFAULT_PORT; try { if (args.length > 0) { port = Integer.parseInt(args[0]); } ChatServer chatServer = new ChatServer(); chatServer.start(port); } catch (NumberFormatException ex) { System.out.println("Usage: java ChatServer [port_number]"); System.exit(1); } }
public void disconnect() { server.logoff(username); try { TransportObject disconnAck = new TransportObject(Command.disconnect, ServerReply.OK); queueReply(disconnAck); mySocket.close(); } catch (IOException e) { System.err.print(e); } }
public void processCommand() { TransportObject recv = null; try { recv = (TransportObject) received.readObject(); } catch (Exception e) { disconnect(); } if (recv == null) disconnect(); else if (recv.getCommand() == Command.disconnect) disconnect(); else if (recv.getCommand() == Command.login) { TransportObject send = new TransportObject(Command.login, ServerReply.REJECTED); queueReply(send); } else if (recv.getCommand() == Command.logout) { server.logoff(username); try { server.startNewTimer(new SocketParams(mySocket, received, sent)); } catch (IOException e) { e.printStackTrace(); } } else if (recv.getCommand() == Command.readlog) { try { server.readlog(username); } catch (SQLException e) { e.printStackTrace(); } } else if (recv.getCommand() == Command.join) server.joinGroup(this, recv.getGname()); else if (recv.getCommand() == Command.leave) server.leaveGroup(this, recv.getGname()); else if (recv.getCommand() == Command.send) send(recv.getDest(), recv.getMessage(), recv.getSQN()); else if (recv.getCommand() == Command.rtt) { try { DBHandler.addRTT(recv.getRTT(), username); } catch (SQLException e) { e.printStackTrace(); } } }
public void run() { while (!loggedOff) { // sendLock.writeLock().lock(); // if (!toSend.isEmpty()) { MessageJob msgJob = null; try { msgJob = toSend.poll(3, TimeUnit.SECONDS); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } if (msgJob != null) { MsgSendError msgStatus = server.processMessage(username, msgJob.dest, msgJob.msg, msgJob.sqn, msgJob.timestamp); sendClientAck(msgStatus, msgJob); } // } // sendLock.writeLock().unlock(); } sendLock.writeLock().lock(); while (!toSend.isEmpty()) { MessageJob msgJob = toSend.poll(); String formattedMsg = username + " " + msgJob.dest + " " + System.currentTimeMillis() / 1000 + "\t" + msgJob.sqn; TestChatServer.logChatServerDropMsg(formattedMsg, new Date()); } sendLock.writeLock().unlock(); groupsJoined.clear(); TestChatServer.logUserLogout(username, new Date()); }
public void logoff() { pendingLogoff = true; logoffAck(); while (!queuedServerReplies.isEmpty()) { TransportObject reply = null; try { reply = queuedServerReplies.poll(); if (reply != null) sent.writeObject(reply); } catch (SocketException e) { System.err.println(e); } catch (Exception e) { if (reply != null && reply.getCommand().equals(Command.send)) { User sender = (User) server.getUser(reply.getSender()); if (sender != null) { TransportObject error = new TransportObject(ServerReply.sendack, reply.getSQN()); sender.queueReply(error); } } } } loggedOff = true; }
@Override protected final void onDisconnect() { chatServer.chatServerDown(); }