-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainTrain.java
More file actions
167 lines (140 loc) · 4.6 KB
/
Copy pathMainTrain.java
File metadata and controls
167 lines (140 loc) · 4.6 KB
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
package test;
import test.server.BookScrabbleHandler;
import test.server.ClientHandler;
import test.server.DictionaryManager;
import test.server.MyServer;
import java.io.*;
import java.net.Socket;
import java.util.Random;
import java.util.Scanner;
public class MainTrain {
public static class ClientHandler1 implements ClientHandler {
PrintWriter out;
Scanner in;
@Override
public void handleClient(InputStream inFromclient, OutputStream outToClient) {
out=new PrintWriter(outToClient);
in=new Scanner(inFromclient);
String text = in.next();
out.println(new StringBuilder(text).reverse().toString());
out.flush();
}
@Override
public void close() {
in.close();
out.close();
}
}
public static void client1(int port) throws Exception{
Socket server=new Socket("localhost", port);
Random r=new Random();
String text = ""+(1000+r.nextInt(100000));
String rev=new StringBuilder(text).reverse().toString();
PrintWriter outToServer=new PrintWriter(server.getOutputStream());
Scanner in=new Scanner(server.getInputStream());
outToServer.println(text);
outToServer.flush();
String response=in.next();
if(response==null || !response.equals(rev))
System.out.println("problem getting the right response from your server, cannot continue the test (-100)");
in.close();
outToServer.println(text);
outToServer.close();
server.close();
}
public static boolean testServer() {
boolean ok=true;
Random r=new Random();
int port=6000+r.nextInt(1000);
MyServer s=new MyServer(port, new ClientHandler1());
int c = Thread.activeCount();
s.start(); // runs in the background
try {
client1(port);
}catch(Exception e) {
System.out.println("some exception was thrown while testing your server, cannot continue the test (-100)");
ok=false;
}
s.close();
try {Thread.sleep(2000);} catch (InterruptedException e) {}
if (Thread.activeCount()!=c) {
System.out.println("you have a thread open after calling close method (-100)");
ok=false;
}
return ok;
}
public static String[] writeFile(String name) {
Random r=new Random();
String txt[]=new String[10];
for(int i=0;i<txt.length;i++)
txt[i]=""+(10000+r.nextInt(10000));
try {
PrintWriter out=new PrintWriter(new FileWriter(name));
for(String s : txt) {
out.print(s+" ");
}
out.println();
out.close();
}catch(Exception e) {}
return txt;
}
public static void testDM() {
String t1[]=writeFile("t1.txt");
String t2[]=writeFile("t2.txt");
String t3[]=writeFile("t3.txt");
DictionaryManager dm=DictionaryManager.get();
if(!dm.query("t1.txt","t2.txt",t2[4]))
System.out.println("problem for Dictionary Manager query (-5)");
if(!dm.query("t1.txt","t2.txt",t1[9]))
System.out.println("problem for Dictionary Manager query (-5)");
if(dm.query("t1.txt","t3.txt","2"+t3[2]))
System.out.println("problem for Dictionary Manager query (-5)");
if(dm.query("t2.txt","t3.txt","3"+t2[5]))
System.out.println("problem for Dictionary Manager query (-5)");
if(!dm.challenge("t1.txt","t2.txt","t3.txt",t3[2]))
System.out.println("problem for Dictionary Manager challenge (-5)");
if(dm.challenge("t2.txt","t3.txt","t1.txt","3"+t2[5]))
System.out.println("problem for Dictionary Manager challenge (-5)");
if(dm.getSize()!=3)
System.out.println("wrong size for the Dictionary Manager (-10)");
}
public static void runClient(int port,String query,boolean result) {
try {
Socket server=new Socket("localhost",port);
PrintWriter out=new PrintWriter(server.getOutputStream());
Scanner in=new Scanner(server.getInputStream());
out.println(query);
out.flush();
String res=in.next();
if((result && !res.equals("true")) || (!result && !res.equals("false")))
System.out.println("problem getting the right answer from the server (-10)");
in.close();
out.close();
server.close();
} catch (IOException e) {
System.out.println("your code ran into an IOException (-10)");
}
}
public static void testBSCH() {
String s1[]=writeFile("s1.txt");
String s2[]=writeFile("s2.txt");
Random r=new Random();
int port=6000+r.nextInt(1000);
MyServer s=new MyServer(port, new BookScrabbleHandler());
s.start();
runClient(port, "Q,s1.txt,s2.txt,"+s1[1], true);
runClient(port, "Q,s1.txt,s2.txt,"+s2[4], true);
runClient(port, "Q,s1.txt,s2.txt,2"+s1[1], false);
runClient(port, "Q,s1.txt,s2.txt,3"+s2[4], false);
runClient(port, "C,s1.txt,s2.txt,"+s1[9], true);
runClient(port, "C,s1.txt,s2.txt,#"+s2[1], false);
s.close();
}
public static void main(String[] args) {
if(testServer()) {
testDM();
testBSCH();
}
System.out.println("done");
}
}