Code cleanup
This commit is contained in:
parent
f5215be8c6
commit
4dd639e651
@ -0,0 +1,47 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2016 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;
|
||||||
|
|
||||||
|
import ch.dissem.bitmessage.entity.payload.ObjectPayload;
|
||||||
|
import ch.dissem.bitmessage.entity.valueobject.InventoryVector;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Default implementation that doesn't do anything.
|
||||||
|
*
|
||||||
|
* @author Christian Basler
|
||||||
|
*/
|
||||||
|
public class BaseMessageCallback implements MessageCallback {
|
||||||
|
@Override
|
||||||
|
public void proofOfWorkStarted(ObjectPayload message) {
|
||||||
|
// No op
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void proofOfWorkCompleted(ObjectPayload message) {
|
||||||
|
// No op
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void messageOffered(ObjectPayload message, InventoryVector iv) {
|
||||||
|
// No op
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void messageAcknowledged(InventoryVector iv) {
|
||||||
|
// No op
|
||||||
|
}
|
||||||
|
}
|
@ -410,23 +410,7 @@ public class BitmessageContext {
|
|||||||
proofOfWorkEngine = new MultiThreadedPOWEngine();
|
proofOfWorkEngine = new MultiThreadedPOWEngine();
|
||||||
}
|
}
|
||||||
if (messageCallback == null) {
|
if (messageCallback == null) {
|
||||||
messageCallback = new MessageCallback() {
|
messageCallback = new BaseMessageCallback();
|
||||||
@Override
|
|
||||||
public void proofOfWorkStarted(ObjectPayload message) {
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void proofOfWorkCompleted(ObjectPayload message) {
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void messageOffered(ObjectPayload message, InventoryVector iv) {
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void messageAcknowledged(InventoryVector iv) {
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
if (customCommandHandler == null) {
|
if (customCommandHandler == null) {
|
||||||
customCommandHandler = new CustomCommandHandler() {
|
customCommandHandler = new CustomCommandHandler() {
|
||||||
|
@ -65,6 +65,9 @@ class DefaultMessageListener implements NetworkHandler.MessageListener {
|
|||||||
receive(object, (Broadcast) payload);
|
receive(object, (Broadcast) payload);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
default: {
|
||||||
|
throw new IllegalArgumentException("Unknown payload type " + payload.getType());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -96,7 +99,7 @@ class DefaultMessageListener implements NetworkHandler.MessageListener {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updatePubkey(BitmessageAddress address, Pubkey pubkey){
|
private void updatePubkey(BitmessageAddress address, Pubkey pubkey) {
|
||||||
address.setPubkey(pubkey);
|
address.setPubkey(pubkey);
|
||||||
LOG.info("Got pubkey for contact " + address);
|
LOG.info("Got pubkey for contact " + address);
|
||||||
ctx.getAddressRepository().save(address);
|
ctx.getAddressRepository().save(address);
|
||||||
|
@ -20,6 +20,7 @@ import ch.dissem.bitmessage.entity.BitmessageAddress;
|
|||||||
import ch.dissem.bitmessage.entity.Encrypted;
|
import ch.dissem.bitmessage.entity.Encrypted;
|
||||||
import ch.dissem.bitmessage.entity.ObjectMessage;
|
import ch.dissem.bitmessage.entity.ObjectMessage;
|
||||||
import ch.dissem.bitmessage.entity.payload.*;
|
import ch.dissem.bitmessage.entity.payload.*;
|
||||||
|
import ch.dissem.bitmessage.exception.ApplicationException;
|
||||||
import ch.dissem.bitmessage.ports.*;
|
import ch.dissem.bitmessage.ports.*;
|
||||||
import ch.dissem.bitmessage.utils.Singleton;
|
import ch.dissem.bitmessage.utils.Singleton;
|
||||||
import ch.dissem.bitmessage.utils.TTL;
|
import ch.dissem.bitmessage.utils.TTL;
|
||||||
@ -187,7 +188,7 @@ public class InternalContext {
|
|||||||
messageCallback.proofOfWorkStarted(payload);
|
messageCallback.proofOfWorkStarted(payload);
|
||||||
proofOfWorkService.doProofOfWork(to, object);
|
proofOfWorkService.doProofOfWork(to, object);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new RuntimeException(e);
|
throw new ApplicationException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -206,7 +207,7 @@ public class InternalContext {
|
|||||||
// TODO: remember that the pubkey is just about to be sent, and on which stream!
|
// TODO: remember that the pubkey is just about to be sent, and on which stream!
|
||||||
proofOfWorkService.doProofOfWork(response);
|
proofOfWorkService.doProofOfWork(response);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new RuntimeException(e);
|
throw new ApplicationException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -16,6 +16,7 @@
|
|||||||
|
|
||||||
package ch.dissem.bitmessage.entity;
|
package ch.dissem.bitmessage.entity;
|
||||||
|
|
||||||
|
import ch.dissem.bitmessage.exception.ApplicationException;
|
||||||
import ch.dissem.bitmessage.utils.Encode;
|
import ch.dissem.bitmessage.utils.Encode;
|
||||||
|
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
@ -84,7 +85,7 @@ public class NetworkMessage implements Streamable {
|
|||||||
try {
|
try {
|
||||||
out.write(getChecksum(payloadBytes));
|
out.write(getChecksum(payloadBytes));
|
||||||
} catch (GeneralSecurityException e) {
|
} catch (GeneralSecurityException e) {
|
||||||
throw new RuntimeException(e);
|
throw new ApplicationException(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
// message payload
|
// message payload
|
||||||
|
@ -191,9 +191,6 @@ public class ObjectMessage implements MessagePayload {
|
|||||||
private long streamNumber;
|
private long streamNumber;
|
||||||
private ObjectPayload payload;
|
private ObjectPayload payload;
|
||||||
|
|
||||||
public Builder() {
|
|
||||||
}
|
|
||||||
|
|
||||||
public Builder nonce(byte[] nonce) {
|
public Builder nonce(byte[] nonce) {
|
||||||
this.nonce = nonce;
|
this.nonce = nonce;
|
||||||
return this;
|
return this;
|
||||||
|
@ -17,6 +17,7 @@
|
|||||||
package ch.dissem.bitmessage.entity.valueobject;
|
package ch.dissem.bitmessage.entity.valueobject;
|
||||||
|
|
||||||
import ch.dissem.bitmessage.entity.Streamable;
|
import ch.dissem.bitmessage.entity.Streamable;
|
||||||
|
import ch.dissem.bitmessage.exception.ApplicationException;
|
||||||
import ch.dissem.bitmessage.utils.Encode;
|
import ch.dissem.bitmessage.utils.Encode;
|
||||||
import ch.dissem.bitmessage.utils.UnixTime;
|
import ch.dissem.bitmessage.utils.UnixTime;
|
||||||
|
|
||||||
@ -85,7 +86,7 @@ public class NetworkAddress implements Streamable {
|
|||||||
try {
|
try {
|
||||||
return InetAddress.getByAddress(ipv6);
|
return InetAddress.getByAddress(ipv6);
|
||||||
} catch (UnknownHostException e) {
|
} catch (UnknownHostException e) {
|
||||||
throw new RuntimeException(e);
|
throw new ApplicationException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -0,0 +1,26 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2016 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;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Christian Basler
|
||||||
|
*/
|
||||||
|
public class ApplicationException extends RuntimeException {
|
||||||
|
public ApplicationException(Throwable cause) {
|
||||||
|
super(cause);
|
||||||
|
}
|
||||||
|
}
|
@ -111,6 +111,7 @@ public class SerializationTest extends TestBase {
|
|||||||
before.write(out);
|
before.write(out);
|
||||||
|
|
||||||
NetworkMessage after = Factory.getNetworkMessage(3, new ByteArrayInputStream(out.toByteArray()));
|
NetworkMessage after = Factory.getNetworkMessage(3, new ByteArrayInputStream(out.toByteArray()));
|
||||||
|
assertNotNull(after);
|
||||||
Inv invAfter = (Inv) after.getPayload();
|
Inv invAfter = (Inv) after.getPayload();
|
||||||
assertEquals(ivs, invAfter.getInventory());
|
assertEquals(ivs, invAfter.getInventory());
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,7 @@ import ch.dissem.bitmessage.cryptography.bc.BouncyCryptography;
|
|||||||
import ch.dissem.bitmessage.entity.ObjectMessage;
|
import ch.dissem.bitmessage.entity.ObjectMessage;
|
||||||
import ch.dissem.bitmessage.entity.payload.GenericPayload;
|
import ch.dissem.bitmessage.entity.payload.GenericPayload;
|
||||||
import ch.dissem.bitmessage.entity.valueobject.PrivateKey;
|
import ch.dissem.bitmessage.entity.valueobject.PrivateKey;
|
||||||
|
import ch.dissem.bitmessage.exception.InsufficientProofOfWorkException;
|
||||||
import ch.dissem.bitmessage.ports.MultiThreadedPOWEngine;
|
import ch.dissem.bitmessage.ports.MultiThreadedPOWEngine;
|
||||||
import ch.dissem.bitmessage.ports.ProofOfWorkEngine;
|
import ch.dissem.bitmessage.ports.ProofOfWorkEngine;
|
||||||
import ch.dissem.bitmessage.utils.CallbackWaiter;
|
import ch.dissem.bitmessage.utils.CallbackWaiter;
|
||||||
@ -22,6 +23,7 @@ import static ch.dissem.bitmessage.utils.UnixTime.MINUTE;
|
|||||||
import static org.hamcrest.CoreMatchers.is;
|
import static org.hamcrest.CoreMatchers.is;
|
||||||
import static org.junit.Assert.assertArrayEquals;
|
import static org.junit.Assert.assertArrayEquals;
|
||||||
import static org.junit.Assert.assertThat;
|
import static org.junit.Assert.assertThat;
|
||||||
|
import static org.junit.Assert.fail;
|
||||||
import static org.mockito.Mockito.mock;
|
import static org.mockito.Mockito.mock;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
@ -102,7 +104,11 @@ public class CryptographyTest {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
objectMessage.setNonce(waiter.waitForValue());
|
objectMessage.setNonce(waiter.waitForValue());
|
||||||
|
try {
|
||||||
crypto.checkProofOfWork(objectMessage, 1000, 1000);
|
crypto.checkProofOfWork(objectMessage, 1000, 1000);
|
||||||
|
} catch (InsufficientProofOfWorkException e) {
|
||||||
|
fail(e.getMessage());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -114,7 +114,7 @@ class Connection {
|
|||||||
|
|
||||||
public static Connection sync(InternalContext ctx, InetAddress address, int port, MessageListener listener,
|
public static Connection sync(InternalContext ctx, InetAddress address, int port, MessageListener listener,
|
||||||
long timeoutInSeconds) throws IOException {
|
long timeoutInSeconds) throws IOException {
|
||||||
return new Connection(ctx, Mode.SYNC, listener, new Socket(address, port),
|
return new Connection(ctx, SYNC, listener, new Socket(address, port),
|
||||||
new HashSet<InventoryVector>(),
|
new HashSet<InventoryVector>(),
|
||||||
new HashSet<InventoryVector>(),
|
new HashSet<InventoryVector>(),
|
||||||
new NetworkAddress.Builder().ip(address).port(port).stream(1).build(),
|
new NetworkAddress.Builder().ip(address).port(port).stream(1).build(),
|
||||||
|
@ -171,6 +171,8 @@ public class DefaultNetworkHandler implements NetworkHandler, ContextHolder {
|
|||||||
case ACTIVE:
|
case ACTIVE:
|
||||||
active++;
|
active++;
|
||||||
break;
|
break;
|
||||||
|
default:
|
||||||
|
// nothing to do
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -52,30 +52,30 @@ public class NetworkHandlerTest {
|
|||||||
public static void setUp() {
|
public static void setUp() {
|
||||||
peerInventory = new TestInventory();
|
peerInventory = new TestInventory();
|
||||||
peer = new BitmessageContext.Builder()
|
peer = new BitmessageContext.Builder()
|
||||||
.addressRepo(Mockito.mock(AddressRepository.class))
|
.addressRepo(mock(AddressRepository.class))
|
||||||
.inventory(peerInventory)
|
.inventory(peerInventory)
|
||||||
.messageRepo(Mockito.mock(MessageRepository.class))
|
.messageRepo(mock(MessageRepository.class))
|
||||||
.powRepo(Mockito.mock(ProofOfWorkRepository.class))
|
.powRepo(mock(ProofOfWorkRepository.class))
|
||||||
.port(6001)
|
.port(6001)
|
||||||
.nodeRegistry(new TestNodeRegistry())
|
.nodeRegistry(new TestNodeRegistry())
|
||||||
.networkHandler(new DefaultNetworkHandler())
|
.networkHandler(new DefaultNetworkHandler())
|
||||||
.cryptography(new BouncyCryptography())
|
.cryptography(new BouncyCryptography())
|
||||||
.listener(Mockito.mock(BitmessageContext.Listener.class))
|
.listener(mock(BitmessageContext.Listener.class))
|
||||||
.build();
|
.build();
|
||||||
peer.startup();
|
peer.startup();
|
||||||
|
|
||||||
nodeInventory = new TestInventory();
|
nodeInventory = new TestInventory();
|
||||||
networkHandler = new DefaultNetworkHandler();
|
networkHandler = new DefaultNetworkHandler();
|
||||||
node = new BitmessageContext.Builder()
|
node = new BitmessageContext.Builder()
|
||||||
.addressRepo(Mockito.mock(AddressRepository.class))
|
.addressRepo(mock(AddressRepository.class))
|
||||||
.inventory(nodeInventory)
|
.inventory(nodeInventory)
|
||||||
.messageRepo(Mockito.mock(MessageRepository.class))
|
.messageRepo(mock(MessageRepository.class))
|
||||||
.powRepo(Mockito.mock(ProofOfWorkRepository.class))
|
.powRepo(mock(ProofOfWorkRepository.class))
|
||||||
.port(6002)
|
.port(6002)
|
||||||
.nodeRegistry(new TestNodeRegistry(localhost))
|
.nodeRegistry(new TestNodeRegistry(localhost))
|
||||||
.networkHandler(networkHandler)
|
.networkHandler(networkHandler)
|
||||||
.cryptography(new BouncyCryptography())
|
.cryptography(new BouncyCryptography())
|
||||||
.listener(Mockito.mock(BitmessageContext.Listener.class))
|
.listener(mock(BitmessageContext.Listener.class))
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -19,6 +19,7 @@ package ch.dissem.bitmessage.repository;
|
|||||||
import ch.dissem.bitmessage.entity.ObjectMessage;
|
import ch.dissem.bitmessage.entity.ObjectMessage;
|
||||||
import ch.dissem.bitmessage.entity.payload.ObjectType;
|
import ch.dissem.bitmessage.entity.payload.ObjectType;
|
||||||
import ch.dissem.bitmessage.entity.valueobject.InventoryVector;
|
import ch.dissem.bitmessage.entity.valueobject.InventoryVector;
|
||||||
|
import ch.dissem.bitmessage.exception.ApplicationException;
|
||||||
import ch.dissem.bitmessage.factory.Factory;
|
import ch.dissem.bitmessage.factory.Factory;
|
||||||
import ch.dissem.bitmessage.ports.Inventory;
|
import ch.dissem.bitmessage.ports.Inventory;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
@ -99,7 +100,7 @@ public class JdbcInventory extends JdbcHelper implements Inventory {
|
|||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
LOG.error(e.getMessage(), e);
|
LOG.error(e.getMessage(), e);
|
||||||
throw new RuntimeException(e);
|
throw new ApplicationException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -126,7 +127,7 @@ public class JdbcInventory extends JdbcHelper implements Inventory {
|
|||||||
return result;
|
return result;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
LOG.error(e.getMessage(), e);
|
LOG.error(e.getMessage(), e);
|
||||||
throw new RuntimeException(e);
|
throw new ApplicationException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user