Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
package org.dilithium.networking.Commands;

import org.dilithium.util.ByteArrayKey;

public abstract class NetworkCommand {

//TODO implement Commands
public static String execute(String[] args) {
return null;
}
//Receives Data and Determines how to process
public abstract byte[] execute(ByteArrayKey args);

//Process Recieved Data
public abstract byte[] recieve(ByteArrayKey args);

//Send Requested Data
public abstract byte[] send(ByteArrayKey args);

}

Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
package org.dilithium.networking.Commands;

import org.dilithium.util.ByteArrayKey;

public class PingCommandHandler extends NetworkCommand{


@Override
public byte[] execute(ByteArrayKey args) {
if(args.data[0].equals((byte)0xFF));
return null;
}

@Override
public byte[] recieve(ByteArrayKey args) {
// TODO Auto-generated method stub
return null;
}

@Override
public byte[] send(ByteArrayKey args) {
// TODO Auto-generated method stub
return null;
}

}
68 changes: 21 additions & 47 deletions src/main/java/org/dilithium/networking/Peer.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,18 @@
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.logging.Level;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.dilithium.networking.Commands.NetworkCommand;
import org.dilithium.networking.Commands.PingCommandHandler;
import org.dilithium.util.Log;

import org.dilithium.util.ByteArrayKey;

