-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClient.java
173 lines (143 loc) · 4.3 KB
/
Client.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class Client {
private Socket s;
private ObjectInputStream in;
private ObjectOutputStream out;
private String command;
public Client() throws UnknownHostException, IOException {
command = "";
}
public ArrayList<IQuestion> getMultipleChooiceQuestions()
throws IOException, ClassNotFoundException {
openSocket();
command = "TakeMultipleChooiceQuestion";
out.writeObject(command);
ArrayList<IQuestion> q = (ArrayList<IQuestion>) in.readObject();
closeSocket();
return (q);
}
public ArrayList<IQuestion> getFillBlankQuestions() throws IOException,
ClassNotFoundException {
openSocket();
command = "TakeFillBlankQuestion";
out.writeObject(command);
ArrayList<IQuestion> q = (ArrayList<IQuestion>) in.readObject();
closeSocket();
return q;
}
public ArrayList<DragDropMemory> getDragDropQuestinos() throws IOException, ClassNotFoundException {
openSocket();
command = "TakeDragDropQuestion";
out.writeObject(command);
ArrayList<DragDropMemory> q = (ArrayList<DragDropMemory>) in.readObject();
closeSocket();
return q;
}
public void addMultipleChooiceQuestion(IQuestion q) throws IOException {
openSocket();
command = "AddMultipleChooiceQuestion";
out.writeObject(command);
out.writeObject(q);
closeSocket();
}
public void addFillBlankQuestion(IQuestion q) throws IOException {
openSocket();
command = "AddFillBlankQuestion";
out.writeObject(command);
out.writeObject(q);
closeSocket();
}
public void addDragDropQuestion(DragDropMemory emptyDragAndDropQuestion) throws IOException{
openSocket();
command = "AddDragDropQuestion";
out.writeObject(command);
out.writeObject(emptyDragAndDropQuestion);
closeSocket();
}
public void deleteFillBlankQuestion(int numberOfDeletedQuestion)
throws UnknownHostException, IOException {
openSocket();
command = "DeleteFillBlankQuestion";
out.writeObject(command);
out.writeObject(numberOfDeletedQuestion);
closeSocket();
}
public void deleteMultipleChoiceQuestions(int numberOfDeletedQuestion) throws IOException {
openSocket();
command = "DeleteMultipleChoiceQuestion";
out.writeObject(command);
out.writeObject(numberOfDeletedQuestion);
closeSocket();
}
public void deleteDragDropQuestions(int numberOfDeletedQuestion) throws IOException {
openSocket();
command = "DeleteDragDropQuestion";
out.writeObject(command);
out.writeObject(numberOfDeletedQuestion);
closeSocket();
}
public boolean passwordControl(String password) throws IOException,
ClassNotFoundException {
openSocket();
command = "PasswordControl";
out.writeObject(command);
out.writeObject(password);
boolean b = (Boolean) in.readObject();
closeSocket();
return b;
}
public void openSocket() {
try {
s = new Socket("cs102project.no-ip.biz", 9090);
//s = new Socket("127.0.0.1", 9090);
} catch (IOException e) {
JOptionPane.showMessageDialog(new JFrame("Error"), "You cannot connect with server!");
System.exit(0);
}
try {
in = new ObjectInputStream(s.getInputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
out = new ObjectOutputStream(s.getOutputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void closeSocket() throws IOException {
s.close();
in.close();
out.close();
}
public void closeServer() throws IOException {
openSocket();
command = "CloseServer";
out.writeObject(command);
closeSocket();
}
public static void main(String[] args) throws UnknownHostException,
IOException {
Client c = new Client();
GrammarComboBoxQuestion a = new GrammarComboBoxQuestion(
"Present Tenses", "is", "are", "am", "What - your name?",
"Beginner");
GrammarFillBlankQuestion b = new GrammarFillBlankQuestion(
"Present Tenses", "are", "How old - you?", "Beginner");
ArrayList<IQuestion> soru = null;
DragDropMemory d = new DragDropMemory("asd","asd",new ArrayList<Box>());
c.addDragDropQuestion(d);
c.addFillBlankQuestion(b);
c.addMultipleChooiceQuestion(a);
//c.closeServer();
}
}