Put back missing Repository import

This commit is contained in:
nicksavers 2014-09-17 19:08:52 +02:00
parent bddcda09f2
commit 3873dc98b5
4 changed files with 304 additions and 4 deletions

View File

@ -0,0 +1,214 @@
package org.ethereum.net.peerdiscovery;
import static org.ethereum.net.message.StaticMessages.PING_MESSAGE;
import static org.ethereum.net.message.StaticMessages.PONG_MESSAGE;
import static org.ethereum.net.message.StaticMessages.HELLO_MESSAGE;
import static org.ethereum.net.message.StaticMessages.GET_PEERS_MESSAGE;
import java.util.Set;
import java.util.Timer;
import java.util.TimerTask;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelOption;
import io.netty.channel.FixedRecvByteBufAllocator;
import org.ethereum.listener.EthereumListener;
import org.ethereum.manager.WorldManager;
import org.ethereum.net.Command;
import org.ethereum.net.MessageQueue;
import org.ethereum.net.PeerListener;
import org.ethereum.net.client.PeerData;
import org.ethereum.net.message.DisconnectMessage;
import org.ethereum.net.message.HelloMessage;
import org.ethereum.net.message.Message;
import org.ethereum.net.message.PeersMessage;
import org.ethereum.net.message.StaticMessages;
import org.ethereum.net.message.StatusMessage;
import org.ethereum.util.RLP;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Process the basic messages between every peer on the network.
*
* Peers can
* <ul>
* <li>HELLO : Announce themselves to the work</li>
* <li>DISCONNECT: Disconnect themselves from the network</li>
* <li>GET_PEERS : Request a list of other knows peers</li>
* <li>PEERS : Send a list of know peers</li>
* <li>PING : Check if another peer is still alive</li>
* <li>PONG : Confirm that they themseves are still alive</li>
* </ul>
*
* www.ethereumJ.com
* @author: Roman Mandeleil
* Created on: 10/04/14 08:19
*/
public class PeerProtocolHandler extends ChannelInboundHandlerAdapter {
private final static Logger logger = LoggerFactory.getLogger("peerdiscovery");
private long lastPongTime;;
private boolean tearDown = false;
private HelloMessage handshake = null;
protected PeerListener peerListener;
protected MessageQueue msgQueue = null;
protected final Timer timer = new Timer("MiscMessageTimer");
public PeerProtocolHandler() {}
public PeerProtocolHandler(PeerListener peerListener) {
this.peerListener = peerListener;
}
@Override
public void channelActive(final ChannelHandlerContext ctx) {
msgQueue = new MessageQueue(ctx);
msgQueue.sendMessage(StaticMessages.HELLO_MESSAGE);
sendPing();
// sample for pinging in background
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
if (tearDown) this.cancel();
sendPing();
}
}, 2000, 5000);
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
sendGetPeers();
}
}, 2000, 60000);
}
@Override
public void channelRead(final ChannelHandlerContext ctx, Object msg) throws InterruptedException {
byte[] payload = (byte[]) msg;
EthereumListener listener = WorldManager.getInstance().getListener();
Command receivedCommand = Command.fromInt(RLP.getCommandCode(payload));
if (peerListener != null) peerListener.console("[Recv: " + receivedCommand.name() + "]");
switch(receivedCommand) {
case HELLO:
HelloMessage helloMessage = new HelloMessage(payload);
if (peerListener != null) peerListener.console(helloMessage.toString());
msgQueue.receivedMessage(helloMessage);
handshake = helloMessage;
if (listener != null)
listener.onRecvMessage(helloMessage);
sendGetPeers();
break;
case STATUS:
StatusMessage statusMessage = new StatusMessage(payload);
if (peerListener != null) peerListener.console(statusMessage.toString());
if (listener != null)
listener.onRecvMessage(statusMessage);
msgQueue.receivedMessage(statusMessage);
case DISCONNECT:
DisconnectMessage disconnectMessage = new DisconnectMessage(payload);
msgQueue.receivedMessage(disconnectMessage);
if (peerListener != null) peerListener.console(disconnectMessage.toString());
if (listener != null)
listener.onRecvMessage(disconnectMessage);
break;
case PING:
msgQueue.receivedMessage(StaticMessages.PING_MESSAGE);
sendPong();
if (listener != null)
listener.onRecvMessage(PING_MESSAGE);
break;
case PONG:
msgQueue.receivedMessage(StaticMessages.PONG_MESSAGE);
this.lastPongTime = System.currentTimeMillis();
if (listener != null)
listener.onRecvMessage(PONG_MESSAGE);
break;
case GET_PEERS:
msgQueue.receivedMessage(StaticMessages.GET_PEERS_MESSAGE);
sendPeers();
if (listener != null)
listener.onRecvMessage(GET_PEERS_MESSAGE);
break;
case PEERS:
PeersMessage peersMessage = new PeersMessage(payload);
msgQueue.receivedMessage(peersMessage);
if (peerListener != null) peerListener.console(peersMessage.toString());
WorldManager.getInstance().addPeers(peersMessage.getPeers());
// sendDisconnectNice(ctx);
if (listener != null)
listener.onRecvMessage(peersMessage);
break;
default:
break;
}
}
@Override
public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
// limit the size of recieving buffer to 1024
ctx.channel().config().setRecvByteBufAllocator(new FixedRecvByteBufAllocator(32368));
ctx.channel().config().setOption(ChannelOption.SO_RCVBUF, 32368);
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
this.tearDown = true;
logger.info("Lost connection to the server");
logger.error(cause.getMessage(), cause);
ctx.close().sync();
ctx.disconnect().sync();
}
public void sendMsg(Message msg) {
msgQueue.sendMessage(msg);
EthereumListener listener = WorldManager.getInstance().getListener();
if (listener != null)
listener.onSendMessage(msg);
}
protected void sendHello(ChannelHandlerContext ctx) {
sendMsg(HELLO_MESSAGE);
}
protected void sendPing() {
sendMsg(PING_MESSAGE);
}
protected void sendPong() {
sendMsg(PONG_MESSAGE);
}
protected void sendGetPeers() {
sendMsg(GET_PEERS_MESSAGE);
}
protected void sendPeers() {
Set<PeerData> peers = WorldManager.getInstance().getPeers();
PeersMessage peersMessage = new PeersMessage(peers);
sendMsg(peersMessage);
}
protected void sendDisconnectNice(ChannelHandlerContext ctx) throws InterruptedException {
logger.info("[Send: DISCONNECT]");
ByteBuf buffer = ctx.alloc().buffer(StaticMessages.DISCONNECT_PEER_QUITTING.length);
buffer.writeBytes(StaticMessages.DISCONNECT_PEER_QUITTING);
ctx.writeAndFlush(buffer);
ctx.close().sync();
ctx.disconnect().sync();
}
protected HelloMessage getHandshake() {
return this.handshake;
}
}

