-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEventManager.java
More file actions
230 lines (202 loc) · 6.8 KB
/
EventManager.java
File metadata and controls
230 lines (202 loc) · 6.8 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.rmi.RemoteException;
import java.rmi.server.RemoteServer;
import java.util.ArrayList;
import java.util.LinkedList;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
* EventManager si occupa della registrazione degli utenti a Worth
* e dei servizi callback
*
*/
public class EventManager extends RemoteServer implements EventManagerInterface {
/**
* file da cui leggo gli utenti già registrati a WORTH
*/
private static final String FILENAME = "lista-utenti-registrati.json";
/**
* directory contenente tutti i file di Worth
*/
private static final String DIRNAME = "Worth_project";
/**
* lista utenti registarti
*/
private LinkedList<Utente> users;
/**
* lista dei clients ai quale viene notificato lo stato aggiornato off/on
* degli utenti registrati a Worth
*/
private ArrayList<NotifyEventInterface> clients;
/**
* lista aggiornata contenente lo stato aggiornato off/on
* degli utenti registrati a Worth
* viene utilizzata solamente durante la registrazione
* di un client al servizio RMI
*/
private LinkedList<UtenteStatus> usersStatus;
/**
* La classe si occupa di creare un ambiente per ospitare i file di Worth,
* se invece esso è già presente recupera la lista dei clienti precedentemente iscritti
* @throws IOException
*/
public EventManager() throws IOException {
this.clients = new ArrayList<NotifyEventInterface>();
this.users = new LinkedList<Utente>();
this.usersStatus = new LinkedList<UtenteStatus>();
//creazione/recupero ambiente
File dir = new File(DIRNAME);
File file = new File(DIRNAME+"/"+FILENAME);
if(!dir.exists()) {
dir.mkdir();
}
else if(!dir.isDirectory()) {
dir.mkdir();
}
if (!file.exists()) {
file.createNewFile();
return;
}
ByteBuffer buffer=ByteBuffer.allocateDirect(4096);
String output = new String();
/*se presente all' interno del file system,
* recupero il contenuto del file e lo memorizzo all'interno di una stringa
*/
try (ReadableByteChannel readChannel= FileChannel.open(Paths.get(DIRNAME+"/"+FILENAME), StandardOpenOption.READ);) {
while(readChannel.read(buffer)!=-1) {
buffer.flip();
while(buffer.hasRemaining()) {
char ch = (char) buffer.get();
output += ch;
}
buffer.compact();
}
readChannel.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ObjectMapper objectMapper = new ObjectMapper();
JsonNode root;
if(!output.isEmpty()) {
//recupero la lista degli utenti registrati sotto forma di JsonNode
try {
root = objectMapper.readTree(output).get("listaUtenti");
if (root.isArray()) {
for (JsonNode objNode : root) {
Utente e = objectMapper.treeToValue(objNode, Utente.class);
this.users.add(e);
UtenteStatus u = new UtenteStatus(e.getName());
this.usersStatus.add(u);
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* @param username nome dell'utente che si vuole registare
* @param password password dell'utente
* @return true se la registrazione avviene, false altrimenti
* @throws IllegalArgumentException
* @throws IOException
*/
public synchronized boolean registerUser(String username, String password) throws IllegalArgumentException, IOException {
System.out.println("Un utente ha richiesto la registrazione all'evento");
boolean notFound = true;
for(Utente user: users) {
if(user.getName().equals(username)) {
notFound = false; //nome utente già registrato
break;
}
}
if(notFound) {
Utente e = new Utente(username, password); //utente registrato correttamente
this.users.add(e);
UtenteStatus u = new UtenteStatus(e.getName());
this.usersStatus.add(u);
this.update(username);
ByteBuffer buffer=ByteBuffer.allocateDirect(1024*1024);
//istanza utilizzata per serializzare una classe in JSON
ObjectMapper objectMapper = new ObjectMapper();
ListaUtenti listaUtenti = new ListaUtenti(this.users);
//scrivo sul file l'elenco dei conti correnti in formato JSON
try(WritableByteChannel outChannel= FileChannel.open(Paths.get(DIRNAME+"/"+FILENAME), StandardOpenOption.WRITE)) {
buffer.clear();
//Serializzazione in JSON
buffer.put(objectMapper.writeValueAsBytes(listaUtenti));
buffer.flip();
while(buffer.hasRemaining()) {
// scrive il contenuto di buffer nel file
outChannel.write(buffer);
}
outChannel.close();
}
}
return notFound;
}
public synchronized LinkedList<UtenteStatus> getListUsers() {
return this.usersStatus;
}
public synchronized boolean loginUser(String name, String password) {
boolean login = false;
for (Utente user: users)
if(user.getName().equals(name) && user.getPassword().equals(password)) {
login = true;
break;
}
return login;
}
public synchronized boolean checkUser(String name) {
boolean found = false;
for (Utente user: users)
if(user.getName().equals(name)) {
found = true;
break;
}
return found;
}
@Override
public synchronized void registerForCallback(NotifyEventInterface ClientInterface, String name) throws RemoteException {
// TODO Auto-generated method stub
if (!clients.contains(ClientInterface)) {
clients.add(ClientInterface);
}
this.update(name, true);
}
@Override
public synchronized void unregisterForCallback(NotifyEventInterface ClientInterface, String name) throws RemoteException {
// TODO Auto-generated method stub
if (clients.remove(ClientInterface)) {
this.update(name, false);
System.out.println("Client disconnesso con successo");
}
else
System.out.println("Impossibile disconnettere il client");
}
public synchronized void update(String name) throws RemoteException {
for(NotifyEventInterface client : this.clients) {
client.notifyEvent(name);
}
}
public synchronized void update(String name, boolean status) throws RemoteException {
for(UtenteStatus u : this.usersStatus) {
if(u.getName().equals(name)) {
u.setStatus(status);
break;
}
}
for(NotifyEventInterface client : this.clients) {
client.notifyEvent(name, status);
}
}
}