Greatly improved network code - the "manage the node repository" part of issue #5 should now be OK

This commit is contained in:
2015-06-16 06:41:59 +02:00
parent ed0d1c2911
commit c0a7acc609
14 changed files with 317 additions and 83 deletions

View File

@ -25,6 +25,7 @@ import ch.dissem.bitmessage.entity.valueobject.PrivateKey;
import ch.dissem.bitmessage.exception.DecryptionFailedException;
import ch.dissem.bitmessage.factory.Factory;
import ch.dissem.bitmessage.ports.*;
import ch.dissem.bitmessage.utils.Property;
import ch.dissem.bitmessage.utils.Security;
import ch.dissem.bitmessage.utils.UnixTime;
import org.slf4j.Logger;
@ -245,6 +246,12 @@ public class BitmessageContext {
}
}
public Property status() {
return new Property("status", null,
ctx.getNetworkHandler().getNetworkStatus()
);
}
public interface Listener {
void receive(Plaintext plaintext);
}

View File

@ -121,6 +121,10 @@ public class NetworkAddress implements Streamable {
Encode.int16(port, stream);
}
public void setTime(long time) {
this.time = time;
}
public static final class Builder {
private long time;
private long stream;

View File

@ -0,0 +1,32 @@
/*
* Copyright 2015 Christian Basler
*
* 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
*
* 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.
*/
package ch.dissem.bitmessage.exception;
/**
* An exception on the node that's severe enough to cause the client to disconnect this node.
*
* @author Ch. Basler
*/
public class NodeException extends RuntimeException {
public NodeException(String message) {
super(message);
}
public NodeException(String message, Throwable cause) {
super(message, cause);
}
}

View File

@ -22,11 +22,13 @@ import ch.dissem.bitmessage.entity.ObjectMessage;
import ch.dissem.bitmessage.entity.Plaintext;
import ch.dissem.bitmessage.entity.payload.*;
import ch.dissem.bitmessage.entity.valueobject.PrivateKey;
import ch.dissem.bitmessage.exception.NodeException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.io.InputStream;
import java.net.SocketException;
import java.net.SocketTimeoutException;
/**
@ -38,8 +40,10 @@ public class Factory {
public static NetworkMessage getNetworkMessage(int version, InputStream stream) throws SocketTimeoutException {
try {
return V3MessageFactory.read(stream);
} catch (SocketTimeoutException e) {
} catch (SocketTimeoutException | NodeException e) {
throw e;
} catch (SocketException e) {
throw new NodeException(e.getMessage(), e);
} catch (Exception e) {
LOG.error(e.getMessage(), e);
return null;

View File

@ -21,6 +21,7 @@ import ch.dissem.bitmessage.entity.payload.GenericPayload;
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.exception.NodeException;
import ch.dissem.bitmessage.utils.AccessCounter;
import ch.dissem.bitmessage.utils.Decode;
import ch.dissem.bitmessage.utils.Security;
@ -211,6 +212,6 @@ class V3MessageFactory {
}
pos++;
}
throw new IOException("Failed to fine MAGIC bytes in stream");
throw new NodeException("Failed to find MAGIC bytes in stream");
}
}

View File

@ -20,8 +20,10 @@ import ch.dissem.bitmessage.BitmessageContext;
import ch.dissem.bitmessage.entity.ObjectMessage;
import ch.dissem.bitmessage.entity.payload.ObjectPayload;
import ch.dissem.bitmessage.entity.valueobject.InventoryVector;
import ch.dissem.bitmessage.utils.Property;
import java.io.IOException;
import java.util.Map;
/**
* Handles incoming messages
@ -33,6 +35,8 @@ public interface NetworkHandler {
void offer(InventoryVector iv);
Property getNetworkStatus();
interface MessageListener {
void receive(ObjectMessage object) throws IOException;
}

View File

@ -23,6 +23,7 @@ import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Map;
public class DebugUtils {
private final static Logger LOG = LoggerFactory.getLogger(DebugUtils.class);
@ -36,4 +37,12 @@ public class DebugUtils {
LOG.debug(e.getMessage(), e);
}
}
public static <K> void inc(Map<K, Integer> map, K key) {
if (map.containsKey(key)) {
map.put(key, map.get(key) + 1);
} else {
map.put(key, 1);
}
}
}

View File

@ -0,0 +1,53 @@
/*
* Copyright 2015 Christian Basler
*
* 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
*
* 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.
*/
package ch.dissem.bitmessage.utils;
/**
* Created by chris on 14.06.15.
*/
public class Property {
private String name;
private Object value;
private Property[] properties;
public Property(String name, Object value, Property... properties) {
this.name = name;
this.value = value;
this.properties = properties;
}
@Override
public String toString() {
return toString("");
}
private String toString(String indentation) {
StringBuilder result = new StringBuilder();
result.append(indentation).append(name).append(": ");
if (value != null || properties.length == 0) {
result.append(value);
}
if (properties.length > 0) {
result.append("{\n");
for (Property property : properties) {
result.append(property.toString(indentation + " ")).append('\n');
}
result.append(indentation).append("}");
}
return result.toString();
}
}

View File

@ -20,10 +20,14 @@ package ch.dissem.bitmessage.utils;
* A simple utility class that simplifies using the second based time used in Bitmessage.
*/
public class UnixTime {
/**
* Length of an hour in seconds, intended for use with {@link #now(long)}.
*/
public static final long HOUR = 60 * 60;
/**
* Length of a day in seconds, intended for use with {@link #now(long)}.
*/
public static final long DAY = 60 * 60 * 24;
public static final long DAY = 24 * HOUR;
/**
* Returns the time in second based Unix time ({@link System#currentTimeMillis()}/1000)