-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEchoThread.java
More file actions
176 lines (158 loc) · 7.23 KB
/
Copy pathEchoThread.java
File metadata and controls
176 lines (158 loc) · 7.23 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
168
169
170
171
172
173
174
175
176
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.StreamCorruptedException;
import java.net.Socket;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Pattern;
public class EchoThread extends Thread {
protected Socket server;
private String clientIP, filetype;
private byte messageType;
// private final static String FILE_TO_SEND = "results.txt";
private int counter = 0;
private int filecounter = 0;
private String everything;
public EchoThread(Socket clientSocket) {
this.server = clientSocket;
PictureServer ps = new PictureServer();
filecounter = ps.filecount;
}
public void run() {
while(true)
{
try
{
DataInputStream dIn = new DataInputStream(server.getInputStream());
DataOutputStream output = new DataOutputStream(server.getOutputStream());
messageType = dIn.readByte();
switch(messageType){
case 1:
clientIP = dIn.readUTF();
System.out.println(clientIP);
messageType = dIn.readByte();
if(messageType == -1){
File file = new File("input" + filecounter + ".jpg");
ObjectInputStream ois = new ObjectInputStream(server.getInputStream());
System.out.println("Input connected");
byte[] bytes;
FileOutputStream fos = null;
try {
bytes = (byte[])ois.readObject();
// ois.close();
System.out.println("Reading");
fos = new FileOutputStream(file);
fos.write(bytes);
System.out.println("Writing");
fos.flush();
Date dNow = new Date( );
SimpleDateFormat ft = new SimpleDateFormat ("E yyyy.MM.dd 'at' hh:mm:ss a zzz");
System.out.println("Received: " + ft.format(dNow));
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
System.out.println("class not found");
} catch (EOFException e){
//e.printStackTrace();
System.out.println("EOF");
} catch(IOException e){
//e.printStackTrace();
System.out.println("IOExc");
}
if (!file.exists()) {
System.out.println("File already exists!");
}
String command = "sudo python classify_image.py ";
Process p = Runtime.getRuntime().exec(command + "--image_file " + "input" + filecounter + ".jpg");
System.out.println("Classified");
// server.shutdownInput();
p.waitFor();
//DATA ADDITIONS
try(BufferedReader br = new BufferedReader(new FileReader("input" + filecounter + ".jpg" +"r.txt"))) {
StringBuilder sb = new StringBuilder();
String line = br.readLine();
while (line != null) {
sb.append(line);
sb.append(System.lineSeparator());
line = br.readLine();
++counter;
}
everything = sb.toString();
String[] display;
display = everything.split(Pattern.quote("\n"));
output.writeByte(3); //3 for good
output.writeByte(counter);
output.flush();
for(int i = 0; i < display.length; ++i){
System.out.println(i + " " + display[i]);
}
output.writeUTF("|" + "IMAGNITIONS|" + display[0] + "|" + display[2] + "|" + display[4] + "|" + display[6] + "|" + display[8] + "|IMAGNITION" + "|");
output.flush();
File file1 = new File("input" + filecounter + ".jpg" +"r.txt");
file.delete();
file1.delete();
System.out.println("Images deleted");
}
catch(FileNotFoundException fe){
System.out.println("----FAILED-----");
output.writeByte(-3); //for bad
output.flush();
file.delete();
System.out.println("--EXCESS FILES DELETED---");
}catch(IOException e1){
e1.printStackTrace();
file.delete();
File file1 = new File("input" + filecounter + ".jpg" +"r.txt");
file1.delete();
return;
}
}
else{
System.out.println("Error");
System.exit(1);
}
break;
default:
System.out.println("Invalid IP processing, exit" + messageType);
break;
}
return;
}
catch(SocketTimeoutException st)
{
System.out.println("Socket timed out!");
break;
}
catch(EOFException ee){
System.out.println("EOF");
break;
}
catch(StreamCorruptedException sce){
System.out.println("File corrupt");
break;
}
catch(IOException e)
{
e.printStackTrace();
break;
}
catch(Exception ex)
{
System.out.println(ex);
}
}
return;
}
}