public class Peer {

private Thread peerThread;
public Socket socket;
private static HashMap<String, NetworkCommand> commands = new HashMap<>();
public DataOutputStream out;
public DataInputStream in;
private static HashMap<ByteArrayKey, NetworkCommand> commands = new HashMap<>();

public Peer(Socket socket) {
this.socket = socket;
Expand All @@ -52,7 +42,7 @@ public Peer(Socket socket) {
public void run() {
try {
listen();
Log.log(Level.INFO, "Closing connection to " + socket.getInetAddress() + ":" + socket.getPort());
System.out.println("Closing connection to " + socket.getInetAddress() + ":" + socket.getPort());
} catch (IOException e) {
e.printStackTrace();
}
Expand All @@ -62,17 +52,15 @@ public void run() {
}

private void initializeCommands() {
this.commands.put("ping", new PingCommandHandler());


}

public void listen() throws IOException {
String command;
byte[] command;
DataInputStream in = new DataInputStream(socket.getInputStream());
while(true){
//System.out.println("Listening for commands");
try{
DataInputStream in = new DataInputStream(this.socket.getInputStream());
DataOutputStream out = new DataOutputStream(this.socket.getOutputStream());
DataOutputStream out = new DataOutputStream(socket.getOutputStream());
command = receive(in);
send(serve(command), out);
} catch (SocketTimeoutException e) {
Expand All @@ -83,46 +71,32 @@ public void listen() throws IOException {
}
}

public static String serve(String input) {
List<String> list = new ArrayList<>();
Matcher m = Pattern.compile("([^\"]\\S*|\".+?\")\\s*").matcher(input);
while (m.find()) {
list.add(m.group(1));
}

String command = list.remove(0); // Get the command and remove it from the list.

if(!commands.containsKey(command)){
return "'" + command + "' is not a command.";
}

String[] args = null;
if (list.size() > 0){
args = list.toArray(new String[list.size()]);
}

return commands.get(command).execute(args);
public static byte[] serve(byte[] input) {
//TODO In execute, send the args as input without the first byte
return commands.get(new ByteArrayKey(input[0])).execute(new ByteArrayKey(input,1,input.length-1));
}

public static void send(String data, DataOutputStream out){
Log.log(Level.INFO, "Sending message: " + data);
public static void send(byte[] data, DataOutputStream out){
System.out.println("Sending message: " + data);
try {
out.writeUTF(data);
out.writeInt(data.length);
out.write(data);
out.flush();
} catch (IOException e) {
e.printStackTrace();
}
}

public String receive(DataInputStream in){
String data = null;
try {
data = in.readUTF();
Log.log(Level.INFO, "Received message: " + data);
public byte[] receive(DataInputStream in){
byte[] data = null;
try {
int size = in.readInt();
in.readFully(data, 0, size);
System.out.println("Received message: "+ data);
} catch (IOException e) {
e.printStackTrace();
}
return data;
return data;
}

@Override
Expand Down
77 changes: 39 additions & 38 deletions src/main/java/org/dilithium/networking/Peer2Peer.java
Original file line number Diff line number Diff line change
@@ -1,59 +1,59 @@
package org.dilithium.networking;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.logging.Level;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


import org.dilithium.networking.Commands.NetworkCommand;
import org.dilithium.networking.Commands.PingCommandHandler;
import org.dilithium.util.Log;

import org.dilithium.util.ByteArrayKey;

public class Peer2Peer {

private int port;
private ArrayList<Peer> peers;
private DataOutputStream outputStream;
public Thread serverThread;
private DataOutputStream out;
private Thread serverThread;
private boolean runningServer;
private HashMap<String, NetworkCommand> commands = new HashMap<>();
private HashMap<ByteArrayKey, NetworkCommand> commands = new HashMap<>();
private ServerSocket server;
private Socket socket = null;

//Node with access to blockchain
private Socket socket;
//Node with out storing Blockchain
public Peer2Peer(int port){
this.port = port;
peers = new ArrayList<>();
serverThread = new Thread(new Runnable() {
public void run() {
try {
listen();
Log.log(Level.INFO, "Connection Ended");
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Making node");
this.port = port;
peers = new ArrayList<>();
serverThread = new Thread(new Runnable() {
public void run() {
try {
listen();
System.out.println("Connection Ended");

} catch (IOException e) {
e.printStackTrace();
}
});
initializeCommands();
}
}
});
initializeCommands();
}

private void initializeCommands() {
this.commands.put("ping", new PingCommandHandler());
/**List of Commands
* 0xFF - Ping
* 0xFE - Block_Height
*/
this.commands.put(new ByteArrayKey((byte)0xFF), new PingCommandHandler());
}

public void start(){
if(serverThread.isAlive()){
Log.log(Level.INFO, "Server is already running.");
System.out.println("Server is already running.");
return;
}
runningServer = true;
Expand All @@ -66,26 +66,27 @@ public void stop() throws IOException{
serverThread.interrupt();
socket.close();
} catch (NullPointerException n) {
Log.log(Level.WARNING, "Null pointer when closing server socket");
n.printStackTrace();
}
Log.log(Level.INFO, "Server Stopped");
System.out.println("Server Stopped");
}

public void listen() throws IOException, SocketTimeoutException{
Log.log(Level.INFO, "Server starting...");
System.out.println("Server starting...");
server = new ServerSocket(this.port);
Log.log(Level.INFO, "Server started on port " + this.port);
System.out.println("Server started on port " + this.port);

Peer peer;
server.setSoTimeout(10000);
while(runningServer){
//System.out.println("Waiting for a connection");
try{
socket = server.accept();
Log.log(Level.INFO, "Passed Accept");
System.out.println("Passed Accept");
peer = new Peer(socket);
Log.log(Level.INFO, "Connection received from: " + peer.toString());
System.out.println("Connection received from: " + peer.toString());
peers.add(peer);
Log.log(Level.INFO, "New peer: " + peer.toString());
System.out.println("New peer: " + peer.toString());
} catch (SocketTimeoutException e) {
//e.printStackTrace();
}
Expand All @@ -96,8 +97,8 @@ public void listen() throws IOException, SocketTimeoutException{

public void connect(Socket socket){
try {
outputStream = new DataOutputStream(socket.getOutputStream());
Peer.send("ping", outputStream);
out = new DataOutputStream(socket.getOutputStream());
Peer.send(commands.get(new ByteArrayKey((byte) 0xFF)).execute(new ByteArrayKey((byte) 0xFF)), out);
} catch (IOException e) {
//e.printStackTrace();
}
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/org/dilithium/util/ByteArrayKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@ public ByteArrayKey(byte[] data)
}
this.data = data;
}

public ByteArrayKey(byte data) {
this.data = new byte[0];
this.data[0] = data;
}

public ByteArrayKey(byte[] data, int a, int b) {
this.data = new byte[b-a+1];
for(int i = 0; i < b-a+1; i++) {
this.data[i] = data[i+a];
}
}

@Override
public boolean equals(Object other)
Expand All @@ -54,3 +66,4 @@ public byte[] toByteArray(){
return data;
}
}