-
Notifications
You must be signed in to change notification settings - Fork 6
/
OutputThread.java
118 lines (110 loc) · 3.97 KB
/
OutputThread.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.io.*;
import java.net.*;
import java.util.*;
/**
* OutputThread writes data to a peer, and never reads in any data in order to prevent blocking and waiting, or some terrible constant back-and-forth keepalive.
* Data written isn't saved anywhere. This behavior doesn't need to be run in a thread, but would block activity on the main thread if write calls were direct.
* Instead, any calls to OutputThread's external methods are extremely lightweight (putting a String in a buffer) rather than waiting on network IO to execute.
*/
public class OutputThread extends Thread
{
private Socket socket;
//Private to mirror InputThread's structure. For OOP model, it makes more sense for a method to simulate 'writing' data (even though it is delayed until the thread writes the data).
private ArrayList<String> outputBuffer;
private boolean shouldContinue = true;
/**
* Constructor to set class socket variable
*/
public OutputThread(Socket socket)
{
this.socket = socket;
}
/**
* Constantly checks outputBuffer for contents, and writes any contents in outputBuffer.
*/
public void run()
{
try
{
outputBuffer = new ArrayList<String>();
PrintWriter out = new PrintWriter(socket.getOutputStream(),true);
while (shouldContinue)
{
//System.out.println("LOOP ALIVE FOR " + socket.getInetAddress());
//System.out.println("LOOP SIZE : " + outputBuffer.size() + " for " + socket.getInetAddress());
if (outputBuffer.size() > 0)
{
if (outputBuffer.get(0) != null)
{
for (int i = 0; i < outputBuffer.size(); i++)
{
if (outputBuffer.get(i).length() > 20)
{
System.out.println("Sending " + outputBuffer.get(i).substring(0, 20) + " to " + socket.getInetAddress());
}
else
{
System.out.println("Sending " + outputBuffer.get(i) + " to " + socket.getInetAddress());
}
out.println(outputBuffer.get(i));
}
outputBuffer = new ArrayList<String>();
outputBuffer.add(null);
}
}
Thread.sleep(100);
}
System.out.println("WHY AM I HERE");
} catch (Exception e)
{
e.printStackTrace();
}
}
/**
* Technically not writing to the network socket, but instead putting the passed-in data in a buffer to be written to the socket as soon as possible.
*
* @param data Data to write
*/
public void write(String data)
{
if (data.length() > 20)
{
System.out.println("PUTTING INTO WRITE BUFFER: " + data.substring(0, 20) + "...");
}
else
{
System.out.println("PUTTING INTO WRITE BUFFER: " + data);
}
File f = new File("writebuffer");
try
{
PrintWriter out = new PrintWriter(f);
out.println("SENDING: " + data);
out.close();
} catch (Exception e)
{
}
if (outputBuffer.size() > 0)
{
if (outputBuffer.get(0) == null)
{
outputBuffer.remove(0);
}
}
outputBuffer.add(data);
}
/**
* Stops thread during the next write cycle. I couldn't call it stop() like I wanted to, cause you can't overwrite that method of Thread. :'(
*/
public void shutdown()
{
shouldContinue = false;
}
}