I am working on a client/server project which watches a system event in the server and sends a notification change to clients. It consists of a Watcher object which does the watching, and the server object which creates threads for each client connecting to it. Considering the simplified code below (I included all of the necessary code)
Now, the problem is that the notification is received from the mySrv
object and the propertyChange
of mySrv
is executed properly and disMsg
is populated. However, I don't know how to reach the run
method of the ClientHandler
to execute the notifyStatusChange()
to send out the change to clients. I tried joining threads and many different things but none worked. I would really appreciate if someone knows a better approach or point me to my mistakes and best practices perhaps) or show me specifically how to achieve that.
Thanks,
class MyServer implements PropertyChangeListener {
// all the fields and imports are removed for the sake of brevity
public class ClientHandler implements Runnable {
BufferedReader reader;
Socket socket;
InputStreamReader iReader;
public ClientHandler(Socket ClientSocket) {
try {
socket = ClientSocket;
iReader = new InputStreamReader(socket.getInputStream());
reader = new BufferedReader(iReader);
} catch (Exception ex) {
ex.printStackTrace();
}
}
public void run() {
String clientMessage;
try {
while (true) {
if (((clientMessage = reader.readLine()) != null)) {
System.out.println("Client: " + clientMessage);
}
if (isChanged) {
System.out.println("Server1: " + dispMsg);
notifyStatusChange();
isChanged = false;
}
}
} catch (IOException ex) {
Logger.getLogger(MyServer.class.getName()).log(Level.SEVERE, null, ex);
} catch (InterruptedException ex) {
Logger.getLogger(MyServer.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
public void goServer() {
try {
ss = new ServerSocket(1234);
while (true) {
s = ss.accept();
PrintWriter writer = new PrintWriter(s.getOutputStream(), true);
Thread t = new Thread(new ClientHandler((s)), "Thread for Client #" + counter);
t.start();
System.out.println("Connection " + counter + " received from: "
+ s.getInetAddress().getHostName());
counter++;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
ss.close();
} catch (IOException ex) {
Logger.getLogger(MyServer.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
public MyServer() {
isChanged = false;
if (dispMsg == null) {
dispMsg = "Waiting for a change...";
}
}
@Override
public void propertyChange(PropertyChangeEvent pce) {
isChanged = true;
dispMsg = "Event to String: " + pce.toString() + "\n"
+ "Property Name: " + pce.getPropertyName() + "\n"
+ "Old Value" + pce.getOldValue() + "\n";
}
public void notifyStatusChange() throws IOException {
PrintWriter pw = new PrintWriter(s.getOutputStream());
pw.println(dispMsg);
}
public static void main(String[] args) {
try {
Watcher myWatcher = new WatcherDir();
MyServer mySrv = new MyServer();
myWatcher.addPropertyChangeListener(mySrv);
myWatcher.start();
mySrv.goServer();
myWatcher.join();
} catch (IOException ex) {
Logger.getLogger(MyServer.class.getName()).log(Level.SEVERE, null, ex);
} catch (InterruptedException ex) {
Logger.getLogger(MyServer.class.getName()).log(Level.SEVERE, null, ex);
}
}
}