|
| 1 | +package ru.vk.itmo.test.emelyanovvitaliy; |
| 2 | + |
| 3 | +import one.nio.http.*; |
| 4 | +import one.nio.server.AcceptorConfig; |
| 5 | +import ru.vk.itmo.ServiceConfig; |
| 6 | +import ru.vk.itmo.dao.BaseEntry; |
| 7 | +import ru.vk.itmo.dao.Config; |
| 8 | +import ru.vk.itmo.dao.Entry; |
| 9 | +import ru.vk.itmo.test.reference.dao.ReferenceDao; |
| 10 | + |
| 11 | +import java.io.IOException; |
| 12 | +import java.io.UncheckedIOException; |
| 13 | +import java.lang.foreign.MemorySegment; |
| 14 | +import java.lang.foreign.ValueLayout; |
| 15 | +import java.nio.charset.StandardCharsets; |
| 16 | + |
| 17 | +import static one.nio.http.Request.*; |
| 18 | + |
| 19 | +public class DhtServer extends HttpServer { |
| 20 | + public static final byte[] EMPTY_BODY = new byte[0]; |
| 21 | + private final ReferenceDao dao; |
| 22 | + public DhtServer(ServiceConfig config) throws IOException { |
| 23 | + super(createConfig(config)); |
| 24 | + dao = new ReferenceDao(new Config(config.workingDir(), 1 << 24)); |
| 25 | + } |
| 26 | + |
| 27 | + @Override |
| 28 | + public synchronized void stop() { |
| 29 | + super.stop(); |
| 30 | + try { |
| 31 | + dao.close(); |
| 32 | + } catch (IOException e) { |
| 33 | + throw new UncheckedIOException(e); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + @RequestMethod(METHOD_GET) |
| 38 | + @Path("/v0/entity") |
| 39 | + public Response entity(@Param(value = "id") String id) { |
| 40 | + if (isKeyIncorrect(id)) { |
| 41 | + return new Response(Response.BAD_REQUEST, EMPTY_BODY); |
| 42 | + } |
| 43 | + Entry<MemorySegment> entry = dao.get(keyFor(id)); |
| 44 | + if (entry == null) { |
| 45 | + return new Response(Response.NOT_FOUND, EMPTY_BODY); |
| 46 | + } |
| 47 | + return new Response(Response.OK, valueFor(entry)); |
| 48 | + } |
| 49 | + |
| 50 | + @RequestMethod(METHOD_PUT) |
| 51 | + @Path("/v0/entity") |
| 52 | + public Response putEntity(@Param(value = "id") String id, Request request) { |
| 53 | + if (isKeyIncorrect(id)) { |
| 54 | + return new Response(Response.BAD_REQUEST, EMPTY_BODY); |
| 55 | + } |
| 56 | + dao.upsert(new BaseEntry<>(keyFor(id), MemorySegment.ofArray(request.getBody()))); |
| 57 | + return new Response(Response.CREATED, EMPTY_BODY); |
| 58 | + } |
| 59 | + |
| 60 | + @RequestMethod(METHOD_DELETE) |
| 61 | + @Path("/v0/entity") |
| 62 | + public Response deleteEntity(@Param("id") String id) { |
| 63 | + if (isKeyIncorrect(id)) { |
| 64 | + return new Response(Response.BAD_REQUEST, EMPTY_BODY); |
| 65 | + } |
| 66 | + dao.upsert(new BaseEntry<>(keyFor(id), null)); |
| 67 | + return new Response(Response.ACCEPTED, EMPTY_BODY); |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public void handleDefault(Request request, HttpSession session) throws IOException { |
| 72 | + int requestMethod = request.getMethod(); |
| 73 | + if (requestMethod != METHOD_GET && requestMethod != METHOD_PUT && requestMethod != METHOD_DELETE) { |
| 74 | + session.sendResponse(new Response(Response.METHOD_NOT_ALLOWED, EMPTY_BODY)); |
| 75 | + } else { |
| 76 | + session.sendResponse(new Response(Response.BAD_REQUEST, EMPTY_BODY)); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + private static boolean isKeyIncorrect(String key) { |
| 81 | + return key == null || key.isEmpty(); |
| 82 | + } |
| 83 | + |
| 84 | + private static HttpServerConfig createConfig(ServiceConfig serviceConfig) { |
| 85 | + HttpServerConfig config = new HttpServerConfig(); |
| 86 | + AcceptorConfig acceptorConfig = new AcceptorConfig(); |
| 87 | + acceptorConfig.port = serviceConfig.selfPort(); |
| 88 | + config.acceptors = new AcceptorConfig[] {acceptorConfig}; |
| 89 | + config.closeSessions = true; |
| 90 | + return config; |
| 91 | + } |
| 92 | + |
| 93 | + private static MemorySegment keyFor(String id) { |
| 94 | + return MemorySegment.ofArray(id.getBytes(StandardCharsets.UTF_8)); |
| 95 | + } |
| 96 | + |
| 97 | + private static byte[] valueFor(Entry<MemorySegment> entry) { |
| 98 | + return entry.value().toArray(ValueLayout.JAVA_BYTE); |
| 99 | + } |
| 100 | + |
| 101 | + |
| 102 | +} |
0 commit comments