-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApplication.java
More file actions
193 lines (171 loc) · 8.17 KB
/
Copy pathApplication.java
File metadata and controls
193 lines (171 loc) · 8.17 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import controller.ConnectionController;
import receiver.ReceiverController;
import sender.SenderController;
import device_searcher.BroadcastHandler;
import device_searcher.ConnectionRequestHandler;
import device_searcher.DeviceSearcher;
import types.Device;
import utils.HotkeyManager;
import utils.ThreadedScanner;
import utils.CloseableInterrupter;
import java.net.*;
import java.util.List;
//import static device_searcher.DeviceSearcher.searchForDevices;
public class Application {
private static boolean running = true;
private static boolean connected = false;
public static final int TCP_PORT = 6789; // TCP port for connection for this app
public static final int UDP_PORT = 9876; // UDP port used for this app (should be same for all users, so
private static final String BROADCAST_MESSAGE = "DISCOVER_DEVICE";
private static final String DEVICE_INFO_PREFIX = "DEVICE_INFO"; // device information will be passed in the following format:
// DEVICE_INFO:hostname:hostAddress:hostTcpPort
// indicates whether this device is already connected to another device currently
public static boolean getConnectionState() {
return connected;
}
public static boolean getRunningState() {
return running;
}
private static void prompt()
{
System.out.println("Choose options: (enter '1' for option 1, vice versa) ");
System.out.println("0. search active local devices");
System.out.println("1. view connection requests ");
System.out.println("2. exit");
}
// private static void clearOutput() {
// try {
// Runtime.getRuntime().exec("cls");
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
public static void main(String[] args) {
running = true;
connected = false;
// threaded scanner so user can provide inputs whenever they want, instead of needing to wait for
// application's prompt before inputting, also allowing for
ThreadedScanner threadedScanner = new ThreadedScanner();
new Thread(threadedScanner).start();
// static background jobs, do not need a reference
BroadcastHandler.init(UDP_PORT, TCP_PORT, BROADCAST_MESSAGE, DEVICE_INFO_PREFIX);
ConnectionRequestHandler.init(TCP_PORT);
DeviceSearcher deviceSearcher = new DeviceSearcher(BROADCAST_MESSAGE, DEVICE_INFO_PREFIX);
ConnectionController connectionController = new ConnectionController(ConnectionRequestHandler.getConnectionRequests());
while (running) {
try {
prompt();
String input = threadedScanner.getInput().trim().toLowerCase();
int option;
// clearOutput();
try {
option = Integer.parseInt(input);
} catch (NumberFormatException e) {
System.out.println("Please input a number!");
continue;
}
switch (option) {
case 0: {
boolean searching = true;
while (searching) {
deviceSearcher.initAvailableDevices();
deviceSearcher.printLocalDevicesNonBlocking(UDP_PORT);
System.out.println();
boolean retry = false;
int index = -1;
while (true) {
input = threadedScanner.getInput().trim().toLowerCase();
if (input.equals("r")) {
retry = true;
break;
}
if (input.equals("n")) {
searching = false;
break;
}
try {
index = Integer.parseInt(input);
// if (index < 0 || index >= devices.size()) {
// }
} catch (NumberFormatException e) {
System.out.println("Please input a number!");
continue;
}
break;
}
if (retry) {continue;}
deviceSearcher.stopDevicePrinter();
if (!searching) {break;}
Device device;
try {
device = deviceSearcher.getAvailableDevices().get(index);
} catch (IndexOutOfBoundsException e) {
System.out.println("Invalid index!");
continue;
}
// clearOutput();
Socket clientSocket = connectionController.connect(device);
if (clientSocket != null) {
SenderController sender = new SenderController(clientSocket);
HotkeyManager.hook(sender);
sender.start();
}
searching = false;
}
break;
}
case 1: {
while (true) {
ConnectionRequestHandler.clearClosedRequests();
List<Device> devices = connectionController.getRequestedDevices();
if (devices.isEmpty()) {
System.out.println("No requests yet (input 'r' to reload, or 'n' to return)");
} else {
System.out.println("would you like to accept any of these connections? (if yes, input their index (e.g. '3'), if no, input 'r' to reload, or 'n' to return.");
}
input = threadedScanner.getInput().trim().toLowerCase();
if (input.equals("r")) {
// clearOutput();
continue;
}
int index;
try {
index = Integer.parseInt(input);
} catch (NumberFormatException e) {
break;
}
if (index < 0 || index >= devices.size()) {
System.out.println("Invalid index!");
break;
}
// clearOutput();
Socket clientSocket = connectionController.acceptConnection(devices.get(index));
if (clientSocket != null) {
ReceiverController receiver = new ReceiverController(clientSocket);
HotkeyManager.hook(receiver);
receiver.start();
}
break;
}
break;
}
case 2: {
running = false;
System.out.println("terminating...");
threadedScanner.terminate();
BroadcastHandler.terminate();
ConnectionRequestHandler.terminate();
CloseableInterrupter.closeAll();
break;
}
default: {
System.out.println("Invalid option!");
break;
}
}
} catch (Exception e) {
System.err.println("Unexpected Exception: " + e.toString());
}
}
}
}