Some work on addresses and private keys that still doesn't work. As a side effect, sending objects now works basically.

This commit is contained in:
2015-05-01 17:14:43 +02:00
parent 8d1466f6f4
commit a65907f13b
29 changed files with 414 additions and 138 deletions

View File

@ -91,6 +91,7 @@ public class Connection implements Runnable {
switch (state) {
case ACTIVE:
receiveMessage(msg.getPayload());
sendQueue();
break;
default:
@ -121,14 +122,19 @@ public class Connection implements Runnable {
}
} catch (SocketTimeoutException e) {
if (state == ACTIVE) {
for (MessagePayload msg = sendingQueue.poll(); msg != null; msg = sendingQueue.poll()) {
send(msg);
}
sendQueue();
}
}
}
}
private void sendQueue() {
LOG.debug("Sending " + sendingQueue.size() + " messages to node " + node);
for (MessagePayload msg = sendingQueue.poll(); msg != null; msg = sendingQueue.poll()) {
send(msg);
}
}
private void receiveMessage(MessagePayload messagePayload) {
switch (messagePayload.getCommand()) {
case INV:
@ -140,11 +146,10 @@ public class Connection implements Runnable {
break;
case GETDATA:
GetData getData = (GetData) messagePayload;
// for (InventoryVector iv : getData.getInventory()) {
// ObjectMessage om = ctx.getInventory().getObject(iv);
// sendingQueue.offer(om);
// }
LOG.error("Node requests data!!!! This shouldn't happen, the hash is done wrong!!!");
for (InventoryVector iv : getData.getInventory()) {
ObjectMessage om = ctx.getInventory().getObject(iv);
if (om != null) sendingQueue.offer(om);
}
break;
case OBJECT:
ObjectMessage objectMessage = (ObjectMessage) messagePayload;
@ -201,5 +206,12 @@ public class Connection implements Runnable {
}
}
public void offer(InventoryVector iv) {
LOG.debug("Offering " + iv + " to node " + node.toString());
sendingQueue.offer(new Inv.Builder()
.addInventoryVector(iv)
.build());
}
public enum State {SERVER, CLIENT, ACTIVE, DISCONNECTED}
}

View File

@ -18,7 +18,7 @@ package ch.dissem.bitmessage.networking;
import ch.dissem.bitmessage.BitmessageContext;
import ch.dissem.bitmessage.BitmessageContext.ContextHolder;
import ch.dissem.bitmessage.entity.payload.ObjectPayload;
import ch.dissem.bitmessage.entity.valueobject.InventoryVector;
import ch.dissem.bitmessage.entity.valueobject.NetworkAddress;
import ch.dissem.bitmessage.ports.NetworkHandler;
import org.slf4j.Logger;
@ -40,9 +40,9 @@ import static ch.dissem.bitmessage.networking.Connection.State.*;
*/
public class NetworkNode implements NetworkHandler, ContextHolder {
private final static Logger LOG = LoggerFactory.getLogger(NetworkNode.class);
private BitmessageContext ctx;
private final ExecutorService pool;
private final List<Connection> connections = new LinkedList<>();
private BitmessageContext ctx;
private ServerSocket serverSocket;
private Thread connectionManager;
@ -137,7 +137,15 @@ public class NetworkNode implements NetworkHandler, ContextHolder {
}
@Override
public void send(final ObjectPayload payload) {
// TODO: sendingQueue.add(message);
public void offer(final InventoryVector iv) {
// TODO:
// - should offer to (random) 8 nodes during 8 seconds (if possible)
// - should probably offer later if no connection available at the moment?
synchronized (connections) {
LOG.debug(connections.size() + " connections available to offer " + iv);
for (Connection connection : connections) {
connection.offer(iv);
}
}
}
}