Commit 80bb5e42 authored by KGreg's avatar KGreg
Browse files

Handling Observer and Client

parent e16b7298
......@@ -234,7 +234,7 @@ public class LobbyActivity extends AppCompatActivity implements ServerConnection
public void onConnect(int serverNumber) {
try {
if(servers.size() > serverNumber) {
String str = "Connect##"+CLIENT_NAME;
String str = "Connect##Client##"+CLIENT_NAME;
ServerModel sm = servers.get(serverNumber);
Socket socket = sm.getSocket();
ServerHolder.setServerModel(sm);
......@@ -254,7 +254,7 @@ public class LobbyActivity extends AppCompatActivity implements ServerConnection
public void onDisconnect() {
try {
if (ServerHolder.getSocket() != null) {
String str = "Disconnect##"+CLIENT_NAME;
String str = "Disconnect##Client"+CLIENT_NAME;
PrintWriter out = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(ServerHolder.getSocket().getOutputStream())), true);
out.println(str);
......
......@@ -41,6 +41,7 @@ public class LobbyActivity extends AppCompatActivity {
Handler updateConversationHandler;
private ArrayList<ClientModel> clients;
private ArrayList<ClientModel> observers;
private RecyclerView recyclerView;
private ClientListAdapter adapter;
......@@ -62,6 +63,7 @@ public class LobbyActivity extends AppCompatActivity {
setContentView(R.layout.activity_lobby);
clients = new ArrayList<ClientModel>();
observers = new ArrayList<ClientModel>();
allclients = new ArrayList<ClientModel>();
......@@ -277,21 +279,57 @@ public class LobbyActivity extends AppCompatActivity {
break;
case "Connect":
boolean van = false;
for (ClientModel client : clients) {
if (client.getClientsocket() == clientSocket) {
van = true;
break;
if(params.length>1)
{
if(params[1].equals("Observer"))
{
for (ClientModel observer : observers) {
if (observer.getClientsocket() == clientSocket) {
van = true;
break;
}
}
}
if(params[1].equals("Client"))
{
for (ClientModel client : clients) {
if (client.getClientsocket() == clientSocket) {
van = true;
break;
}
}
}
else
{
//hiba
}
}
if (!van) {
String cname = "Unknown";
if (params.length > 2) {
cname = params[2];
}
if (params.length > 1) {
cname = params[1];
if (params[1].equals("Observer")) {
observers.add(new ClientModel(cname, clientSocket));
// TODO: 2018. 04. 30. Add observer
}
if (params[1].equals("Client")) {
clients.add(new ClientModel(cname, clientSocket));
updateConversationHandler.post(new updateUIThread(Operation.ADD_CLIENT, ""));
}
}
else
{
//hiba
}
clients.add(new ClientModel(cname, clientSocket));
updateConversationHandler.post(new updateUIThread(Operation.ADD_CLIENT, ""));
}
out.println("Connect##Ok!");
break;
......@@ -311,16 +349,42 @@ public class LobbyActivity extends AppCompatActivity {
};
break;
case "Disconnect":
for (ClientModel client : clients) {
if (client.getClientsocket() == clientSocket) {
int position = clients.indexOf(client);
clients.remove(client);
updateConversationHandler.post(new updateUIThread(Operation.REMOVE_CLIENT, Integer.toString(position)));
break;
if(params.length>1)
{
if(params[1].equals("Observer"))
{
for (ClientModel observer : observers) {
if (observer.getClientsocket() == clientSocket) {
int position = observers.indexOf(observer);
observers.remove(observer);
// TODO: 2018. 04. 30. Remove Observer
break;
}
};
}
if(params[1].equals("Client"))
{
for (ClientModel client : clients) {
if (client.getClientsocket() == clientSocket) {
int position = clients.indexOf(client);
clients.remove(client);
updateConversationHandler.post(new updateUIThread(Operation.REMOVE_CLIENT, Integer.toString(position)));
break;
}
};
}
else
{
//hiba
}
}
else
{
//hiba
}
};
break;
default:
out.println("Unknown Message!");
......
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