Skip to content

Commit

Permalink
updates to network handling code
Browse files Browse the repository at this point in the history
  • Loading branch information
mliotta committed Apr 1, 2018
1 parent 92d4cbd commit 1f3fce2
Show file tree
Hide file tree
Showing 3 changed files with 260 additions and 101 deletions.
30 changes: 17 additions & 13 deletions tokhn/src/io/tokhn/TokhnD.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,47 +18,51 @@

import java.io.IOException;
import java.security.Security;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.bouncycastle.jce.provider.BouncyCastleProvider;

import io.grpc.Server;
import io.grpc.ServerBuilder;
import io.tokhn.core.Blockchain;
import io.tokhn.node.Network;
import io.tokhn.node.TokhnServiceImpl;
import io.tokhn.store.MapDBBlockStore;
import io.tokhn.store.MapDBUTXOStore;
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;

@Command(name = "Tokhn Daemon", version = { "Tokhn 0.0.1", "(c) 2018 Matt Liotta" }, showDefaultValues = true)
public class TokhnD extends Thread {
private final static Map<Network, Blockchain> chains = new HashMap<>();

@Option(names = { "-h", "--help" }, usageHelp = true, description = "displays this help message and exit")
private boolean helpRequested = false;

@Option(names = { "-P", "--port" }, required = false, description = "the remote port")
private int PORT = 1337;

@Option(names = { "-n", "--network" }, required = false, description = "the list of networks to support")
Set<Network> NETWORKS = Network.getAll();

public static void main(String[] args) {
Security.addProvider(new BouncyCastleProvider());
System.out.println("Daemon running...");
CommandLine.run(new TokhnD(), System.out, args);
}

@Override
public void run() {
Set<Network> networks = Network.getAll();
for(Network network: networks) {
chains.put(network, new Blockchain(network, new MapDBBlockStore(network), new MapDBUTXOStore(network)));
String version = getClass().getPackage().getImplementationVersion();
if(version != null) {
System.out.println("Daemon [" + version + "] running...");
} else {
System.out.println("Daemon running...");
}
Server server = ServerBuilder.forPort(PORT).addService(new TokhnServiceImpl(chains)).build();

Server server = ServerBuilder.forPort(PORT).addService(new TokhnServiceImpl(NETWORKS)).build();

Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
server.shutdownNow();
}
});

try {
server.start();
server.awaitTermination();
Expand Down
80 changes: 64 additions & 16 deletions tokhn/src/io/tokhn/node/Peer.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
/*
* Copyright 2018 Matt Liotta
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/

package io.tokhn.node;
Expand All @@ -22,20 +22,68 @@

public class Peer implements Serializable {
private static final long serialVersionUID = 5689132905549792119L;
public String host;
public int port;
private final String host;
private final int port;
private String ipAddress = null;

public Peer(String host, int port) {
this.host = host;
this.port = port;
ipAddress = getIpAddress();
}

public InetAddress getInetAddress() {
try {
return InetAddress.getByName(host);
} catch (UnknownHostException e) {
System.err.println(e);
public String getIpAddress() {
if(ipAddress == null) {
try {
ipAddress = InetAddress.getByName(host).getHostAddress();
return ipAddress;
} catch(UnknownHostException e) {
System.err.println(e);
}
}
return null;

return ipAddress;
}

public String getHost() {
return host;
}

public int getPort() {
return port;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((host == null) ? 0 : host.hashCode());
result = prime * result + ((ipAddress == null) ? 0 : ipAddress.hashCode());
result = prime * result + port;
return result;
}

@Override
public boolean equals(Object obj) {
if(this == obj)
return true;
if(obj == null)
return false;
if(getClass() != obj.getClass())
return false;
Peer other = (Peer) obj;
if(host == null) {
if(other.host != null)
return false;
} else if(!host.equals(other.host))
return false;
if(ipAddress == null) {
if(other.ipAddress != null)
return false;
} else if(!ipAddress.equals(other.ipAddress))
return false;
if(port != other.port)
return false;
return true;
}
}
Loading

0 comments on commit 1f3fce2

Please sign in to comment.