forked from kishanrajput23/Java-Projects-Collections
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClientThread.java
110 lines (101 loc) · 3.72 KB
/
ClientThread.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.TimeUnit;
class Quiz {
int quesNum = 0;
int threadnum = 0;
static String Question, Option, Answer;
static boolean has_started = false;
void sendQues(DataOutputStream dataOutputStream, int count) throws IOException, InterruptedException {
threadnum++;
if (threadnum < 3) {
synchronized (this) {
wait();
}
}
if (!has_started) {
if (Initiator.initiator()) { // call initiator if everything is fine
System.out.println("!! Quiz is LIVE now !!");
System.out.println("-------------------------");
}
has_started = true;
}
dataOutputStream.writeUTF(Question);
dataOutputStream.writeUTF(Option);
if (threadnum == 3)
synchronized (this) {
Answer = reader.readAns();
threadnum = 0;
notifyAll();
}
TimeUnit.SECONDS.sleep(5);
}
int receiveAns(DataInputStream dataInputStream) throws IOException {
String ans = dataInputStream.readUTF();
if (ans.equals(Answer))
return 1;
return 0;
}
}
public class ClientThread extends Thread {
static int count = 0, name_count = 0, total_ques = 0, time_per_ques = 0;
final static Quiz quiz = new Quiz();
static boolean isAnswering = false;
String name;
int port, score = 0;
ClientThread(int port) {
this.port = port;
}
public static void initialise(int total_ques, int time_per_ques) {
ClientThread.total_ques = total_ques;
ClientThread.time_per_ques = time_per_ques;
}
@Override
public void run() {
try {
ServerSocket serverSocket = new ServerSocket(port);
// connecting to Server..
Socket socket = serverSocket.accept();
DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream());
DataInputStream dataInputStream = new DataInputStream(socket.getInputStream());
name = dataInputStream.readUTF();
dataOutputStream.writeInt(total_ques);
dataOutputStream.writeInt(time_per_ques);
System.out.println(name + " Connected.");
count++;
while (quiz.quesNum < total_ques) {
if (port == 1000) {
String[] list = reader.readQues();
// call readQues which reads Question from generated file
quiz.Question = list[0];
quiz.Option = list[1];
} else {
Thread.sleep(1000);
}
quiz.sendQues(dataOutputStream, count);
if (port == 1000)
isAnswering = true;
score += quiz.receiveAns(dataInputStream);
if (port == 1000) {
isAnswering = false;
count = 0;
quiz.quesNum++;
} else {
if (quiz.quesNum == total_ques - 1 && isAnswering)
break;
Thread.sleep(500);
}
}
serverSocket.close();
// Print participants score on Host side
System.out.println(name + "'s score: " + score);
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}