mirror of
https://github.com/eclipse-sailing-analytics/sailing-analytics.git
synced 2026-10-04 03:13:49 +00:00
added first test case towards SwissTiming SailMaster connectivity
This commit is contained in:
1 parent
c49c239289
commit
70f0d8ce35
4 files changed
+180
No files matched your search
+31
@@ -0,0 +1,31 @@
|
||||
package com.sap.sailing.domain.swisstimingadapter.impl;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.net.Socket;
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
public class SailMasterConnector extends SailMasterTransceiver {
|
||||
private final String host;
|
||||
private final int port;
|
||||
private Socket socket;
|
||||
|
||||
public SailMasterConnector(String host, int port) {
|
||||
super();
|
||||
this.host = host;
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
public String sendRequestAndGetResponse(String requestMessage) throws UnknownHostException, IOException {
|
||||
ensureSocketIsOpen();
|
||||
OutputStream os = socket.getOutputStream();
|
||||
sendMessage(requestMessage, os);
|
||||
return receiveMessage(socket.getInputStream());
|
||||
}
|
||||
|
||||
private void ensureSocketIsOpen() throws UnknownHostException, IOException {
|
||||
if (socket == null) {
|
||||
socket = new Socket(host, port);
|
||||
}
|
||||
}
|
||||
}
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
package com.sap.sailing.domain.swisstimingadapter.impl;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
|
||||
public class SailMasterTransceiver {
|
||||
private static final byte STX = 0x02;
|
||||
private static final byte ETX = 0x03;
|
||||
|
||||
public void sendMessage(String message, OutputStream os) throws IOException {
|
||||
os.write(STX);
|
||||
os.write(message.getBytes()); // TODO clarify which character set / encoding to use
|
||||
os.write(ETX);
|
||||
}
|
||||
|
||||
public String receiveMessage(InputStream inputStream) throws IOException {
|
||||
// read until an STX byte comes along
|
||||
int read = inputStream.read();
|
||||
while (read != -1 && read != STX) {
|
||||
read = inputStream.read();
|
||||
}
|
||||
String message = null;
|
||||
if (read == STX) {
|
||||
message = readMessage(inputStream);
|
||||
}
|
||||
return message;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read bytes until EOF or ETX is reached and turn those bytes into a string.
|
||||
*/
|
||||
private String readMessage(InputStream is) throws IOException {
|
||||
ByteArrayOutputStream bos = new ByteArrayOutputStream(4096);
|
||||
int read = is.read();
|
||||
while (read != -1 && read != ETX) {
|
||||
bos.write(read);
|
||||
read = is.read();
|
||||
}
|
||||
String message = new String(bos.toByteArray()); // TODO clarify character set / encoding with Kai
|
||||
return message;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user