Commit 7a4e67d3 authored by KGreg's avatar KGreg
Browse files

Disconnect mechanism

parent f9a90e91
......@@ -46,6 +46,8 @@ public class LobbyActivity extends AppCompatActivity implements ServerConnection
private ArrayList<ServerModel> servers;
private ArrayList<Thread> threads;
private RecyclerView recyclerView;
private ServerListAdapter adapter;
......@@ -56,6 +58,8 @@ public class LobbyActivity extends AppCompatActivity implements ServerConnection
servers = new ArrayList<ServerModel>();
threads = new ArrayList<Thread>();
mNsdManager = (NsdManager) getSystemService(Context.NSD_SERVICE);
mNsdManager.discoverServices(SERVICE_TYPE,
NsdManager.PROTOCOL_DNS_SD, mDiscoveryListener);
......@@ -81,6 +85,17 @@ public class LobbyActivity extends AppCompatActivity implements ServerConnection
if (mNsdManager != null && discovering) {
mNsdManager.stopServiceDiscovery(mDiscoveryListener);
discovering = false;
for (ServerModel server : servers) {
SendDisconnect(server);
}
for (Thread thread : threads) {
thread.interrupt();
}
}
super.onPause();
}
......@@ -94,6 +109,7 @@ public class LobbyActivity extends AppCompatActivity implements ServerConnection
SERVICE_TYPE, NsdManager.PROTOCOL_DNS_SD, mDiscoveryListener);
discovering = true;
}
}
@Override
......@@ -101,6 +117,8 @@ public class LobbyActivity extends AppCompatActivity implements ServerConnection
if (mNsdManager != null && discovering) {
mNsdManager.stopServiceDiscovery(mDiscoveryListener);
discovering = false;
}
super.onDestroy();
}
......@@ -141,15 +159,7 @@ public class LobbyActivity extends AppCompatActivity implements ServerConnection
// When the network service is no longer available.
// Internal bookkeeping code goes here.
Log.e(TAG, "service lost" + service);
//
for (ServerModel server: servers) {
if(server.getSocket().isClosed()) {
Log.d("fdsa", "bbbbbbbbbbbbbbbbbbb");
int position = servers.indexOf(server);
updateConversationHandler.post(new updateUIThread(Operation.REMOVE_SERVER, Integer.toString(position)));
servers.remove(server);
}
}
}
@Override
......@@ -212,7 +222,9 @@ public class LobbyActivity extends AppCompatActivity implements ServerConnection
});
CommunicationThread commThread = new CommunicationThread(serverModel);
new Thread(commThread).start();
Thread newthread = new Thread(commThread);
threads.add(newthread);
newthread.start();
} catch (IOException e) {
e.printStackTrace();
}
......@@ -307,7 +319,33 @@ public class LobbyActivity extends AppCompatActivity implements ServerConnection
}
break;
case "Disconnect":
//sikeres disconnect, vagy pedig a szerver csatlakozott le
for (ServerModel server: servers) {
if(server.getSocket() == serverModel.getSocket())
{
int position = servers.indexOf(serverModel);
updateConversationHandler.post(new updateUIThread(Operation.REMOVE_SERVER, Integer.toString(position)));
servers.remove(serverModel);
serverModel.getSocket().close();
threads.remove(Thread.currentThread());
Thread.currentThread().interrupt();
}
}
break;
case "-1":
for (ServerModel server: servers) {
if(server.getSocket() == serverModel.getSocket())
{
int position = servers.indexOf(serverModel);
updateConversationHandler.post(new updateUIThread(Operation.REMOVE_SERVER, Integer.toString(position)));
servers.remove(serverModel);
serverModel.getSocket().close();
threads.remove(Thread.currentThread());
Thread.currentThread().interrupt();
}
}
break;
default:
break;
}
}
......@@ -323,7 +361,8 @@ public class LobbyActivity extends AppCompatActivity implements ServerConnection
}
}
} catch (IOException e) {
e.printStackTrace();
Thread.currentThread().interrupt();
threads.remove(Thread.currentThread());
}
}
}
......@@ -366,6 +405,20 @@ public class LobbyActivity extends AppCompatActivity implements ServerConnection
recyclerView.setAdapter(adapter);
}
private void SendDisconnect(ServerModel server)
{
try {
PrintWriter out = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(server.getSocket().getOutputStream())), true);
out.println("Disconnect");
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
enum Operation {
......
......@@ -55,6 +55,11 @@ public class LobbyActivity extends AppCompatActivity {
private String questionFileName;
private ArrayList<Thread> threads;
private ArrayList<ClientModel> allclients;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
......@@ -62,6 +67,11 @@ public class LobbyActivity extends AppCompatActivity {
setContentView(R.layout.activity_lobby);
clients = new ArrayList<ClientModel>();
allclients = new ArrayList<ClientModel>();
threads = new ArrayList<Thread>();
//-------NSD-----//
mNsdManager = (NsdManager) getSystemService(Context.NSD_SERVICE);
registerService(SERVERPORT);
......@@ -107,6 +117,17 @@ public class LobbyActivity extends AppCompatActivity {
if (mNsdManager != null && registred) {
mNsdManager.unregisterService(mRegistrationListener);
registred = false;
for (ClientModel client : allclients) {
SendDisconnect(client);
}
/*
for (Thread thread : threads) {
thread.interrupt();
}
*/
}
super.onPause();
}
......@@ -198,8 +219,12 @@ public class LobbyActivity extends AppCompatActivity {
socket = serverSocket.accept();
allclients.add(new ClientModel("Unknown", socket));
CommunicationThread commThread = new CommunicationThread(socket);
new Thread(commThread).start();
Thread newthread = new Thread(commThread);
threads.add(newthread);
newthread.start();
} catch (IOException e) {
e.printStackTrace();
......@@ -274,10 +299,13 @@ public class LobbyActivity extends AppCompatActivity {
int position = clients.indexOf(client);
updateConversationHandler.post(new updateUIThread(Operation.REMOVE_CLIENT, Integer.toString(position)));
clients.remove(client);
clientSocket.close();
threads.remove(Thread.currentThread());
Thread.currentThread().interrupt();
break;
}
}
out.println("Disconnect##Ok!");
};
break;
default:
......@@ -338,6 +366,20 @@ public class LobbyActivity extends AppCompatActivity {
recyclerView.setAdapter(adapter);
}
private void SendDisconnect(ClientModel client)
{
try {
PrintWriter out = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(client.getClientsocket().getOutputStream())), true);
out.println("Disconnect");
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
enum Operation {
......
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