-
Notifications
You must be signed in to change notification settings - Fork 6
/
PeerNetwork.java
118 lines (110 loc) · 3.71 KB
/
PeerNetwork.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
* Curecoin 2.0.0a Source Code
* Copyright (c) 2015 Curecoin Developers
* Distributed under MIT License
* Requires Apache Commons Library
* Supports Java 1.7+
*/
import java.net.*;
import java.util.*;
/**
* This thread listens on a provided port (8015 by default) for incoming connections, and attempts to make connections to external peers based on guidance from MainClass.
* It needs a bit of help with memory management and resource deallocation, but otherwise it works. Good enough for 2.0.0a1.
*
* Future plans include some form of UPNP support and NAT punchthroughs.
*/
public class PeerNetwork extends Thread
{
public int listenPort;
public boolean shouldRun = true;
public ArrayList<PeerThread> peerThreads;
public ArrayList<String> newPeers;
/**
* Default settings constructor
*/
public PeerNetwork()
{
this.listenPort = 8015;
this.peerThreads = new ArrayList<PeerThread>();
this.newPeers = new ArrayList<String>();
}
/**
* Attempts a connection to an external peer
*
* @param peer Peer to connect to
* @param port Port on peer to connect to
*/
public void connectToPeer(String peer, int port)
{
try
{
Socket socket = new Socket(peer, port);
String remoteHost = socket.getInetAddress() + "";
remoteHost = remoteHost.replace("/", "");
remoteHost = remoteHost.replace("\\", "");
int remotePort = socket.getPort();
newPeers.add(remoteHost + ":" + remotePort);
peerThreads.add(new PeerThread(socket));
peerThreads.get(peerThreads.size() - 1).start();
} catch (Exception e)
{
System.out.println("Unable to connect to " + peer + ":" + port);
}
}
/**
* Optional, currently-unused constructor for a non-default port selection
*
* @param port Port to listen on
*/
public PeerNetwork(int port)
{
this.listenPort = port;
this.peerThreads = new ArrayList<PeerThread>();
}
/**
* Runs as a separate thread, constantly listening for peer connections.
*/
public void run()
{
try
{
ServerSocket listenSocket = new ServerSocket(listenPort);
while (shouldRun) //Doesn't actually quit right when shouldRun is changed, as while loop is pending.
{
peerThreads.add(new PeerThread(listenSocket.accept()));
peerThreads.get(peerThreads.size() - 1).start();
}
listenSocket.close();
} catch (Exception e)
{
e.printStackTrace(); //Most likely tripped by the inability to bind the listenPort.
}
}
/**
* Announces the same message to all peers simultaneously. Useful when re-broadcasting messages.
*
* @param toBroadcast String to broadcast to peers
*/
public void broadcast(String toBroadcast)
{
for (int i = 0; i < peerThreads.size(); i++)
{
System.out.println("Sent:: " + toBroadcast);
peerThreads.get(i).send(toBroadcast);
}
}
/**
* Announces the same message to all peers except the ignored one simultaneously. Useful when re-broadcasting messages.
* Peer ignored as it's the peer that sent you info.
*
* @param toBroadcast String to broadcast to peers
* @param peerToIgnore Peer to not send broadcast too--usually the peer who sent information that is being rebroadcast
*/
public void broadcastIgnorePeer(String toBroadcast, String peerToIgnore)
{
for (int i = 0; i < peerThreads.size(); i++)
{
peerThreads.get(i).send(toBroadcast);
}
}
}