Skip to content

Commit 70eb39a

Browse files
author
vitaliy.emelyanov
committed
Stage 1
Signed-off-by: vitaliy.emelyanov <vitaliy.emelyanov@yadro.com>
1 parent e22da0c commit 70eb39a

File tree

9 files changed

+5265
-0
lines changed

9 files changed

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

0 commit comments

Comments
 (0)