wip: send record v2

This commit is contained in:
Thibaut Valentin 2024-03-22 17:32:17 +01:00
parent 9b5192c395
commit 3c0f1b01d6
2 changed files with 40 additions and 2 deletions

1
.gitignore vendored
View File

@ -47,3 +47,4 @@ nb-configuration.xml
/cle_prive.jks
/src/main/resources/META-INF/resources/
/media/
/media-ext/

View File

@ -46,17 +46,24 @@ public class FileSocket {
try {
if (sessions.containsKey(code)) {
FileRecv fileRecv = sessions.get(code);
fileRecv.session = session;
fileRecv.file = new File(media + "-ext", "record/" + fileRecv.name);
fileRecv.fos = new FileOutputStream(fileRecv.file, false);
logger.info("Start reception of file: " + fileRecv.file.getAbsolutePath());
} else {
session.close(new CloseReason(CloseReason.CloseCodes.VIOLATED_POLICY, "File not found"));
// session.close(new CloseReason(CloseReason.CloseCodes.VIOLATED_POLICY, "File not found"));
}
} catch (IOException e) {
StringWriter errors = new StringWriter();
e.printStackTrace(new PrintWriter(errors));
logger.error(errors.toString());
}
session.getAsyncRemote().sendText("ok", result -> {
if (result.getException() != null) {
System.out.println("Unable to send message: " + result.getException());
}
});
}
@OnClose
@ -116,15 +123,45 @@ public class FileSocket {
@OnMessage
public void onMessage(byte[] data, @PathParam("code") String code) {
int length = (data[1] << 7) | data[2];
byte check_sum = 0;
for (int j = 3; j < length + 3; j++) {
check_sum = (byte) (check_sum ^ data[j]);
}
// System.out.println(length + " - " + data[1] + " - " + data[0] + " - " + check_sum);
if (sessions.containsKey(code)) {
FileRecv fileRecv = sessions.get(code);
if (check_sum != data[0]) {
fileRecv.session.getAsyncRemote().sendText("Error: Checksum error", result -> {
if (result.getException() != null) {
System.out.println("Unable to send message: " + result.getException());
}
});
return;
}
try {
fileRecv.fos.write(data);
fileRecv.fos.write(data, 3, length);
} catch (IOException e) {
StringWriter errors = new StringWriter();
e.printStackTrace(new PrintWriter(errors));
logger.error(errors.toString());
}
try {
Thread.sleep(1);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
fileRecv.session.getAsyncRemote().sendText("ok", result -> {
if (result.getException() != null) {
System.out.println("Unable to send message: " + result.getException());
}
});
}
}