forked from MiniZinc/cpp-integration
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Connector.java
198 lines (162 loc) · 5.74 KB
/
Connector.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package cpp;
import java.io.File;
import java.io.IOException;
import java.net.Socket;
import java.util.List;
/**
* This class is a Java representation of
* the <a href="https://github.com/MiniZinc/cpp-integration">C++ Connector</a> class
* to communicate with cp-profiler, now part of MiniZincIDE.
* Please refer to the README.md file of the original project for more information.
* The Java code is a direct translation of the C++ code, so adapting it to Java
* must be quite straightforward.
*
*/
public class Connector {
public static class Option<T> {
private T value;
private boolean present = false;
public boolean isValid() {
return present;
}
public void set(T value) {
this.value = value;
present = true;
}
public void unset() {
present = false;
}
public T getValue() {
if (!present) throw new IllegalStateException("Value not present");
return value;
}
}
public static class Node {
private final Connector connector;
private final int node;
private final int parent;
private final int restartCount;
private final int alt;
private final int kids;
private final Message.NodeStatus status;
private final Option<String> label = new Option<>();
private final Option<String> nogood = new Option<>();
private final Option<String> info = new Option<>();
public Node(int node, int parent, int restartCount, int alt, int kids, Message.NodeStatus status, Connector connector) {
this.connector = connector;
this.node = node;
this.parent = parent;
this.restartCount = restartCount;
this.alt = alt;
this.kids = kids;
this.status = status;
}
public Option<String> getLabel() {
return label;
}
public Node setLabel(String label) {
this.label.set(label);
return this;
}
public Option<String> getNogood() {
return nogood;
}
public Node setNogood(String nogood) {
this.nogood.set(nogood);
return this;
}
public Option<String> getInfo() {
return info;
}
public Node setInfo(String info) {
this.info.set(info);
return this;
}
public int getAlt() {
return alt;
}
public int getKids() {
return kids;
}
public Message.NodeStatus getStatus() {
return status;
}
public void send() throws IOException {
Connector.sendNode(connector, this);
}
}
private final Message.MessageMarshalling marshalling;
private final int port;
private Socket socket;
private boolean connected;
public Connector(int port) {
this.port = port;
this.connected = false;
this.marshalling = new Message.MessageMarshalling();
}
public boolean isConnected() {
return connected;
}
public void connect() throws IOException {
socket = new Socket("localhost", port);
connected = true;
}
public void sendRawMsg(byte[] buf) throws IOException {
// Each message starts with a four-byte integer encoding the size of the remainder of the message in bytes
int bufSize = buf.length;
byte[] sizeBuffer = new byte[4 + bufSize];
for (int i = 0; i < 4; i++) {
sizeBuffer[i] = (byte) (bufSize >>> (i * 8));
}
System.arraycopy(buf, 0, sizeBuffer, 4, bufSize);
socket.getOutputStream().write(sizeBuffer);
socket.getOutputStream().flush();
}
public void start(String filePath, int executionId, boolean hasRestarts) throws IOException {
String baseName = new File(filePath).getName();
String info = String.format(
"{\"has_restarts\": %b, \"name\": \"%s\", \"execution_id\": %d}",
hasRestarts, baseName, executionId
);
marshalling.makeStart(info);
sendOverSocket();
}
public void restart(int restartId) throws IOException {
String info = String.format("{\"restart_id\": %d}", restartId);
marshalling.makeRestart(info);
sendOverSocket();
}
public void done() throws IOException {
marshalling.makeDone();
sendOverSocket();
}
public void disconnect() throws IOException {
if (socket != null && !socket.isClosed()) {
socket.close();
}
}
private void sendOverSocket() throws IOException {
if (!connected) return;
List<Byte> buf = marshalling.serialize();
byte[] byteArray = new byte[buf.size()];
for (int i = 0; i < buf.size(); i++) {
byteArray[i] = buf.get(i);
}
sendRawMsg(byteArray);
}
public void sendNode(Node node) throws IOException {
if (!connected) return;
Message msg = marshalling.makeNode(node.node, node.parent, node.restartCount,
node.getAlt(), node.getKids(), node.getStatus());
if (node.getLabel().isValid()) msg.setLabel(node.getLabel().getValue());
if (node.getNogood().isValid()) msg.setNogood(node.getNogood().getValue());
if (node.getInfo().isValid()) msg.setInfo(node.getInfo().getValue());
sendOverSocket();
}
public Node createNode(int node, int parent, int restartCount, int alt, int kids, Message.NodeStatus status) {
return new Node(node, parent, restartCount, alt, kids, status, this);
}
public static void sendNode(Connector connector, Node node) throws IOException {
connector.sendNode(node);
}
}