feat: receive match record
This commit is contained in:
parent
f7c33044d7
commit
3211c642f5
42
src/main/java/fr/titionfire/ffsaf/net2/packet/RFile.java
Normal file
42
src/main/java/fr/titionfire/ffsaf/net2/packet/RFile.java
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
package fr.titionfire.ffsaf.net2.packet;
|
||||||
|
|
||||||
|
import fr.titionfire.ffsaf.ws.FileSocket;
|
||||||
|
import jakarta.enterprise.context.ApplicationScoped;
|
||||||
|
import org.jboss.logging.Logger;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
@ApplicationScoped
|
||||||
|
public class RFile {
|
||||||
|
private static final Logger LOGGER = Logger.getLogger(RFile.class);
|
||||||
|
|
||||||
|
final IAction requestSend = (client_Thread, message) -> {
|
||||||
|
try {
|
||||||
|
switch (message.data().get("type").asText()) {
|
||||||
|
case "match":
|
||||||
|
String code = UUID.randomUUID() + "-" + UUID.randomUUID();
|
||||||
|
|
||||||
|
FileSocket.FileRecv fileRecv = new FileSocket.FileRecv(message.data().get("name").asText(), null, null,
|
||||||
|
System.currentTimeMillis());
|
||||||
|
FileSocket.sessions.put(code, fileRecv);
|
||||||
|
|
||||||
|
client_Thread.sendRepTo(code, message);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
client_Thread.sendErrTo("", message);
|
||||||
|
break;
|
||||||
|
|
||||||
|
}
|
||||||
|
} catch (Throwable e) {
|
||||||
|
LOGGER.error(e.getMessage(), e);
|
||||||
|
client_Thread.sendErrTo(e.getMessage(), message);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
public static void register(HashMap<String, IAction> iMap) {
|
||||||
|
RFile rFile = new RFile();
|
||||||
|
|
||||||
|
iMap.put("requestSend", rFile.requestSend);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -9,5 +9,6 @@ public class RegisterAction {
|
|||||||
|
|
||||||
RComb.register(iMap);
|
RComb.register(iMap);
|
||||||
RClub.register(iMap);
|
RClub.register(iMap);
|
||||||
|
RFile.register(iMap);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -6,79 +6,107 @@ import jakarta.websocket.*;
|
|||||||
import jakarta.websocket.server.PathParam;
|
import jakarta.websocket.server.PathParam;
|
||||||
import jakarta.websocket.server.ServerEndpoint;
|
import jakarta.websocket.server.ServerEndpoint;
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
|
import org.eclipse.microprofile.config.inject.ConfigProperty;
|
||||||
|
import org.jboss.logging.Logger;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.*;
|
||||||
import java.io.FileOutputStream;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
@ServerEndpoint("/api/ws/file/{id}")
|
@ServerEndpoint("/api/ws/file/{code}")
|
||||||
@ApplicationScoped
|
@ApplicationScoped
|
||||||
public class FileSocket {
|
public class FileSocket {
|
||||||
Map<String, FileRecv> sessions = new ConcurrentHashMap<>();
|
private static final Logger logger = Logger.getLogger(FileSocket.class);
|
||||||
|
public static Map<String, FileRecv> sessions = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
|
@ConfigProperty(name = "upload_dir")
|
||||||
|
String media;
|
||||||
|
|
||||||
@OnOpen
|
@OnOpen
|
||||||
public void onOpen(Session session, @PathParam("id") String id) {
|
public void onOpen(Session session, @PathParam("code") String code) {
|
||||||
try {
|
try {
|
||||||
File file = File.createTempFile("safca-", ".tmp");
|
if (sessions.containsKey(code)) {
|
||||||
FileRecv fileRecv = new FileRecv(file, new FileOutputStream(file, true), System.currentTimeMillis(),
|
FileRecv fileRecv = sessions.get(code);
|
||||||
session);
|
fileRecv.file = new File(media, "record/" + fileRecv.name);;
|
||||||
System.out.println("File created: " + file.getAbsolutePath());
|
fileRecv.fos = new FileOutputStream(fileRecv.file, false);
|
||||||
sessions.put(id, fileRecv);
|
logger.info("Start reception of file: " + fileRecv.file.getAbsolutePath());
|
||||||
|
} else {
|
||||||
|
session.close(new CloseReason(CloseReason.CloseCodes.VIOLATED_POLICY, "File not found"));
|
||||||
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
StringWriter errors = new StringWriter();
|
||||||
|
e.printStackTrace(new PrintWriter(errors));
|
||||||
|
logger.error(errors.toString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@OnClose
|
@OnClose
|
||||||
public void onClose(Session session, @PathParam("id") String id) {
|
public void onClose(Session session, @PathParam("code") String code) {
|
||||||
if (sessions.containsKey(id)) {
|
if (sessions.containsKey(code)) {
|
||||||
FileRecv fileRecv = sessions.get(id);
|
FileRecv fileRecv = sessions.get(code);
|
||||||
if (fileRecv.fos != null) {
|
if (fileRecv.fos != null) {
|
||||||
try {
|
try {
|
||||||
fileRecv.fos.close();
|
fileRecv.fos.close();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
StringWriter errors = new StringWriter();
|
||||||
|
e.printStackTrace(new PrintWriter(errors));
|
||||||
|
logger.error(errors.toString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
logger.info("File received: " + fileRecv.file.getAbsolutePath());
|
||||||
}
|
}
|
||||||
sessions.remove(id);
|
sessions.remove(code);
|
||||||
}
|
}
|
||||||
|
|
||||||
@OnError
|
@OnError
|
||||||
public void onError(Session session, @PathParam("id") String id, Throwable throwable) {
|
public void onError(Session session, @PathParam("code") String code, Throwable throwable) {
|
||||||
if (sessions.containsKey(id)) {
|
if (sessions.containsKey(code)) {
|
||||||
FileRecv fileRecv = sessions.get(id);
|
FileRecv fileRecv = sessions.get(code);
|
||||||
if (fileRecv.fos != null) {
|
if (fileRecv.fos != null) {
|
||||||
try {
|
try {
|
||||||
fileRecv.fos.close();
|
fileRecv.fos.close();
|
||||||
|
if (fileRecv.file.exists()) {
|
||||||
|
//noinspection ResultOfMethodCallIgnored
|
||||||
|
fileRecv.file.delete();
|
||||||
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
StringWriter errors = new StringWriter();
|
||||||
|
e.printStackTrace(new PrintWriter(errors));
|
||||||
|
logger.error(errors.toString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
sessions.remove(id);
|
sessions.remove(code);
|
||||||
|
logger.error("Error on file reception: " + throwable.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@OnMessage
|
||||||
|
public void onMessage(String message, @PathParam("code") String code) {
|
||||||
|
System.out.println("Received: " + message);
|
||||||
}
|
}
|
||||||
|
|
||||||
@OnMessage
|
@OnMessage
|
||||||
public void onMessage(String message, @PathParam("id") String id) {
|
public void onMessage(byte[] data, @PathParam("code") String code) {
|
||||||
if (sessions.containsKey(id)) {
|
if (sessions.containsKey(code)) {
|
||||||
FileRecv fileRecv = sessions.get(id);
|
FileRecv fileRecv = sessions.get(code);
|
||||||
try {
|
try {
|
||||||
fileRecv.fos.write(message.getBytes());
|
fileRecv.fos.write(data);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
StringWriter errors = new StringWriter();
|
||||||
|
e.printStackTrace(new PrintWriter(errors));
|
||||||
|
logger.error(errors.toString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
@RegisterForReflection
|
@RegisterForReflection
|
||||||
class FileRecv {
|
public static class FileRecv {
|
||||||
|
String name;
|
||||||
File file;
|
File file;
|
||||||
FileOutputStream fos;
|
FileOutputStream fos;
|
||||||
long time;
|
long time;
|
||||||
Session session;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,7 +3,6 @@ import {useLoadingSwitcher} from "../../../hooks/useLoading.jsx";
|
|||||||
import {apiAxios} from "../../../utils/Tools.js";
|
import {apiAxios} from "../../../utils/Tools.js";
|
||||||
import {toast} from "react-toastify";
|
import {toast} from "react-toastify";
|
||||||
import {BirthDayField, OptionField, TextField} from "../../../components/MemberCustomFiels.jsx";
|
import {BirthDayField, OptionField, TextField} from "../../../components/MemberCustomFiels.jsx";
|
||||||
import {ClubSelect} from "../../../components/ClubSelect.jsx";
|
|
||||||
import {addPhoto} from "../../admin/member/InformationForm.jsx";
|
import {addPhoto} from "../../admin/member/InformationForm.jsx";
|
||||||
|
|
||||||
export function NewMemberPage() {
|
export function NewMemberPage() {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user