Skip to content

Commit 91e96a0

Browse files
committed
ultime tracce reti
1 parent 124552a commit 91e96a0

File tree

22 files changed

+1692
-2
lines changed

22 files changed

+1692
-2
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package tracce.t20190904.parte1;
2+
3+
import java.util.ArrayList;
4+
import java.util.HashMap;
5+
import java.util.List;
6+
import java.util.Map;
7+
8+
public class ProdottiService {
9+
10+
// id -> (prodotto -> venduti)
11+
Map<String, Map<Prodotto, Integer>> magazziniProdottiVenduti = new HashMap<>();
12+
13+
class Prodotto {
14+
String codice;
15+
String nome;
16+
String produttore;
17+
double prezzo;
18+
}
19+
20+
class ListaProdotti {
21+
List<Prodotto> prodotti = new ArrayList<>();
22+
}
23+
24+
Prodotto prodottoPiuVenduto(String in0) {
25+
int max = -1;
26+
Prodotto pmax = null;
27+
for (String magazzino : magazziniProdottiVenduti.keySet()) {
28+
Map<Prodotto, Integer> prodottiVenduti = magazziniProdottiVenduti.get(magazzino);
29+
for (Prodotto prodotto : prodottiVenduti.keySet()) {
30+
if (
31+
prodotto.produttore.equals(in0) &&
32+
max < prodottiVenduti.get(prodotto)
33+
) {
34+
max = prodottiVenduti.get(prodotto);
35+
pmax = prodotto;
36+
}
37+
}
38+
}
39+
return pmax;
40+
}
41+
42+
ListaProdotti ProdottiMaxIncasso(String in0) {
43+
ListaProdotti ret = new ListaProdotti();
44+
if (!magazziniProdottiVenduti.containsKey(in0)) {
45+
return ret;
46+
}
47+
Map<Prodotto, Integer> prodottiVenduti = magazziniProdottiVenduti.get(in0);
48+
ret.prodotti = prodottiVenduti.keySet().stream()
49+
.sorted((o1, o2) -> {
50+
double i1 = prodottiVenduti.get(o1) * o1.prezzo;
51+
double i2 = prodottiVenduti.get(o2) * o2.prezzo;
52+
return Double.compare(i1, i2);
53+
})
54+
.limit(3)
55+
.toList();
56+
return ret;
57+
}
58+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package tracce.t20190904.parte2;
2+
3+
import utils.Logging;
4+
5+
import java.io.ByteArrayInputStream;
6+
import java.io.IOException;
7+
import java.io.ObjectInputStream;
8+
import java.net.DatagramPacket;
9+
import java.net.DatagramSocket;
10+
import java.util.ArrayList;
11+
import java.util.List;
12+
13+
public class Direzione {
14+
15+
public static void main(String[] args) {
16+
List<Statistica> lista = new ArrayList<Statistica>();
17+
DatagramSocket server = null;
18+
try {
19+
server = new DatagramSocket(Server.STATISTICHE_PORT);
20+
byte[] buffer = new byte[1024];
21+
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
22+
while (true) {
23+
server.receive(packet);
24+
ByteArrayInputStream byteIn = new ByteArrayInputStream(packet.getData());
25+
ObjectInputStream in = new ObjectInputStream(byteIn);
26+
Statistica statistica = (Statistica) in.readObject();
27+
lista.add(statistica);
28+
}
29+
} catch (IOException e) {
30+
Logging.print(Logging.Type.ERROR, "Couldn't init notifications' receiver correctly", "notifica", null, e);
31+
} catch (ClassNotFoundException e) {
32+
Logging.print(Logging.Type.ERROR, "Received unknown object", "notifica", null, e);
33+
} finally {
34+
server.close();
35+
}
36+
}
37+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package tracce.t20190904.parte2;
2+
3+
import utils.Logging;
4+
5+
import java.io.BufferedReader;
6+
import java.io.IOException;
7+
import java.io.InputStreamReader;
8+
import java.io.PrintWriter;
9+
import java.net.Socket;
10+
11+
public class Paziente {
12+
13+
public static void main(String[] args) {
14+
Socket socket = null;
15+
try {
16+
socket = new Socket(Server.HOSTNAME, Server.PRENOTAZIONE_PORT);
17+
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
18+
out.println("cod21497");
19+
20+
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
21+
System.out.println(in.readLine());
22+
} catch (IOException e) {
23+
Logging.print(Logging.Type.ERROR, "Couldn't init communication with server correctly", "utente", null, e);
24+
} finally {
25+
try {
26+
socket.close();
27+
} catch (IOException e) {
28+
Logging.print(Logging.Type.ERROR, "Couldn't end connection with server gracefully", "utente", null, e);
29+
}
30+
}
31+
}
32+
}
Lines changed: 242 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,242 @@
1+
package tracce.t20190904.parte2;
2+
3+
import utils.Logging;
4+
5+
import java.io.*;
6+
import java.net.*;
7+
import java.time.LocalTime;
8+
import java.util.HashMap;
9+
import java.util.List;
10+
import java.util.Map;
11+
import java.util.concurrent.ConcurrentHashMap;
12+
import java.util.concurrent.Semaphore;
13+
import java.util.concurrent.TimeUnit;
14+
import java.util.concurrent.atomic.AtomicInteger;
15+
16+
public class Server {
17+
18+
final static String HOSTNAME = "clinica.unical.it";
19+
final static int PRENOTAZIONE_PORT = 3000;
20+
final static int ANNULLAMENTO_PORT = 4000;
21+
final static int STATISTICHE_PORT = 5000; // udp
22+
final InetAddress externalServer;
23+
24+
// codice esame -> lista matricole
25+
final Map<String, List<String>> esamiMedici;
26+
27+
// medico -> prenotazione
28+
Map<String, List<Prenotazione>> prenotazioni = new ConcurrentHashMap<>();
29+
30+
// codice esame -> prenotazione
31+
Map<String, List<Prenotazione>> setPrenotazioni = new ConcurrentHashMap<>();
32+
33+
// codice esame -> non prenotati
34+
Map<String, Integer> nonPrenotati = new ConcurrentHashMap<>();
35+
36+
Map<String, Semaphore> coda = new ConcurrentHashMap<>();
37+
38+
public Server(Map<String, List<String>> esamiMedici) {
39+
this.esamiMedici = new HashMap<>(esamiMedici);
40+
esamiMedici.keySet().stream().forEach(key -> {
41+
coda.put(key, new Semaphore(0));
42+
});
43+
44+
try {
45+
externalServer = InetAddress.getByName("direzione.unical.it");
46+
} catch (UnknownHostException e) {
47+
throw new RuntimeException(e);
48+
}
49+
50+
richiestaPrenotazione();
51+
annullamentoPrenotazione();
52+
invioStatistiche();
53+
}
54+
55+
class Prenotazione {
56+
static AtomicInteger ID_PROGRESSIVO = new AtomicInteger(0);
57+
58+
int id;
59+
String codiceEsame;
60+
String medico;
61+
62+
public Prenotazione(String codiceEsame, String medico) {
63+
this.codiceEsame = codiceEsame;
64+
this.medico = medico;
65+
this.id = ID_PROGRESSIVO.incrementAndGet();
66+
}
67+
}
68+
69+
void richiestaPrenotazione() {
70+
new Thread(() -> {
71+
ServerSocket server = null;
72+
try {
73+
server = new ServerSocket(PRENOTAZIONE_PORT);
74+
while (true) {
75+
Socket socket = server.accept();
76+
new PrenotazioneHandler(socket).start();
77+
}
78+
} catch (IOException e) {
79+
Logging.print(Logging.Type.ERROR, "Couldn't init server socket on port " + PRENOTAZIONE_PORT, "prenotazioni", this, e);
80+
}
81+
});
82+
}
83+
84+
class PrenotazioneHandler extends Thread {
85+
86+
Socket socket;
87+
LocalTime time = LocalTime.now();
88+
89+
PrenotazioneHandler(Socket socket) {
90+
this.socket = socket;
91+
}
92+
93+
@Override
94+
public void run() {
95+
try {
96+
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
97+
if (
98+
time.isBefore(LocalTime.of(8, 0)) ||
99+
time.isAfter(LocalTime.of(12, 0))
100+
) {
101+
out.println("service not available");
102+
return;
103+
}
104+
105+
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
106+
String codiceEsame = in.readLine();
107+
if (!esamiMedici.containsKey(codiceEsame)) {
108+
out.println("Codice inesistente");
109+
return;
110+
}
111+
112+
for (String medico : esamiMedici.get(codiceEsame)) {
113+
List<Prenotazione> prenotazioniMedico = prenotazioni.get(medico);
114+
long prenotazioniCorrenti = prenotazioniMedico.stream()
115+
.filter(o -> o.codiceEsame.equals(codiceEsame))
116+
.count();
117+
if (prenotazioniCorrenti < 10) {
118+
Prenotazione prenotazione = new Prenotazione(codiceEsame, medico);
119+
prenotazioniMedico.add(prenotazione);
120+
setPrenotazioni.get(codiceEsame).add(prenotazione);
121+
} else {
122+
// inserimento in coda di attesa
123+
boolean haAcquisito = coda.get(codiceEsame).tryAcquire(1, 1, TimeUnit.HOURS);
124+
if (!haAcquisito) {
125+
nonPrenotati.put(codiceEsame, nonPrenotati.get(codiceEsame) + 1);
126+
}
127+
}
128+
}
129+
130+
} catch (IOException e) {
131+
Logging.print(Logging.Type.ERROR, "Couldn't communicate correctly with client " + socket.toString(), "prenotazioni", this, e);
132+
} catch (InterruptedException e) {
133+
Logging.print(Logging.Type.ERROR, "Couldn't suspend thread correctly", "prenotazioni", this, e);
134+
} finally {
135+
try {
136+
socket.close();
137+
} catch (IOException e) {
138+
Logging.print(Logging.Type.ERROR, "Couldn't end connection gracefully " + socket.toString(), "prenotazioni", this, e);
139+
}
140+
}
141+
}
142+
}
143+
144+
void annullamentoPrenotazione() {
145+
new Thread(() -> {
146+
ServerSocket server = null;
147+
try {
148+
server = new ServerSocket(ANNULLAMENTO_PORT);
149+
while (true) {
150+
Socket socket = server.accept();
151+
new AnnullamentoHandler(socket).start();
152+
}
153+
} catch (IOException e) {
154+
Logging.print(Logging.Type.ERROR, "Couldn't init server socket on port " + ANNULLAMENTO_PORT, "annullamento", this, e);
155+
}
156+
});
157+
}
158+
159+
class AnnullamentoHandler extends Thread {
160+
161+
Socket socket;
162+
163+
AnnullamentoHandler(Socket socket) {
164+
this.socket = socket;
165+
}
166+
167+
@Override
168+
public void run() {
169+
try {
170+
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
171+
String richiesta = in.readLine();
172+
173+
String[] parts = richiesta.split("-");
174+
int codice = Integer.parseInt(parts[0]);
175+
String esame = parts[1];
176+
177+
if (esamiMedici.containsKey(esame) && setPrenotazioni.containsKey(esame)) {
178+
Prenotazione prenotazione = setPrenotazioni.get(esame).stream().filter(o -> o.id == codice).toList().getFirst();
179+
180+
List<Prenotazione> prenotazioniMedico = prenotazioni.get(prenotazione.medico);
181+
prenotazioniMedico = prenotazioniMedico.stream().filter(o -> o.id == codice).toList();
182+
prenotazioni.put(prenotazione.medico, prenotazioniMedico);
183+
184+
List<Prenotazione> prenotazioniEsame = setPrenotazioni.get(esame);
185+
prenotazioniEsame = prenotazioniEsame.stream().filter(o -> o.id == codice).toList();
186+
setPrenotazioni.put(esame, prenotazioniEsame);
187+
188+
coda.get(esame).release();
189+
}
190+
191+
} catch (IOException e) {
192+
Logging.print(Logging.Type.ERROR, "Couldn't communicate correctly with client ", "annullamento", this, e);
193+
} finally {
194+
try {
195+
socket.close();
196+
} catch (IOException e) {
197+
Logging.print(Logging.Type.ERROR, "Couldn't end connection gracefully " + socket.toString(), "annullamento", this, e);
198+
}
199+
}
200+
}
201+
}
202+
203+
void invioStatistiche() {
204+
new Thread(() -> {
205+
DatagramSocket external = null;
206+
try {
207+
LocalTime now;
208+
LocalTime endTime = LocalTime.of(12, 0);
209+
while (( now = LocalTime.now() ).isBefore(endTime)) {
210+
long millisToWait = java.time.Duration.between(now, endTime).toMillis();
211+
try {
212+
Thread.sleep(millisToWait);
213+
} catch (InterruptedException e) { }
214+
}
215+
216+
external = new DatagramSocket();
217+
byte[] buf;
218+
DatagramPacket packet;
219+
220+
for (String codiceEsame : setPrenotazioni.keySet()) {
221+
List<Prenotazione> prenotazioniEsame = setPrenotazioni.get(codiceEsame);
222+
Statistica statistica = new Statistica(codiceEsame, prenotazioniEsame.size(), nonPrenotati.get(codiceEsame));
223+
224+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
225+
ObjectOutputStream oos = new ObjectOutputStream(baos);
226+
oos.writeObject(statistica);
227+
oos.flush();
228+
229+
buf = baos.toByteArray();
230+
packet = new DatagramPacket(buf, buf.length, externalServer, STATISTICHE_PORT);
231+
external.send(packet);
232+
}
233+
} catch (IOException e) {
234+
Logging.print(Logging.Type.ERROR, "Couldn't init notification's server correctly", "notifica", this, e);
235+
} finally {
236+
if (external != null) {
237+
external.close();
238+
}
239+
}
240+
}).start();
241+
}
242+
}

0 commit comments

Comments
 (0)