Commit ae98b0a5 authored by KGreg's avatar KGreg
Browse files

Add Observers to Server Lobby

parent 7d55dd74
......@@ -259,7 +259,7 @@ public class LobbyActivity extends AppCompatActivity implements ServerConnection
public void onDisconnect() {
try {
if (ServerHolder.getSocket() != null) {
String str = "Disconnect##Client"+CLIENT_NAME;
String str = "Disconnect##Client##"+CLIENT_NAME;
if (out == null) {
out = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(ServerHolder.getSocket().getOutputStream())), true);
......
......@@ -46,6 +46,9 @@ public class LobbyActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private ClientListAdapter adapter;
private RecyclerView observerView;
private ClientListAdapter observeradapter;
private String gameMode;
private ArrayList<Thread> threads;
......@@ -143,6 +146,9 @@ public class LobbyActivity extends AppCompatActivity {
for(ClientModel c : clients)
if(c.getClientsocket() == client.getClientsocket())
req = false;
for(ClientModel o : observers)
if(o.getClientsocket() == client.getClientsocket())
req = false;
if(req || exit)
SendConnectionLost(client);
}
......@@ -316,7 +322,7 @@ public class LobbyActivity extends AppCompatActivity {
if (params.length > 1) {
if (params[1].equals("Observer")) {
observers.add(new ClientModel(cname, clientSocket));
// TODO: 2018. 04. 30. Add observer
updateConversationHandler.post(new updateUIThread(Operation.ADD_OBSERVER, ""));
}
if (params[1].equals("Client")) {
clients.add(new ClientModel(cname, clientSocket));
......@@ -347,6 +353,19 @@ public class LobbyActivity extends AppCompatActivity {
break;
}
};
for (ClientModel observer : observers) {
if (observer.getClientsocket() == clientSocket) {
int position = observers.indexOf(observer);
updateConversationHandler.post(new updateUIThread(Operation.REMOVE_OBSERVER, Integer.toString(position)));
observers.remove(observer);
clientSocket.close();
allclients.remove(observer);
threads.remove(Thread.currentThread());
Thread.currentThread().interrupt();
break;
}
};
break;
case "Disconnect":
if(params.length>1)
......@@ -357,7 +376,7 @@ public class LobbyActivity extends AppCompatActivity {
if (observer.getClientsocket() == clientSocket) {
int position = observers.indexOf(observer);
observers.remove(observer);
// TODO: 2018. 04. 30. Remove Observer
updateConversationHandler.post(new updateUIThread(Operation.REMOVE_OBSERVER, Integer.toString(position)));
break;
}
......@@ -429,6 +448,12 @@ public class LobbyActivity extends AppCompatActivity {
case REMOVE_CLIENT:
adapter.removeItem(Integer.parseInt(msg));
break;
case ADD_OBSERVER:
observeradapter.addItem();
break;
case REMOVE_OBSERVER:
observeradapter.removeItem((Integer.parseInt(msg)));
break;
default:
Log.d("updateUIThread", "Unknown operation");
break;
......@@ -442,6 +467,12 @@ public class LobbyActivity extends AppCompatActivity {
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
recyclerView.setAdapter(adapter);
observerView = (RecyclerView) findViewById(R.id.ObserverRecyclerView);
observeradapter = new ClientListAdapter(observers);
observerView.setLayoutManager(new LinearLayoutManager(this));
observerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
observerView.setAdapter(observeradapter);
}
private void SendConnectionLost(ClientModel client)
......
package onlab.kvizserver;
public enum Operation {
ADD_CLIENT, REMOVE_CLIENT;
ADD_CLIENT, REMOVE_CLIENT, ADD_OBSERVER, REMOVE_OBSERVER;
}
......@@ -9,7 +9,8 @@
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
android:orientation="vertical"
android:layout_marginLeft="5dp">
<LinearLayout
android:layout_width="match_parent"
......@@ -41,15 +42,56 @@
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/MainRecyclerView"
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
/>
android:orientation="horizontal"
android:weightSum="2">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Clients:"
android:textStyle="bold"/>
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/MainRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Observers:"
android:textStyle="bold"/>
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/ObserverRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
/>
</LinearLayout>
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
</LinearLayout>
......
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