Commit 925f2714 authored by fpeterfalvi's avatar fpeterfalvi
Browse files

ServerModels and Clientmodels made global objects.

parent b7b8e32e
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="onlab.kvizclient">
<uses-permission
android:required="true"
android:name="android.permission.ACCESS_WIFI_STATE"/>
android:name="android.permission.ACCESS_WIFI_STATE"
android:required="true" />
<uses-permission
android:required="true"
android:name="android.permission.CHANGE_WIFI_STATE"/>
android:name="android.permission.CHANGE_WIFI_STATE"
android:required="true" />
<uses-permission
android:required="true"
android:name="android.permission.INTERNET"/>
android:name="android.permission.INTERNET"
android:required="true" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
......@@ -29,8 +31,9 @@
<activity
android:name=".LobbyActivity"
android:label="@string/title_activity_lobby"
android:theme="@style/AppTheme.NoActionBar"
android:screenOrientation="landscape"></activity>
android:screenOrientation="landscape"
android:theme="@style/AppTheme.NoActionBar" />
<activity android:name=".GameActivity"></activity>
</application>
</manifest>
\ No newline at end of file
package onlab.kvizclient;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import org.w3c.dom.Text;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.UnknownHostException;
public class GameActivity extends AppCompatActivity {
TextView questionText;
Handler updateConversationHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
questionText = (TextView) findViewById(R.id.QuestionTextView);
updateConversationHandler = new Handler();
CommunicationThread commThread = new CommunicationThread();
Thread newthread = new Thread(commThread);
newthread.start();
}
class CommunicationThread implements Runnable {
private BufferedReader input;
private PrintWriter output;
public CommunicationThread() {
try {
input = new BufferedReader(new InputStreamReader(ServerHolder.getSocket().getInputStream()));
output = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(ServerHolder.getSocket().getOutputStream())),true);
} catch (IOException e) {
e.printStackTrace();
}
}
public void run() {
while (!Thread.currentThread().isInterrupted()) {
try {
String read = input.readLine();
if (read != null) {
Log.d("GameActivity", "Szervertől kapott üzenet: " + read);
updateConversationHandler.post(new GameActivity.updateUIThread(read));
String params[] = read.split("##");
}
} catch (IOException e) {
Thread.currentThread().interrupt();
}
}
}
}
class updateUIThread implements Runnable {
String msg;
public updateUIThread(String msg) {
this.msg = msg;
}
@Override
public void run() {
questionText.setText(msg);
}
}
}
package onlab.kvizclient;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ServiceInfo;
import android.net.nsd.NsdManager;
import android.net.nsd.NsdServiceInfo;
......@@ -86,9 +87,11 @@ public class LobbyActivity extends AppCompatActivity implements ServerConnection
mNsdManager.stopServiceDiscovery(mDiscoveryListener);
discovering = false;
/*
for (ServerModel server : servers) {
SendDisconnect(server);
}
*/
for (Thread thread : threads) {
thread.interrupt();
......@@ -238,10 +241,11 @@ public class LobbyActivity extends AppCompatActivity implements ServerConnection
try {
if(servers.size() > serverNumber) {
String str = "Connect##"+CLIENT_NAME;
Socket socket = servers.get(serverNumber).getSocket();
SocketHolder.setSocket(socket);
ServerModel sm = servers.get(serverNumber);
Socket socket = sm.getSocket();
ServerHolder.setServerModel(sm);
PrintWriter out = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream())), true);
new OutputStreamWriter(sm.getSocket().getOutputStream())), true);
out.println(str);
}
} catch (UnknownHostException e) {
......@@ -255,10 +259,10 @@ public class LobbyActivity extends AppCompatActivity implements ServerConnection
public void onDisconnect() {
try {
if (SocketHolder.getSocket() != null) {
if (ServerHolder.getSocket() != null) {
String str = "Disconnect##"+CLIENT_NAME;
PrintWriter out = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(SocketHolder.getSocket().getOutputStream())), true);
new OutputStreamWriter(ServerHolder.getSocket().getOutputStream())), true);
out.println(str);
}
} catch (IOException e) {
......@@ -331,6 +335,10 @@ public class LobbyActivity extends AppCompatActivity implements ServerConnection
}
}
break;
case "GameStarted":
Intent intent = new Intent(getApplicationContext(), GameActivity.class);
startActivity(intent);
break;
case "-1":
for (ServerModel server: servers) {
......
package onlab.kvizclient;
import java.net.Socket;
import onlab.kvizclient.model.ServerModel;
public class ServerHolder {
private static ServerModel serverModel;
public static Socket getSocket() {
return serverModel.getSocket();
}
public static void setServerModel(ServerModel serverModel) {
ServerHolder.serverModel = serverModel;
}
}
package onlab.kvizclient;
import java.net.Socket;
public class SocketHolder {
private static Socket socket;
public static Socket getSocket() {
return socket;
}
public static void setSocket(Socket socket) {
SocketHolder.socket = socket;
}
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="onlab.kvizclient.GameActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/QuestionTextView"
android:text="Ide jön a kérdés"/>
</android.support.constraint.ConstraintLayout>
package onlab.kvizserver;
import java.util.ArrayList;
import java.util.List;
import onlab.kvizserver.model.ClientModel;
public class ClientHolder {
private static List<ClientModel> clients = new ArrayList<>();
public static void add(ClientModel clientModel) {
clients.add(clientModel);
}
public static void remove(ClientModel clientModel) {
clients.remove(clientModel);
}
public static void addAll(List<ClientModel> clients) {
ClientHolder.clients.addAll(clients);
}
public static ClientModel get(int index) {
return clients.get(index);
}
public static int size() {
return clients.size();
}
}
......@@ -9,20 +9,30 @@ import android.widget.Button;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import onlab.kvizserver.model.ClientModel;
import onlab.kvizserver.model.GuessQuestion;
public class GameActivity extends AppCompatActivity {
List<GuessQuestion> questions = new ArrayList<>();
private List<GuessQuestion> questions = new ArrayList<>();
private int questionIndex = 0;
private List<BufferedReader> inputs = new ArrayList<>();
private List<PrintWriter> outputs = new ArrayList<>();
int questionIndex = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
......@@ -47,11 +57,27 @@ public class GameActivity extends AppCompatActivity {
Button nextQuestionBtn = (Button) findViewById(R.id.NextQuestion);
final TextView questionText = (TextView) findViewById(R.id.QuestionText);
try {
for (int i=0;i<ClientHolder.size();i++) {
inputs.add(new BufferedReader(new InputStreamReader(ClientHolder.get(i).getClientsocket().getInputStream())));
PrintWriter output = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(ClientHolder.get(i).getClientsocket().getOutputStream())), true);
outputs.add(output);
output.println("GameStarted");
}
} catch (IOException e) {
e.printStackTrace();
}
nextQuestionBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (questionIndex < questions.size()) {
questionText.setText(questions.get(questionIndex).getQuestionText());
String questionString = questions.get(questionIndex).getQuestionText();
questionText.setText(questionString);
for (int i=0;i<outputs.size();i++) {
outputs.get(i).println(questionString);
}
questionIndex++;
} else {
questionText.setText("No more questions.");
......@@ -59,4 +85,5 @@ public class GameActivity extends AppCompatActivity {
}
});
}
}
......@@ -59,6 +59,7 @@ public class LobbyActivity extends AppCompatActivity {
private ArrayList<ClientModel> allclients;
private Thread commThread;
@Override
public void onCreate(Bundle savedInstanceState) {
......@@ -100,6 +101,8 @@ public class LobbyActivity extends AppCompatActivity {
@Override
public void onClick(View view) {
serverThread.interrupt();
commThread.interrupt();
ClientHolder.addAll(clients);
Intent intent = new Intent(getApplicationContext(), GameActivity.class);
intent.putExtra("QUESTION_FILE_NAME", questionFileName);
startActivity(intent);
......@@ -109,18 +112,20 @@ 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);
}
*/
/*
for (Thread thread : threads) {
......@@ -149,11 +154,11 @@ public class LobbyActivity extends AppCompatActivity {
mNsdManager.unregisterService(mRegistrationListener);
registred = false;
}
try {
/*try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}*/
super.onDestroy();
}
......@@ -221,10 +226,10 @@ public class LobbyActivity extends AppCompatActivity {
allclients.add(new ClientModel("Unknown", socket));
CommunicationThread commThread = new CommunicationThread(socket);
Thread newthread = new Thread(commThread);
threads.add(newthread);
newthread.start();
CommunicationThread comm = new CommunicationThread(socket);
commThread = new Thread(comm);
threads.add(commThread);
commThread.start();
} catch (IOException e) {
e.printStackTrace();
......
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