Commit 5fe59cec authored by KGreg's avatar KGreg
Browse files

ConnectionLost Mechanism & Exit

parent 925f2714
......@@ -35,6 +35,8 @@ public class LobbyActivity extends AppCompatActivity implements ServerConnection
private boolean discovering = false;
private String CLIENT_NAME = "Unknown";
private boolean exit = true;
private String hostname ="";
......@@ -87,11 +89,14 @@ public class LobbyActivity extends AppCompatActivity implements ServerConnection
mNsdManager.stopServiceDiscovery(mDiscoveryListener);
discovering = false;
/*
for (ServerModel server : servers) {
SendDisconnect(server);
if(ServerHolder.getSocket() != server.getSocket() || exit)
SendConnectionLost(server);
if(exit)
ServerHolder.setServerModel(null);
}
*/
for (Thread thread : threads) {
thread.interrupt();
......@@ -127,6 +132,7 @@ public class LobbyActivity extends AppCompatActivity implements ServerConnection
}
NsdManager.DiscoveryListener mDiscoveryListener = new NsdManager.DiscoveryListener() {
// Called as soon as service discovery begins.
......@@ -322,7 +328,7 @@ public class LobbyActivity extends AppCompatActivity implements ServerConnection
//nem sikerült
}
break;
case "Disconnect":
case "ConnectionLost":
for (ServerModel server: servers) {
if(server.getSocket() == serverModel.getSocket())
{
......@@ -337,21 +343,11 @@ public class LobbyActivity extends AppCompatActivity implements ServerConnection
break;
case "GameStarted":
exit = false;
Intent intent = new Intent(getApplicationContext(), GameActivity.class);
startActivity(intent);
break;
case "-1":
for (ServerModel server: servers) {
if(server.getSocket() == serverModel.getSocket())
{
int position = servers.indexOf(serverModel);
updateConversationHandler.post(new updateUIThread(Operation.REMOVE_SERVER, serverModel));
servers.remove(serverModel);
serverModel.getSocket().close();
threads.remove(Thread.currentThread());
Thread.currentThread().interrupt();
}
}
case "Disconnect":
break;
default:
break;
......@@ -413,13 +409,13 @@ public class LobbyActivity extends AppCompatActivity implements ServerConnection
recyclerView.setAdapter(adapter);
}
private void SendDisconnect(ServerModel server)
private void SendConnectionLost(ServerModel server)
{
try {
PrintWriter out = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(server.getSocket().getOutputStream())), true);
out.println("Disconnect");
out.println("ConnectionLost");
out.close();
} catch (IOException e) {
......
......@@ -61,6 +61,8 @@ public class LobbyActivity extends AppCompatActivity {
private Thread commThread;
private boolean exit = true;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
......@@ -100,6 +102,7 @@ public class LobbyActivity extends AppCompatActivity {
startGameBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
exit = false;
serverThread.interrupt();
commThread.interrupt();
ClientHolder.addAll(clients);
......@@ -112,26 +115,31 @@ public class LobbyActivity extends AppCompatActivity {
@Override
protected void onPause() {
/*try {
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}*/
}
if (mNsdManager != null && registred) {
mNsdManager.unregisterService(mRegistrationListener);
registred = false;
/*
for (ClientModel client : allclients) {
SendDisconnect(client);
boolean req = true;
for(ClientModel c : clients)
if(c.getClientsocket() == client.getClientsocket())
req = false;
if(req || exit)
SendConnectionLost(client);
}
*/
/*
for (Thread thread : threads) {
thread.interrupt();
}
*/
}
super.onPause();
......@@ -154,11 +162,11 @@ public class LobbyActivity extends AppCompatActivity {
mNsdManager.unregisterService(mRegistrationListener);
registred = false;
}
/*try {
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}*/
}
super.onDestroy();
}
......@@ -298,13 +306,14 @@ public class LobbyActivity extends AppCompatActivity {
}
out.println("Connect##Ok!");
break;
case "Disconnect":
case "ConnectionLost":
for (ClientModel client : clients) {
if (client.getClientsocket() == clientSocket) {
int position = clients.indexOf(client);
updateConversationHandler.post(new updateUIThread(Operation.REMOVE_CLIENT, Integer.toString(position)));
clients.remove(client);
clientSocket.close();
allclients.remove(client);
threads.remove(Thread.currentThread());
Thread.currentThread().interrupt();
......@@ -312,7 +321,18 @@ 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;
}
};
break;
default:
out.println("Unknown Message!");
......@@ -371,13 +391,13 @@ public class LobbyActivity extends AppCompatActivity {
recyclerView.setAdapter(adapter);
}
private void SendDisconnect(ClientModel client)
private void SendConnectionLost(ClientModel client)
{
try {
PrintWriter out = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(client.getClientsocket().getOutputStream())), true);
out.println("Disconnect");
out.println("ConnectionLost");
out.close();
} catch (IOException e) {
......
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