-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTuring.java
More file actions
128 lines (98 loc) · 3.79 KB
/
Copy pathTuring.java
File metadata and controls
128 lines (98 loc) · 3.79 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
package turing;
import java.io.File;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import java.util.Iterator;
import java.util.Set;
import java.util.Timer;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.atomic.AtomicInteger;
public class Turing {
final static int RMI_PORT = 3000;
final static int LOGIN_PORT = 2010;
final static int REQUEST_PORT = 2011;
final static String DATA_DIR = "./ServerDocuments/";
//coda condivisa con il dispatcher
static LinkedBlockingQueue<SocketChannel> sockets = new LinkedBlockingQueue<SocketChannel>();
static UserDB databaseUsers = new UserDB();
static DocumentDB databaseDocuments = new DocumentDB();
static AtomicInteger n_porta = new AtomicInteger(2012); //numero porta di una chat di gruppo
public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
//stub per la registrazione di un nuovo utente
try {
UserRMI stub = (UserRMI) UnicastRemoteObject.exportObject(databaseUsers, 0);
LocateRegistry.createRegistry(RMI_PORT);
Registry r = LocateRegistry.getRegistry(RMI_PORT);
r.rebind("USER-RMI", (Remote) stub);
}
catch (RemoteException e) {
System.out.println("Communication-RMI error");
}
ServerSocketChannel loginChannel;
ServerSocketChannel requestChannel;
Selector selector;
File dir = new File(DATA_DIR);
if (!dir.exists()) dir.mkdirs();
try {
loginChannel = ServerSocketChannel.open();
requestChannel = ServerSocketChannel.open();
ServerSocket loginSocket = loginChannel.socket();
InetSocketAddress loginAddress = new InetSocketAddress(LOGIN_PORT);
loginSocket.bind(loginAddress);
ServerSocket requestSocket = requestChannel.socket();
InetSocketAddress requestAddress = new InetSocketAddress(REQUEST_PORT);
requestSocket.bind(requestAddress);
System.out.println("Turing-Server: \nListening for connections...\n ");
loginChannel.configureBlocking(false);
requestChannel.configureBlocking(false);
selector = Selector.open();
loginChannel.register(selector, SelectionKey.OP_ACCEPT);
requestChannel.register(selector, SelectionKey.OP_ACCEPT);
}
catch (IOException ex) {
ex.printStackTrace();
return;
}
Thread dispatcher = new Thread(new Dispatcher());
dispatcher.start(); //avvio il thread dispatcher
Timer timer = new Timer();
//thread che controlla status dei client
timer.scheduleAtFixedRate(new CheckClientStatus(), 3000,30000);
try {
while (true) {
selector.select();
Set <SelectionKey> readyKeys = selector.selectedKeys();
Iterator <SelectionKey> iterator = readyKeys.iterator();
while (iterator.hasNext()) {
SelectionKey key = iterator.next();
iterator.remove(); //rimuove la chiave dal Selected Set, ma non dal registered Set
if (key.isAcceptable()) {
ServerSocketChannel server = (ServerSocketChannel)key.channel();
SocketChannel client = server.accept();
client.configureBlocking(false);
client.register(selector, SelectionKey.OP_READ);
}
else if(key.isReadable()) {
SocketChannel client = (SocketChannel)key.channel();
sockets.put(client); //inserisco nella coda per far prelevare al dispatcher
key.cancel();
}
}
}
}
catch (IOException ex) {
ex.printStackTrace();
}
dispatcher.join();
}
}