Commit b5eb0fe4 authored by KGreg's avatar KGreg
Browse files

Observer Proto Connect & Disconnect

parent ae98b0a5
......@@ -8,11 +8,14 @@ import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;
import javax.jmdns.JmDNS;
import javax.jmdns.ServiceEvent;
import javax.jmdns.ServiceInfo;
import javax.jmdns.ServiceListener;
import javax.swing.SwingWorker;
import model.ServerModel;
......@@ -22,7 +25,7 @@ public class Main {
private String SERVICE_NAME = "ClientServer";
private String SERVICE_TYPE = "_http._tcp.";
private static String SERVER_SERVICE_NAME = "KvizServer";
private String CLIENT_NAME = "Unknown";
private static String CLIENT_NAME = "Unknown";
private boolean exit = true;
......@@ -36,6 +39,8 @@ public class Main {
private static ArrayList<ServerModel> servers;
private static ArrayList<Thread> threads;
private static PrintWriter out = null;
private static class SampleListener implements ServiceListener {
......@@ -103,8 +108,8 @@ public class Main {
System.out.println("Set The ObserverName:");
Scanner s = new Scanner(System.in);
hostname = s.nextLine();
s.close();
CLIENT_NAME = s.nextLine();
// Create a JmDNS instance
JmDNS jmdns = JmDNS.create(InetAddress.getLocalHost());
......@@ -115,12 +120,33 @@ public class Main {
jmdns.addServiceListener("_http._tcp.local.", new SampleListener());
// Wait a bit
Thread.sleep(30000);
Thread.sleep(3000);
String msg = "";
s = new Scanner(System.in);
System.out.println("Type 'Connect' to Connect:");
msg = s.nextLine();
if(msg.equals("Connect"))
{
onConnect(0);
}
System.out.println("Type 'Disconnect' to Disconnect:");
msg = s.nextLine();
if(msg.equals("Disconnect"))
{
onDisconnect();
}
} catch (UnknownHostException e) {
System.out.println(e.getMessage());
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
class CommunicationThread implements Runnable {
......@@ -214,4 +240,73 @@ public class Main {
}
}
public static void onConnect(int serverNumber) {
try {
if(servers.size() > serverNumber) {
String str = "Connect##Observer##"+CLIENT_NAME;
ServerModel sm = servers.get(serverNumber);
Socket socket = sm.getSocket();
ServerHolder.setServerModel(sm);
if (out == null) {
out = new PrintWriter(
new OutputStreamWriter(sm.getSocket().getOutputStream()), true);
}
m.new SendMessageToServerTask(str).execute();
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void onDisconnect() {
try {
if (ServerHolder.getSocket() != null) {
String str = "Disconnect##Observer##"+CLIENT_NAME;
if (out == null) {
out = new PrintWriter(
new OutputStreamWriter(ServerHolder.getSocket().getOutputStream()), true);
}
m.new SendMessageToServerTask(str).execute();
}
} catch (IOException e) {
e.printStackTrace();
}
}
private void SendConnectionLost(ServerModel server)
{
try {
PrintWriter out = new PrintWriter(
new OutputStreamWriter(server.getSocket().getOutputStream()), true);
out.println("ConnectionLost");
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private class SendMessageToServerTask extends SwingWorker {
String message;
public SendMessageToServerTask(String...params) {
super();
message = params[0];
}
@Override
protected Object doInBackground() throws Exception {
out.println(message);
return null;
}
}
}
\ No newline at end of file
import java.net.Socket;
import model.ServerModel;
public class ServerHolder {
private static ServerModel serverModel = null;
public static Socket getSocket() {
return serverModel.getSocket();
}
public static void setServerModel(ServerModel serverModel) {
ServerHolder.serverModel = serverModel;
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment