-
Notifications
You must be signed in to change notification settings - Fork 2
/
ARServer.java
176 lines (118 loc) · 3.72 KB
/
ARServer.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
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
package server;
import java.io.*;
import java.net.*;
import java.util.*;
import java.lang.ref.WeakReference;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
//import android.content.Intent;
//import android.util.Log;
public class ARServer {
private static final int KEEP_ALIVE_TIME = 10;
private final int CORE_THREAD_POOL_SIZE = 10;
private final int MAX_THREAD_POOL_SIZE = 10;
private final TimeUnit KEEP_ALIVE_TIME_UNIT;
//Default server port
private static final int DEFAULT_PORT = 4444;
//Maximum port since we are using ServetSocket
private final BlockingQueue<Runnable> mWorkQueue;
private static final int MAXIMUM_PORT = 5555;
//Socket receiving connections
static Socket app_socket;
static ServerSocket serverSocket;
//number of clients connected to the server
static int number_of_clients = 0;
//List<ServerThread> current_thread = new ArrayList<>();
// static {
// try
//
// {
// private static ARServer Instance = new ARServer(DEFAULT_PORT);
//
//
// } catch(IOException e)
//
// {
// e.printStackTrace();
// }
//
//}
private static ARServer Instance;
static
{
try {
// if(number_of_clients%2==0)
Instance = new ARServer(4444);
// else
// Instance = new ARServer(MAXIMUM_PORT);
} catch (IOException e) {
e.printStackTrace();}
}
private final ThreadPoolExecutor mDownloadThreadPool;
public static ARServer getInstance()
{
return Instance;
}
//Socket clientSocket= new ServerSocket;
/**
* Make a ARServer listening on port
* @param port: port on which server is listening (has to be less than 65535)
* @throws IOException is thrown if an errors creating the server socket
*/
public ARServer(int port) throws IOException {
serverSocket= new ServerSocket(port);
KEEP_ALIVE_TIME_UNIT = TimeUnit.SECONDS;
mWorkQueue = new LinkedBlockingQueue<Runnable>();
mDownloadThreadPool = new ThreadPoolExecutor(CORE_THREAD_POOL_SIZE, MAX_THREAD_POOL_SIZE,
KEEP_ALIVE_TIME, KEEP_ALIVE_TIME_UNIT, mWorkQueue);
}
/**
* Run the server, listening for new request
* on success a new thread will be created handling the individual connection
*
* @throws IOException
*/
public void serve( ) throws IOException {
while(true) {
try {
Socket clientSocket = serverSocket.accept();
// this is for niloo test to see if I can save the java client data
// if(number_of_clients==1)
// app_socket= clientSocket;
// Get the InetAddress of the remote client
InetAddress clientAddress = clientSocket.getInetAddress();
String clientIP = clientAddress.getHostAddress();
System.out.println("Client connected from IP address: " + clientIP);
number_of_clients++;
System.out.println("New connection created: " + clientSocket);
System.out.println("Number of clients: " + number_of_clients);
// this is to start auto decimation
Instance.mDownloadThreadPool.execute(new server.ServerThread(clientSocket,Instance));
// Instance.mDownloadThreadPool.execute(new offloading(clientSocket,Instance));
// this is to start offloading
}
catch(IOException e) {
System.out.println("Exception found on accept connection");
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}
public static void main(String[] args) throws IOException, InterruptedException {
ARServer server;
//String [] server_task=new String[]{"AutoDecimate","Offloading"};
try {
server=Instance;
System.out.println( " Running");
//temp commented
server.serve();
} catch (IOException e) {
e.printStackTrace();
}
}
}