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
a14b836
commit 94ef23d
Showing
8 changed files
with
228 additions
and
5 deletions.
There are no files selected for viewing
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
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
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
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
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
Binary file not shown.
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,79 @@ | ||
package snowblossom.shackleton; | ||
|
||
import snowblossom.Config; | ||
import snowblossom.ConfigFile; | ||
import snowblossom.Globals; | ||
import snowblossom.LogSetup; | ||
|
||
import java.util.logging.Logger; | ||
import java.util.logging.Level; | ||
|
||
import io.grpc.ManagedChannel; | ||
import io.grpc.ManagedChannelBuilder; | ||
import io.grpc.stub.StreamObserver; | ||
|
||
import snowblossom.proto.UserServiceGrpc.UserServiceStub; | ||
import snowblossom.proto.UserServiceGrpc.UserServiceBlockingStub; | ||
import snowblossom.proto.UserServiceGrpc; | ||
|
||
import snowblossom.NetworkParams; | ||
|
||
|
||
/** Yes a penguin taught me french back in antacrtica */ | ||
public class Shackleton | ||
{ | ||
private static final Logger logger = Logger.getLogger("snowblossom.shackleton"); | ||
|
||
public static void main(String args[]) | ||
throws Exception | ||
{ | ||
Globals.addCryptoProvider(); | ||
if (args.length != 1) | ||
{ | ||
logger.log(Level.SEVERE, "Incorrect syntax. Syntax: Shackleton <config_file>"); | ||
System.exit(-1); | ||
} | ||
|
||
ConfigFile config = new ConfigFile(args[0]); | ||
|
||
LogSetup.setup(config); | ||
|
||
new Shackleton(config); | ||
|
||
} | ||
|
||
private Config config; | ||
private WebServer web_server; | ||
private UserServiceStub asyncStub; | ||
private UserServiceBlockingStub blockingStub; | ||
private NetworkParams params; | ||
|
||
public Shackleton(Config config) | ||
throws Exception | ||
{ | ||
this.config = config; | ||
|
||
web_server = new WebServer(config, this); | ||
config.require("node_host"); | ||
|
||
params = NetworkParams.loadFromConfig(config); | ||
|
||
String host = config.get("node_host"); | ||
int port = config.getIntWithDefault("node_port", params.getDefaultPort()); | ||
ManagedChannel channel = ManagedChannelBuilder.forAddress(host, port).usePlaintext(true).build(); | ||
|
||
asyncStub = UserServiceGrpc.newStub(channel); | ||
blockingStub = UserServiceGrpc.newBlockingStub(channel); | ||
|
||
web_server.start(); | ||
|
||
} | ||
|
||
|
||
public UserServiceBlockingStub getStub() | ||
{ | ||
return blockingStub; | ||
} | ||
|
||
|
||
} |
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,99 @@ | ||
package snowblossom.shackleton; | ||
|
||
import snowblossom.Config; | ||
import java.util.Map; | ||
|
||
import java.net.InetSocketAddress; | ||
import com.sun.net.httpserver.HttpExchange; | ||
import com.sun.net.httpserver.HttpHandler; | ||
import com.sun.net.httpserver.HttpServer; | ||
import java.io.OutputStream; | ||
import java.io.IOException; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.PrintStream; | ||
|
||
import snowblossom.proto.UserServiceGrpc.UserServiceStub; | ||
import snowblossom.proto.NullRequest; | ||
|
||
public class WebServer | ||
{ | ||
private HttpServer server; | ||
private Shackleton shackleton; | ||
|
||
public WebServer(Config config, Shackleton shackleton) | ||
throws Exception | ||
{ | ||
this.shackleton = shackleton; | ||
|
||
config.require("port"); | ||
int port = config.getInt("port"); | ||
|
||
server = HttpServer.create(new InetSocketAddress(port), 0); | ||
server.createContext("/", new RootHandler()); | ||
server.createContext("/status", new RootHandler()); | ||
server.createContext("/test", new MyHandler()); | ||
server.setExecutor(null); | ||
|
||
|
||
} | ||
|
||
public void start() | ||
throws java.io.IOException | ||
{ | ||
server.start(); | ||
|
||
|
||
} | ||
class RootHandler extends GeneralHandler | ||
{ | ||
@Override | ||
public void innerHandle(HttpExchange t, PrintStream out) | ||
{ | ||
out.println(shackleton.getStub().getNodeStatus(nr()).toString()); | ||
|
||
} | ||
} | ||
|
||
|
||
static class MyHandler implements HttpHandler { | ||
@Override | ||
public void handle(HttpExchange t) throws IOException { | ||
String response = "This is the response"; | ||
t.sendResponseHeaders(200, response.length()); | ||
OutputStream os = t.getResponseBody(); | ||
os.write(response.getBytes()); | ||
os.close(); | ||
} | ||
} | ||
|
||
public NullRequest nr() | ||
{ | ||
return NullRequest.newBuilder().build(); | ||
} | ||
|
||
public abstract class GeneralHandler implements HttpHandler | ||
{ | ||
@Override | ||
public void handle(HttpExchange t) throws IOException { | ||
ByteArrayOutputStream b_out = new ByteArrayOutputStream(); | ||
PrintStream print_out = new PrintStream(b_out); | ||
try | ||
{ | ||
innerHandle(t, print_out); | ||
} | ||
catch(Throwable e) | ||
{ | ||
print_out.println("Exception: " + e); | ||
} | ||
|
||
byte[] data = b_out.toByteArray(); | ||
t.sendResponseHeaders(200, data.length); | ||
t.getResponseBody().write(data); | ||
|
||
|
||
} | ||
|
||
public abstract void innerHandle(HttpExchange t, PrintStream out) throws Exception; | ||
} | ||
|
||
} |