-
Notifications
You must be signed in to change notification settings - Fork 6
/
PeerThread.java
60 lines (56 loc) · 1.51 KB
/
PeerThread.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
/*
* 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.*;
/**
* Class handles all networking after a socket is accepted. Delegates work into two separate threads,
* one for incoming data, and one for outgoing data, so data in one direction doesn't block data in
* the other.
*/
public class PeerThread extends Thread
{
private Socket socket;
public InputThread inputThread;
public OutputThread outputThread;
/**
* Constructor sets socket
*
* @param socket Socket with peer
*/
public PeerThread(Socket socket)
{
this.socket = socket;
}
/**
* As the name might suggest, each PeerThread runs on its own thread. Additionally, each child network IO thread
* runs on its own thread.
*/
public void run()
{
System.out.println("Got connection from " + socket.getInetAddress() + ".");
inputThread = new InputThread(socket);
inputThread.start();
outputThread = new OutputThread(socket);
outputThread.start();
}
/**
* Used to send data to a peer. Passthrough to outputThread.send()
*
* @param data String of data to send
*/
public void send(String data)
{
if (outputThread == null)
{
System.out.println("Couldn't send " + data + " !!!!");
}
else
{
outputThread.write(data);
}
}
}