Commit 48a9aa08 authored by fpeterfalvi's avatar fpeterfalvi
Browse files

Network operations executed in AsyncTask

parent e16b7298
package onlab.kvizclient;
import android.os.AsyncTask;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
......@@ -45,7 +46,7 @@ public class GameActivity extends AppCompatActivity implements MultipleChoiceFra
input = new BufferedReader(new InputStreamReader(ServerHolder.getSocket().getInputStream()));
output = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(ServerHolder.getSocket().getOutputStream())),true);
output.println("$$$$");
new SendMessageToServerTask().execute("$$$$");
} catch (IOException e) {
e.printStackTrace();
}
......@@ -127,8 +128,15 @@ public class GameActivity extends AppCompatActivity implements MultipleChoiceFra
} else {
displayTextView.setText("nem válaszolt");
}
output.println(Integer.toString(answer) + "##" + Double.toString(answerTime));
new SendMessageToServerTask().execute(Integer.toString(answer) + "##" + Double.toString(answerTime));
}
private class SendMessageToServerTask extends AsyncTask<String, Void, Void> {
protected Void doInBackground(String... params) {
String message = params[0];
output.println(message);
return null;
}
}
}
......@@ -5,6 +5,7 @@ import android.content.Intent;
import android.content.pm.ServiceInfo;
import android.net.nsd.NsdManager;
import android.net.nsd.NsdServiceInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
......@@ -54,6 +55,8 @@ public class LobbyActivity extends AppCompatActivity implements ServerConnection
private RecyclerView recyclerView;
private ServerListAdapter adapter = null;
private PrintWriter out = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
......@@ -238,9 +241,11 @@ public class LobbyActivity extends AppCompatActivity implements ServerConnection
ServerModel sm = servers.get(serverNumber);
Socket socket = sm.getSocket();
ServerHolder.setServerModel(sm);
PrintWriter out = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(sm.getSocket().getOutputStream())), true);
out.println(str);
if (out == null) {
out = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(sm.getSocket().getOutputStream())), true);
}
new SendMessageToServerTask().execute(str);
}
} catch (UnknownHostException e) {
e.printStackTrace();
......@@ -255,9 +260,11 @@ public class LobbyActivity extends AppCompatActivity implements ServerConnection
try {
if (ServerHolder.getSocket() != null) {
String str = "Disconnect##"+CLIENT_NAME;
PrintWriter out = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(ServerHolder.getSocket().getOutputStream())), true);
out.println(str);
if (out == null) {
out = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(ServerHolder.getSocket().getOutputStream())), true);
}
new SendMessageToServerTask().execute(str);
}
} catch (IOException e) {
e.printStackTrace();
......@@ -417,6 +424,15 @@ public class LobbyActivity extends AppCompatActivity implements ServerConnection
}
}
private class SendMessageToServerTask extends AsyncTask<String, Void, Void> {
protected Void doInBackground(String... params) {
String message = params[0];
out.println(message);
return null;
}
}
}
enum Operation {
......
package onlab.kvizclient;
import android.os.AsyncTask;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
......@@ -51,7 +52,7 @@ public class QuizUpActivity extends AppCompatActivity implements MultipleChoiceF
input = new BufferedReader(new InputStreamReader(ServerHolder.getSocket().getInputStream()));
output = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(ServerHolder.getSocket().getOutputStream())),true);
output.println("$$$$");
new SendMessageToServerTask().execute("$$$$");
} catch (IOException e) {
e.printStackTrace();
}
......@@ -127,8 +128,15 @@ public class QuizUpActivity extends AppCompatActivity implements MultipleChoiceF
Date questionAnsweredDate = new Date();
double answerTime = (double)(questionAnsweredDate.getTime() - questionReceivedDate.getTime()) / 1000;
Log.d("A válasz: ", Integer.toString(answer) + " (" + Double.toString(answerTime) + "s)");
output.println(Integer.toString(answer) + "##" + Double.toString(answerTime));
new SendMessageToServerTask().execute(Integer.toString(answer) + "##" + Double.toString(answerTime));
}
private class SendMessageToServerTask extends AsyncTask<String, Void, Void> {
protected Void doInBackground(String... params) {
String message = params[0];
output.println(message);
return null;
}
}
}
package onlab.kvizserver;
import android.content.pm.PackageManager;
import android.os.AsyncTask;
import android.os.Handler;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
......@@ -203,11 +204,13 @@ public class GameActivity extends AppCompatActivity implements GameControlFragme
for (int i=0;i<outputs.size();i++) {
gameControlFragment.displayAnswerOfPlayer(i,"The answer of " + playerAnswers[i].getPlayerName() + ": ");
if (question.getType() == Question.MULTIPLE_CHOICE) {
outputs.get(i).println("questionMC##" + questionString + "##" + answers.get(0) + "##" + answers.get(1)
String message = "questionMC##" + questionString + "##" + answers.get(0) + "##" + answers.get(1)
+ "##" + answers.get(2) + "##" + answers.get(3) + "##" + playerAnswersString
+ "##" + Integer.toString(i) + "##-1##" + Integer.toString(timeLimit));
+ "##" + Integer.toString(i) + "##-1##" + Integer.toString(timeLimit);
new SendMessageToClientTask().execute(Integer.toString(i), message);
} else {
outputs.get(i).println("questionG##" + questionString + "##" + Integer.toString(timeLimit));
String message = "questionG##" + questionString + "##" + Integer.toString(timeLimit);
new SendMessageToClientTask().execute(Integer.toString(i), message);
}
}
......@@ -225,14 +228,16 @@ public class GameActivity extends AppCompatActivity implements GameControlFragme
for (int i=0;i<outputs.size();i++) {
if (answers.size() == 4) {
String playerAnswersString = gson.toJson(playerAnswers);
outputs.get(i).println("answerMC##" + questionString + "##" + answers.get(0) + "##" + answers.get(1)
String message = "answerMC##" + questionString + "##" + answers.get(0) + "##" + answers.get(1)
+ "##" + answers.get(2) + "##" + answers.get(3) + "##" + playerAnswersString
+ "##" + Integer.toString(i) + "##" + Integer.toString(correctAnswer) + "##-1");
+ "##" + Integer.toString(i) + "##" + Integer.toString(correctAnswer) + "##-1";
new SendMessageToClientTask().execute(Integer.toString(i), message);
} else {
calculateGuessQuestionWinner();
String playerAnswersString = gson.toJson(playerAnswers);
outputs.get(i).println("answerG##" + questionString + "##" + Integer.toString(correctAnswer)
+ "##" + playerAnswersString + "##" + Integer.toString(i));
String message = "answerG##" + questionString + "##" + Integer.toString(correctAnswer)
+ "##" + playerAnswersString + "##" + Integer.toString(i);
new SendMessageToClientTask().execute(Integer.toString(i), message);
}
}
}
......@@ -291,15 +296,12 @@ public class GameActivity extends AppCompatActivity implements GameControlFragme
Iterator<Question> it = questions.iterator();
while( it.hasNext() ) {
Question q = it.next();
boolean selected = false;
String topic = q.getTopic();
if(q.getType() == Question.MULTIPLE_CHOICE &&
selectedMultiple.get(topic) != null && selectedMultiple.get(topic) < numberOfMultipleChoice) {
selected = true;
selectedMultiple.put(topic, selectedMultiple.get(topic) + 1);
} else if (q.getType() == Question.GUESS &&
selectedGuess.get(topic) != null && selectedGuess.get(topic) < numberOfGuess) {
selected = true;
selectedGuess.put(topic, selectedGuess.get(topic) + 1);
} else {
q.setCurrentIndex(0);
......@@ -338,4 +340,12 @@ public class GameActivity extends AppCompatActivity implements GameControlFragme
}
}
private class SendMessageToClientTask extends AsyncTask<String, Void, Void> {
protected Void doInBackground(String... params) {
int index = Integer.parseInt(params[0]);
String message = params[1];
outputs.get(index).println(message);
return null;
}
}
}
package onlab.kvizserver;
import android.os.AsyncTask;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
......@@ -94,16 +95,14 @@ public class QuizUpActivity extends AppCompatActivity {
for (int i=0;i<numberOfPlayers;i++) {
final TextView playerAnswerTextView = new TextView(this);
//playerAnswerTextView.setText("The answer of the " + Integer.toString(i + 1) + ". player: ");
playerAnswerTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 30);
playerAnswerTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
playerAnswersLinearLayout.addView(playerAnswerTextView);
playerAnswerTextViews.add(playerAnswerTextView);
}
for (int i=0;i<numberOfPlayers;i++) {
final TextView playerScoreTextView = new TextView(this);
//playerScoreTextView.setText("The score of the " + Integer.toString(i + 1) + ". player: 0");
playerScoreTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 30);
playerScoreTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
playerAnswersLinearLayout.addView(playerScoreTextView);
playerScoreTextViews.add(playerScoreTextView);
}
......@@ -113,10 +112,10 @@ public class QuizUpActivity extends AppCompatActivity {
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);
output.println("$$$$");
//output.println("$$$$");
outputs.add(output);
Log.d("QuizUpActivity", "activity started");
output.println("GameStarted##quizup");
//output.println("GameStarted##quizup");
}
} catch (IOException e) {
e.printStackTrace();
......@@ -165,7 +164,7 @@ public class QuizUpActivity extends AppCompatActivity {
+ "##" + answers.get(0) + "##" + answers.get(1)
+ "##" + answers.get(2) + "##" + answers.get(3) + "##" + playerAnswersString
+ "##" + Integer.toString(i) + "##-1";
outputs.get(i).println(message);
new SendMessageToClientTask().execute(Integer.toString(i), message);
Log.d("KvizServer", Integer.toString(i) + "-es kliensnek küldött üzenet: " + message);
}
round++;
......@@ -184,14 +183,14 @@ public class QuizUpActivity extends AppCompatActivity {
messageForPlayers[i] = "message##" + Integer.toString(playerScores[i]) + "##" + Integer.toString(playerScores[1-i]) + "##";
}
if (playerScores[0] > playerScores[1]) {
outputs.get(0).println(messageForPlayers[0] + "Nyertél!");
outputs.get(1).println(messageForPlayers[1] + "Vesztettél!");
new SendMessageToClientTask().execute(Integer.toString(0), messageForPlayers[0] + "Nyertél!");
new SendMessageToClientTask().execute(Integer.toString(1), messageForPlayers[1] + "Vesztettél!");
} else if (playerScores[1] > playerScores[0]) {
outputs.get(1).println(messageForPlayers[1] + "Nyertél!");
outputs.get(0).println(messageForPlayers[0] + "Vesztettél!");
new SendMessageToClientTask().execute(Integer.toString(0), messageForPlayers[1] + "Nyertél!");
new SendMessageToClientTask().execute(Integer.toString(1), messageForPlayers[0] + "Vesztettél!");
} else {
outputs.get(0).println(messageForPlayers[0] + "Döntetlen!");
outputs.get(1).println(messageForPlayers[1] + "Döntetlen!");
new SendMessageToClientTask().execute(Integer.toString(0), messageForPlayers[0] + "Döntetlen!");
new SendMessageToClientTask().execute(Integer.toString(1), messageForPlayers[1] + "Döntetlen!");
}
}
}
......@@ -220,7 +219,7 @@ public class QuizUpActivity extends AppCompatActivity {
+ "##" + answers.get(0) + "##" + answers.get(1)
+ "##" + answers.get(2) + "##" + answers.get(3) + "##" + playerAnswersString
+ "##" + Integer.toString(i) + "##" + Integer.toString(correctAnswer);
outputs.get(i).println(message);
new SendMessageToClientTask().execute(Integer.toString(i), message);
Log.d("KvizServer", Integer.toString(i) + "-es kliensnek küldött üzenet: " + message);
}
}
......@@ -234,8 +233,9 @@ public class QuizUpActivity extends AppCompatActivity {
@Override
public void run() {
for (int i=0;i<2;i++) {
outputs.get(i).println("message##" + Integer.toString(playerScores[i])
+ "##" + Integer.toString(playerScores[1-i]) + "##" + message);
String mess = "message##" + Integer.toString(playerScores[i])
+ "##" + Integer.toString(playerScores[1-i]) + "##" + message;
new SendMessageToClientTask().execute(Integer.toString(i), mess);
}
}
});
......@@ -259,6 +259,9 @@ public class QuizUpActivity extends AppCompatActivity {
}
public void run() {
output.println("$$$$");
output.println("GameStarted##quizup");
while (!Thread.currentThread().isInterrupted()) {
try {
String read = input.readLine();
......@@ -317,4 +320,13 @@ public class QuizUpActivity extends AppCompatActivity {
}
}
private class SendMessageToClientTask extends AsyncTask<String, Void, Void> {
protected Void doInBackground(String... params) {
int index = Integer.parseInt(params[0]);
String message = params[1];
outputs.get(index).println(message);
return null;
}
}
}
......@@ -15,37 +15,37 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/QuestionText"
android:textSize="30sp"/>
android:textSize="20sp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/Answer1TextView"
android:textSize="30sp"/>
android:textSize="20sp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/Answer2TextView"
android:textSize="30sp"/>
android:textSize="20sp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/Answer3TextView"
android:textSize="30sp"/>
android:textSize="20sp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/Answer4TextView"
android:textSize="30sp"/>
android:textSize="20sp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/CorrectAnswerTextView"
android:textSize="30sp"/>
android:textSize="20sp"/>
<LinearLayout
android:layout_width="match_parent"
......
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