-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e22da0c
commit 08cc420
Showing
28 changed files
with
4,965 additions
and
0 deletions.
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13
src/main/java/ru/vk/itmo/test/nikitaprokopev/MyFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package ru.vk.itmo.test.nikitaprokopev; | ||
|
||
import ru.vk.itmo.Service; | ||
import ru.vk.itmo.ServiceConfig; | ||
import ru.vk.itmo.test.ServiceFactory; | ||
|
||
@ServiceFactory(stage = 1) | ||
public class MyFactory implements ServiceFactory.Factory { | ||
@Override | ||
public Service create(ServiceConfig config) { | ||
return new MyService(config); | ||
} | ||
} |
126 changes: 126 additions & 0 deletions
126
src/main/java/ru/vk/itmo/test/nikitaprokopev/MyServer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
package ru.vk.itmo.test.nikitaprokopev; | ||
|
||
import one.nio.http.*; | ||
import one.nio.server.AcceptorConfig; | ||
import ru.vk.itmo.ServiceConfig; | ||
import ru.vk.itmo.dao.BaseEntry; | ||
import ru.vk.itmo.dao.Config; | ||
import ru.vk.itmo.dao.Entry; | ||
import ru.vk.itmo.test.nikitaprokopev.dao.ReferenceDao; | ||
|
||
import java.io.IOException; | ||
import java.io.UncheckedIOException; | ||
import java.lang.foreign.MemorySegment; | ||
import java.lang.foreign.ValueLayout; | ||
import java.nio.charset.StandardCharsets; | ||
|
||
import static one.nio.http.Request.*; | ||
import static one.nio.http.Response.*; | ||
|
||
public class MyServer extends HttpServer { | ||
|
||
private static final long FLUSH_THRESHOLD_BYTES = 2 * 1024 * 1024; // 2 MB | ||
private final ReferenceDao dao; | ||
|
||
public MyServer(ServiceConfig serviceConfig) throws IOException { | ||
super(createServerConfig(serviceConfig)); | ||
dao = new ReferenceDao(new Config(serviceConfig.workingDir(), FLUSH_THRESHOLD_BYTES)); | ||
} | ||
|
||
private static HttpServerConfig createServerConfig(ServiceConfig serviceConfig) { | ||
HttpServerConfig httpServerConfig = new HttpServerConfig(); | ||
AcceptorConfig acceptorConfig = new AcceptorConfig(); | ||
acceptorConfig.port = serviceConfig.selfPort(); | ||
acceptorConfig.reusePort = true; | ||
|
||
httpServerConfig.acceptors = new AcceptorConfig[]{acceptorConfig}; | ||
httpServerConfig.closeSessions = true; | ||
return httpServerConfig; | ||
} | ||
|
||
public void close() { | ||
try { | ||
dao.close(); | ||
} catch (IOException e) { | ||
throw new UncheckedIOException(e); | ||
} | ||
} | ||
|
||
@Path("/v0/entity") | ||
@RequestMethod(METHOD_PUT) | ||
public Response put(@Param(value = "id", required = true) String id, Request request) { | ||
MemorySegment msKey = (id == null || id.isEmpty()) ? null : toMemorySegment(id); | ||
if (msKey == null) { | ||
return new Response(BAD_REQUEST, EMPTY); | ||
} | ||
|
||
Entry<MemorySegment> entry = new BaseEntry<>( | ||
msKey, | ||
toMemorySegment(request.getBody()) | ||
); | ||
|
||
dao.upsert(entry); | ||
|
||
return new Response(CREATED, EMPTY); | ||
} | ||
|
||
@Path("/v0/entity") | ||
@RequestMethod(METHOD_GET) | ||
public Response get(@Param(value = "id", required = true) String id) { | ||
MemorySegment msKey = (id == null || id.isEmpty()) ? null : toMemorySegment(id); | ||
if (msKey == null) { | ||
return new Response(BAD_REQUEST, EMPTY); | ||
} | ||
|
||
Entry<MemorySegment> entry = dao.get(msKey); | ||
|
||
if (entry == null) { | ||
return new Response(NOT_FOUND, EMPTY); | ||
} | ||
|
||
return Response.ok(toByteArray(entry.value())); | ||
} | ||
|
||
@Path("/v0/entity") | ||
@RequestMethod(METHOD_DELETE) | ||
public Response delete(@Param(value = "id", required = true) String id) { | ||
MemorySegment msKey = (id == null || id.isEmpty()) ? null : toMemorySegment(id); | ||
if (msKey == null) { | ||
return new Response(BAD_REQUEST, EMPTY); | ||
} | ||
|
||
Entry<MemorySegment> entry = new BaseEntry<>( | ||
msKey, | ||
null | ||
); | ||
|
||
dao.upsert(entry); | ||
|
||
return new Response(ACCEPTED, EMPTY); | ||
} | ||
|
||
// For other methods | ||
@Path("/v0/entity") | ||
public Response other() { | ||
return new Response(METHOD_NOT_ALLOWED, EMPTY); | ||
} | ||
|
||
private MemorySegment toMemorySegment(String s) { | ||
return MemorySegment.ofArray(s.getBytes(StandardCharsets.UTF_8)); | ||
} | ||
|
||
private MemorySegment toMemorySegment(byte[] bytes) { | ||
return MemorySegment.ofArray(bytes); | ||
} | ||
|
||
private byte[] toByteArray(MemorySegment ms) { | ||
return ms.toArray(ValueLayout.JAVA_BYTE); | ||
} | ||
|
||
// For other requests - 400 Bad Request | ||
@Override | ||
public void handleDefault(Request request, HttpSession session) throws IOException { | ||
Response response = new Response(BAD_REQUEST, EMPTY); | ||
session.sendResponse(response); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/main/java/ru/vk/itmo/test/nikitaprokopev/MyService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package ru.vk.itmo.test.nikitaprokopev; | ||
|
||
import ru.vk.itmo.Service; | ||
import ru.vk.itmo.ServiceConfig; | ||
|
||
import java.io.IOException; | ||
import java.io.UncheckedIOException; | ||
import java.util.concurrent.CompletableFuture; | ||
|
||
public class MyService implements Service { | ||
|
||
private MyServer server; | ||
private final ServiceConfig config; | ||
|
||
public MyService(ServiceConfig config) { | ||
this.config = config; | ||
} | ||
|
||
@Override | ||
public CompletableFuture<Void> start() throws IOException { | ||
server = new MyServer(config); | ||
server.start(); | ||
return CompletableFuture.completedFuture(null); | ||
} | ||
|
||
@Override | ||
public CompletableFuture<Void> stop() { | ||
server.stop(); | ||
server.close(); | ||
return CompletableFuture.completedFuture(null); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/ru/vk/itmo/test/nikitaprokopev/ServerStarter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package ru.vk.itmo.test.nikitaprokopev; | ||
|
||
import ru.vk.itmo.ServiceConfig; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.util.List; | ||
|
||
public class ServerStarter { | ||
public static void main(String[] args) throws IOException { | ||
MyServer server = new MyServer( | ||
new ServiceConfig( | ||
8080, | ||
"http://localhost", | ||
List.of("http://localhost"), | ||
Files.createTempDirectory("dao") | ||
) | ||
); | ||
|
||
server.start(); | ||
} | ||
} |
Oops, something went wrong.