View File

@ -0,0 +1,31 @@
package org.ethereum.net;
import static org.junit.Assert.assertEquals;
import org.ethereum.net.message.PingMessage;
import org.ethereum.net.message.PongMessage;
import org.junit.Test;
public class PingPongMessageTest {
/* PING_MESSAGE & PONG_MESSAGE */
@Test /* PingMessage */
public void testPing() {
PingMessage pingMessage = new PingMessage();
System.out.println(pingMessage);
assertEquals(PongMessage.class, pingMessage.getAnswerMessage());
}
@Test /* PongMessage */
public void testPong() {
PongMessage pongMessage = new PongMessage();
System.out.println(pongMessage);
assertEquals(null, pongMessage.getAnswerMessage());
}
}

View File

@ -0,0 +1,56 @@
package org.ethereum.net;
import static org.junit.Assert.assertEquals;
import org.ethereum.net.message.HelloMessage;
import org.ethereum.net.message.StatusMessage;
import org.junit.Test;
import org.spongycastle.util.encoders.Hex;
public class StatusMessageTest {
/* STATUS_MESSAGE */
@Test /* StatusMessage 1 from PeerServer */
public void testPeer() {
String statusMessageRaw = "";
byte[] payload = Hex.decode(statusMessageRaw);
StatusMessage statusMessage = new StatusMessage(payload);
System.out.println(statusMessage);
assertEquals(0, statusMessage.getGenesisHash());
assertEquals(0, statusMessage.getNetworkId());
assertEquals(0, statusMessage.getProtocolVersion());
assertEquals(0, statusMessage.getTotalDifficulty());
assertEquals(0, statusMessage.getBestHash());
}
@Test /* HelloMessage 2 from Node */
public void testNode() {
String helloMessageRaw = "F8 80 80 80 B7 45 74 68 " +
"65 72 65 75 6D 28 2B 2B 29 2F 50 65 65 72 20 53 " +
"65 72 76 65 72 20 5A 65 72 6F 2F 76 30 2E 36 2E " +
"38 64 2F 52 65 6C 65 61 73 65 2F 4C 69 6E 75 78 " +
"2F 67 2B 2B C0 82 76 5F B8 40 20 17 B9 5D 55 86 " +
"AD D0 53 E7 C5 DC C8 11 2D B1 D5 57 ED 83 58 9C " +
"4E 0B D2 54 42 F3 9E 41 11 65 5A 48 72 57 AA 7E " +
"4E D3 09 E8 B4 D5 5B E5 FA 8D 8D 6E 97 B7 2C 67 " +
"D7 6A A0 3E B6 9A D9 81 ED 60";
byte[] payload = Hex.decode(helloMessageRaw);
HelloMessage helloMessage = new HelloMessage(payload);
helloMessage.parse();
System.out.println(helloMessage);
assertEquals(0, helloMessage.getP2PVersion());
assertEquals("Ethereum(++)/Nick/v0.6.8d/Release/Linux/g++", helloMessage.getClientId());
assertEquals(3, helloMessage.getCapabilities().size());
assertEquals(30303, helloMessage.getListenPort());
assertEquals(
"2017B95D5586ADD053E7C5DCC8112DB1D557ED83589C4E0BD25442F39E4111655A487257AA7E4ED309E8B4D55BE5FA8D8D6E97B72C67D76AA03EB69AD981ED60",
Hex.toHexString(helloMessage.getPeerId()).toUpperCase() );
}
}

View File

@ -2,7 +2,7 @@ package org.ethereum.gui;
import org.ethereum.core.Block;
import org.ethereum.core.Transaction;
import org.ethereum.db.RepositoryImpl;
import org.ethereum.facade.Repository;
import org.ethereum.manager.WorldManager;
import org.ethereum.vm.*;
import org.spongycastle.util.encoders.Hex;
@ -37,9 +37,8 @@ public class ProgramPlayDialog extends JPanel implements ActionListener,
}
public ProgramPlayDialog(byte[] code, Transaction tx, Block lastBlock) {
this(code, ProgramInvokeFactory.createProgramInvoke(tx,
lastBlock,
WorldManager.getInstance().getRepository()));
this(code, ProgramInvokeFactory.createProgramInvoke(tx, lastBlock,
WorldManager.getInstance().getRepository()));
}
public ProgramPlayDialog(byte[] code, ProgramInvoke programInvoke) {