|
| 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