@Override public void run() { // Wait for clients to connect. // When a client has connected, create a new ClientConnection Socket clientSock = null; channel.startServer(); while (true) { clientSock = channel.accept(); System.out.println("A new client has connected"); if (clientSock != null) { ClientConnection clientConn = new ClientConnection(clientSock, this, nextClientID); nextClientID++; Thread thread = new Thread(clientConn); thread.start(); clientSock = null; } System.out.println( "Client has been served by ClientHandler. " + "Now looking for new connections"); } }
/** * openConnections: open the connection to the master. has retry mechanism * * @return: true if the connection is built, false if failed */ public boolean openConnections() { _log("Try to connect to Master..."); int i = 0; while (true) { boolean isConnected = connect(); if (!isConnected) { i++; if (i == DropboxConstants.MAX_TRY) { break; } _log("Cannot connect to Master, retry " + i); try { Thread.sleep(DropboxConstants.TRY_CONNECT_MILLIS); } catch (InterruptedException e) { if (!_server.noException()) { _elog(e.toString()); } if (_server.debugMode()) { e.printStackTrace(); } _log("Retry connection is interrupted"); break; } } else { _log("Success!"); return true; } } _log("Failed"); return false; }
/** * Make contact with the Server who controls the game Players will need to know about the model * * @param model Of the game * @param cont Controller (MVC) of the Game */ public void makeContactWithServer(C_PongModel model, C_PongController cont) { // Also starts the Player task that get the current state // of the game from the server try { socket = new Socket("localhost", 50000); // try connect with socket at port 50000 on localhost NetObjectWriter writer = new NetObjectWriter(socket); // initialise writer with socket cont.setWriter(writer); // set writer in the controller Player Player = new Player(model, socket); // create new player using model and socket Thread thread = new Thread(Player); // create a thread with Player and start it thread.setPriority(Thread.NORM_PRIORITY); thread.start(); } catch (IOException e) { } }
/** listen: start to listen if there is any clients connected */ public void listen() { // Firstly spawn a new thread and then the main thread // also start listening _userNet = new DropboxFileServerUserNet(_server, _userOut, _userIn, _userSock); _userNet.start(); /* Use main thread, cannot be stopped */ while (_sock != null && !_sock.isClosed()) { try { String line = NetComm.receive(_in); _dlog(line); parse(line); } catch (Exception e) { if (!_server.noException()) { _elog(e.toString()); } break; // Break the loop } } /* Clear */ // Close main thread clear(); // Cancel the listening thread and retry _server.cancelListeningThread(); /* Also cancel all of the syncers */ _server.cancelSyncers(); // Retry after try { _log( "The connection to master is broken," + " reset everything and retry connection after " + DropboxConstants.TRY_CONNECT_MILLIS + " milliseconds"); Thread.sleep(DropboxConstants.TRY_CONNECT_MILLIS); } catch (InterruptedException e) { if (!_server.noException()) { _elog(e.toString()); } if (_server.debugMode()) { e.printStackTrace(); } } _server.run(); clear(); _log(_threadName + " is stopped"); }