odnoklassniki/one-nio

Not starting server

kilinochi opened this issue · 1 comments

I am doing a course project in the project's boundaries "Technopolis" (https://github.com/kilinochi/2019-highload-dht) and I have a problem with server - he is not running.

My main class

` public final class Server {
private static final int PORT = 8080
private Server() {
// Not instantiable
}

public static void main(String[] args) throws IOException {
    // Temporary storage in the file system
    final File data = Files.createTempDirectory();

    // Start the service
    final DAO dao = DAOFactory.create(data);
    final Service service =
            ServiceFactory.create(
                    PORT,
                    dao);
    service.start();
    Runtime.getRuntime().addShutdownHook(
            new Thread(() -> {
                service.stop();
                try {
                    dao.close();
                } catch (IOException e) {
                    throw new RuntimeException("Can't close dao", e);
                }
            }));
}

}`

and RestService class, which implement Service

`public final class RestService extends HttpServer implements Service {

private static final Logger logger = LoggerFactory.getLogger(RestService.class);
private static final String API = "/v0/entity";

private final DAO dao;

public RestService(final int port, @NotNull final DAO dao) throws IOException {
    super(getConfig(port));
    logger.info("Server is running on port " + port);
    this.dao = dao;
}

@Override
public void start() {

}

@Override
public void stop() {

}

@Path(API)
public Response entity (@Param("id") final String id, final Request request) {

    if(id == null || id.isEmpty()) {
        return new Response(Response.BAD_REQUEST, "Key not found".getBytes(Charsets.UTF_8));
    }

    final ByteBuffer key = ByteBuffer.wrap(id.getBytes(Charsets.UTF_8));

    logger.info("Request " + request.getMethod() + " with param: " + id);

    try {
        switch (request.getMethod()) {
            case Request.METHOD_GET:
                final ByteBuffer value = dao.get(key);
                final ByteBuffer duplicate = value.duplicate();
                byte [] body = new byte[duplicate.remaining()];
                duplicate.get(body);
                return new Response(Response.OK, body);
            case Request.METHOD_PUT:
                dao.upsert(key, ByteBuffer.wrap(request.getBody()));
                return new Response(Response.CREATED, Response.EMPTY);
            case Request.METHOD_DELETE:
                dao.remove(key);
            default:
                return new Response(Response.METHOD_NOT_ALLOWED, Response.EMPTY);
        }
    }
    catch (IOException e) {
        return  new Response(Response.INTERNAL_ERROR, Response.EMPTY);
    }
    catch (NoSuchElementException e) {
        return new Response(Response.NOT_FOUND, "Key not found".getBytes(Charsets.UTF_8));
    }
}

private static HttpServerConfig getConfig(final int port) {
    if(port <= 1024 || port >= 65535) {
        throw new IllegalArgumentException("Invalid port");
    }
    AcceptorConfig acceptorConfig = new AcceptorConfig();
    acceptorConfig.port = port;
    HttpServerConfig config = new HttpServerConfig();
    config.acceptors = new AcceptorConfig[]{acceptorConfig};
    return config;
}

} `

So, out all of this
Снимок экрана от 2019-09-29 20-01-54

And server not running...

I'm afraid, github issues is not the right place to seek for debugging help with your course projects.

First, please take an effort to investigate the problem. There are multiple tests in one-nio test directory, and they are known to pass. If you suspect there is a bug somewhere in one-nio, please create a minimal complete verifiable example that demonstrates the problem, describe the expected vs. actual behavior and explain why you think the problem is in one-nio.