edu.pdx.cs410J.net.ChatCommunicator Maven / Gradle / Ivy
The newest version!
package edu.pdx.cs410J.net;
import java.io.*;
import java.util.*;
import java.net.*;
/**
* A ChatCommunicator
obtains a Socket
and
* then creates a ChatSpeaker
and a
* ChatListener
that run in their own threads.
*/
public class ChatCommunicator implements Runnable {
private static PrintStream err = System.err;
private int port; // Where the socket is
private ChatSpeaker speaker; // Send messsages
private ChatListener listener; // Receives messages
/**
* Creates a new ChatCommunicator
on a given port.
*
*/
public ChatCommunicator(int port) {
this.port = port;
}
/**
* Starts up this ChatCommunicator
.
*/
public void startup() {
this.speaker = new ChatSpeaker();
this.listener = new ChatListener();
(new Thread(this)).start();
}
/**
* Make the connection to the socket. If it cannot open a
* Socket
, is starts a SocketServer
and
* waits for a connection. Then, start the speaker and listener.
*/
public void run() {
// Attempt to make a socket
Socket socket = null;
try {
socket = new Socket("localhost", port);
} catch (IOException ex) {
// Nobody listening
// System.out.println("Nobody's listening");
}
if (socket == null) {
// Listen
try {
ServerSocket server = new ServerSocket(port, 10);
socket = server.accept();
} catch (IOException ex) {
err.println("** IOException: " + ex);
System.exit(1);
}
}
this.speaker.setSocket(socket);
this.listener.setSocket(socket);
(new Thread(this.speaker)).start();
(new Thread(this.listener)).start();
}
/**
* Delegates to the ChatSpeaker
*/
public void sendMessage(ChatMessage message) {
this.speaker.sendMessage(message);
}
/**
* Gets messages from the ChatListener
*/
public List getMessages() {
return this.listener.getMessages();
}
}