/** Thread that will handle messages coming across the socket */ private void listenThread() { /* Note that the thread is starting */ Bugout.msg("Link " + address + ": THREAD START"); /* Now read in data from the socket and pass it to the host */ try { Message message; while (running && (message = socket.receive()) != null) { Bugout.msg("Link " + address + ": received: " + new MessagePointer(message)); host.receiveAnnouncement(message); } } catch (NetworkException e) { Bugout.err("Link " + address + ": " + e.getMessage()); } catch (IncorrectFormatException e) { Bugout.err( "Link " + address + ": received a message that is incorrectly formatted:" + e.getMessage()); } /* If we exit the while loop, remove and close this link */ host.removeLink(this); Bugout.msg("Link " + address + ": THREAD END"); stop(); }
/** Stop the thread. */ public void stop() { /* Note that we're stopping on the console */ Bugout.err("Link " + address + ": STOPPING"); /* Close the socket */ running = false; try { socket.close(); } catch (IOException e) { Bugout.err("Link " + address + ": while stopping: " + e.getMessage()); } }
/** Start the thread. */ public void start() { /* Note that we're starting on the console */ Bugout.msg("Link " + address + ": STARTING"); /* listenThread will stop immediately if not set here */ running = true; /* Start the listening thread */ Thread t = new Thread( new Runnable() { public void run() { listenThread(); } }); /* Establish the priority of this thread as one less than the host thread */ t.setPriority(Thread.currentThread().getPriority() + 1); t.start(); }