org.zodiac.sdk.nio.http.server.ServerEntryPoint Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of zodiac-sdk-nio Show documentation
Show all versions of zodiac-sdk-nio Show documentation
Zodiac SDK NIO2(New Non-Blocking IO)
package org.zodiac.sdk.nio.http.server;
import org.zodiac.sdk.cli.*;
import org.zodiac.sdk.nio.http.ApplicationProtocol;
import org.zodiac.sdk.nio.http.NioHttpConstants;
import org.zodiac.sdk.nio.http.TransportProtocol;
import io.vavr.control.Either;
import io.vavr.control.Try;
@Command(name = NioHttpConstants.HTTPFS, description = "httpfs is a simple file server.")
public class ServerEntryPoint {
@Flag(
name = "verbose",
alias = {"--verbose", "-v"},
required = false,
description = "Prints debugging messages.")
boolean verbose;
@Flag(
name = "udp",
alias = {"--udp"},
required = false,
description = "Use a Selective Repeat over UDP transport protocol implementation.")
boolean udp;
@Option(
name = "port",
alias = {"--port", "-p"},
argument = @Argument(
name = "port",
format = "number",
regex = "(^\\d+$)",
description = ""),
description = "Specifies the port number that the server will listen and serve at (default is 8080).")
int port;
@Option(
name = "directory",
alias = {"--dir", "-d"},
argument = @Argument(name = "directory",
format = "/path/to/directory",
regex = "(.*)",
description = "Path to the directory"),
description = "Specifies the directory that the server will use to read/write requested files (default is the current directory when launching the application).")
String directory;
public ServerEntryPoint() {
super();
}
public ServerEntryPoint(boolean verbose, boolean udp, int port, String directory) {
super();
this.verbose = verbose;
this.udp = udp;
this.port = port;
this.directory = directory;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((directory == null) ? 0 : directory.hashCode());
result = prime * result + port;
result = prime * result + (udp ? 1231 : 1237);
result = prime * result + (verbose ? 1231 : 1237);
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
ServerEntryPoint other = (ServerEntryPoint)obj;
if (directory == null) {
if (other.directory != null)
return false;
} else if (!directory.equals(other.directory))
return false;
if (port != other.port)
return false;
if (udp != other.udp)
return false;
if (verbose != other.verbose)
return false;
return true;
}
@Override
public String toString() {
return "ServerEntryPoint [verbose=" + verbose + ", udp=" + udp + ", port=" + port + ", directory=" + directory + "]";
}
public static void entryPoint(final String[] args) {
final Parser parser = new Parser<>(ServerEntryPoint.class);
final Try> result = parser.parse(String.join(" ", args));
result
.onSuccess(success -> {
if (success.isLeft()) {
System.out.println(success.getLeft());
}
if (success.isRight()) {
exec(success.get());
}
})
.onFailure(failure -> {
System.err.println(failure.getMessage());
System.err.println(parser.help());
System.exit(1);
});
}
static void exec(final ServerEntryPoint ep) {
final Server.Configuration configuration = new Server.Configuration(
TransportProtocol.Type.of(ep.udp ? "UDP" : "TCP"),
ApplicationProtocol.Type.of("FS"),
ep.port,
ep.verbose,
ep.directory);
new Server(configuration).run();
}
}