-
Notifications
You must be signed in to change notification settings - Fork 0
/
ServerOperation.java
145 lines (132 loc) · 5.87 KB
/
ServerOperation.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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package pkg2014vision;
import java.io.File;
import java.io.IOException;
import java.net.Socket;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.bytedeco.javacpp.opencv_core.CvSeq;
/**
*
* @author Jared "Jär Bär" Gentner
*/
public class ServerOperation {
Runnable serverop = new Runnable() {
@Override
public void run() {
for (;;) {
try {
byte[] arg = new byte[1];
int i = 0;
try {
System.out.print("Reading...");
client.getInputStream().read(arg);
System.out.println(arg[0]);
System.out.println("done");
} catch (IOException ex) {
Logger.getLogger(ServerOperation.class.getName()).log(Level.SEVERE, null, ex);
}
switch (arg[0]) {
case 7:
try {
Runtime.getRuntime().exec("shutdown -P now");
} catch (IOException ex) {
Logger.getLogger(ServerOperation.class.getName()).log(Level.SEVERE, null, ex);
}
break;
case 5:
if (searches.isEmpty()) {
client.getOutputStream().write(new byte[]{0, 0, 0, 0, 0, 0,});
client.getOutputStream().flush();
searches.clear();
break;
}
ArrayList<ImageObject> templates = new ArrayList<>();
for (Integer val : searches) {
for (ImageObject object : objects) {
if (object.getID() == val) {
templates.add(object);
}
}
}
searches.clear();
filter.setTemplates(templates.toArray(new ImageObject[templates.size()]));
System.out.println(filter.getNumTemplates());
System.out.println(templates.size());
try {
Map<ImageObject, ArrayList<CvSeq>> map = filter.getScores();
System.out.println("keys:" + map.keySet().size());
if (map.keySet().isEmpty()) {
client.getOutputStream().write(new byte[]{0, 0, 0, 0, 0, 0,});
client.getOutputStream().flush();
searches.clear();
break;
}
for (ImageObject key : map.keySet()) {
System.out.println("here");
if (searches.contains(key.getID())) {
for (CvSeq seq : map.get(key)) {
byte[] write = server.packData(new HashMap.SimpleEntry<>(key, seq));
System.out.println(write[0]);
System.out.println("writing");
client.getOutputStream().write(write);
System.out.println("done");
}
}
}
System.out.println("flushing");
client.getOutputStream().flush();
System.out.println("done");
} catch (IOException ex) {
Logger.getLogger(ServerOperation.class.getName()).log(Level.SEVERE, null, ex);
}
break;
case 64:
break;
default:
System.out.println("set" + arg[0]);
Integer search = (int) arg[0];
if (!searches.contains(search)) {
searches.add(search);
}
System.out.println("Num searches: " + searches.size());
break;
}
} catch (IOException ex) {
Logger.getLogger(ServerOperation.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
};
ScoreFilter filter;
Socket client;
ArrayList<ImageObject> objects;
ArrayList<Integer> searches;
TargetServer server;
public ServerOperation(TargetServer server, Socket client, ScoreFilter filter) {
objects = new ArrayList<>();
searches = new ArrayList<>();
this.filter = filter;
this.server = server;
this.client = client;
}
public void scanObjects(String filepath) {
File dir = new File(filepath);
for (File file : dir.listFiles()) {
System.out.println(file);
ImageObject object = ImageObject.fromFile(file);
objects.add(object);
System.out.println("added " + object);
}
}
public Runnable getServerOperation() {
return this.serverop;
}
}