Programmazione II: Esami Svolti

Documenti analoghi
Sockets in Java. Lorenzo Gallucci

Programmazione distribuita in Java. Socket & Client-Server

Server Sequenziale. Server Concorrente. Esercitazione: Socket Java con connessione

Corso di Reti di Calcolatori L-A

Architettura Client-Server

Esercizi su Java RMI. Progetto di Cliente / Servitore e supporto. Possibile tabella mantenuta dal server

Java, Oggetti e Strutture Dati di G. Callegarin - Edizioni CEDAM

Esercizio monitor. Sistemi Operativi T 1

Esercitazione 15. Il problema dello Sleeping Barber

Sincronizzazione con Java

Programmazione di rete in Java

MagiCum S.r.l. Progetto Inno-School

public static boolean occorre (int[] a, int n) { int i = 0; boolean trovato = false;

Ottava Esercitazione. introduzione ai thread java mutua esclusione

Tecnologie Web L-A. Java e HTTP. Dario Bottazzi Tel , dario.bottazzi@unibo.it, SkypeID: dariobottazzi. Java e TCP/IP in a Nutshell

Laboratorio di Sistemi Classi e relazioni Java

Sezione: Upcast - downcast

Esercizio Sincronizzazione Thread

Esercitazione: Socket Java senza connessione

7 Esercitazione (svolta): Callback. Polling. Java RMI: callback. Server. Server. Client. Client. due possibilità:

Chat. Si ha un server in ascolto sulla porta Quando un client richiede la connessione, il server risponde con: Connessione accettata.

(VHUFLWD]LRQLGLEDVHVXOOH6RFNHWLQ-DYD 6RFNHWGLWLSRVWUHDP

Linguaggi di programmazione II AA 2010/2011 Esercitazione 2

Corso di Telematica II

Laboratorio di Programmazione Lezione 2. Cristian Del Fabbro

3. un metodo che restituisce l elenco delle matricole di tutti gli studenti del corso;

Fondamenti di Java. Soluzione esercizio hashcode

Cifratura simmetrica

Esempio: Interfacce. Gioco Interfacce

Datagrammi. NOTA: MulticastSocket estende DatagramSocket

Parcheggio.rtf 1/8 6 gennaio Prova di programmazione: parcheggio a pagamento

SAPIENZA Università di Roma Facoltà di Ingegneria dell Informazione, Informatica e Statistica

Esempio: Interfacce. Gioco Interfacce

Esercizi su UDP. Esercitazione di Laboratorio di Programmazione di Rete A. Daniele Sgandurra 22/10/2008. Università di Pisa

Transcript:

Raccolta di Codici per imparare a programmare in Java in modo tale da affrontare e superare con successo l esame di Programmazione II Programmazione II: Esami Svolti A cura di: Emanuele Cennamo

Nel seguente PDF sono riportati un insieme di Compiti Svolti di Programmazione II per esercitarsi su come organizzare una traccia e come risolverla utilizzando Proxy Skeleton, RMI o JMS. 1 A Cura di Emanuele Cennamo

Indice 1. Prova del 01/07/014.... 3. Prova del 17/06/014.... 17 3. Prova del 11/09/013.... 5 4. Prova del 16/06/015.... 37 5. Prova del 9/07/015.... 44 6. Prova del 17/09/015.... 61 7. Prova del 11/09/014.... 69 8. Prova del 0/07/015.... 80 9. Prova Simulazione JMS..... 90 A Cura di Emanuele Cennamo

3 A Cura di Emanuele Cennamo

Organizzazione Traccia: packbroker/broker.java 3 public class Broker { 4 5 public static void main(string[] args){ 6 BrokerImpl broker = new BrokerImpl(); 7 broker.runskeleton(); 8 } 9 10 } 4 A Cura di Emanuele Cennamo

packbroker/brokerimpl.java 3 import java.util.arraylist; 4 import java.util.list; 5 6 import packinterfacce.isportello; 7 8 public class BrokerImpl extends BrokerSkeleton{ 9 10 private List<OggettoSportello> sportelli; 11 1 public BrokerImpl() { 13 this.sportelli = new ArrayList<OggettoSportello>(); 14 } 15 16 @Override 17 public boolean inoltrarichiesta(int id_cliente) { 18 19 if (sportelli.isempty()){ 0 System.out.println("Non ci sono Sportelli connessi alla Rete"); 1 return false; } 3 else{ 4 for (int i = 0; i < sportelli.size(); i++){ 5 ISportello sportello = new BrokerStub(sportelli.get(i).getHost(), sportelli.get(i).getport()); 6 if(sportello.servirichiesta(id_cliente) == true){ 7 return true; 8 } 9 } 30 } 31 3 return false; 33 } 34 35 @Override 36 public void sottoscrivi(string host, int port) { 37 38 OggettoSportello sportello = new OggettoSportello(host, port); 39 int n = sportelli.size(); 40 sportelli.add(sportello); 41 if (n+1 == sportelli.size()){ 4 System.out.println("Uno Sportello ha effettuato una sottoscrizione"); 43 System.out.println("Totale Sportelli sottoscritti: " + sportelli.size()); 44 } 45 else 46 System.out.println("Uno Sportello ha fallito la sottoscrizione"); 47 48 } 49 50 51 } 5 A Cura di Emanuele Cennamo

packbroker/brokerskeleton.java 3 import java.io.ioexception; 4 import java.net.serversocket; 5 import java.net.socket; 6 7 import packinterfacce.ibroker; 8 9 public abstract class BrokerSkeleton implements IBroker { 10 11 public void runskeleton(){ 1 13 ServerSocket serversock; 14 try { 15 serversock = new ServerSocket(500); 16 System.out.println("Server In Ascolto sulla porta 500"); 17 while (true){ 18 Socket sock = serversock.accept(); 19 Thread t1 = new BrokerSkeletonThread(sock, this); 0 t1.start(); 1 } } catch (IOException e) { 3 e.printstacktrace(); 4 } 5 6 } 7 8 9 } packbroker/brokerskeletonthread.java 3 import java.io.bufferedinputstream; 4 import java.io.bufferedoutputstream; 5 import java.io.datainputstream; 6 import java.io.dataoutputstream; 7 import java.io.ioexception; 8 import java.net.socket; 9 10 import packinterfacce.ibroker; 11 1 public class BrokerSkeletonThread extends Thread { 13 14 private Socket sock; 15 private IBroker broker; 16 17 public BrokerSkeletonThread(Socket sock, IBroker broker) { 18 super(); 19 this.sock = sock; 0 this.broker = broker; 1 } 6 A Cura di Emanuele Cennamo

3 @Override 4 public void run() { 5 boolean esito = false; 6 try { 7 8 DataOutputStream dos = new DataOutputStream (new BufferedOutputStream(sock.getOutputStream())); 9 DataInputStream dis = new DataInputStream ( new BufferedInputStream(sock.getInputStream())); 30 31 String command = dis.readutf(); 3 if(command.equals("sottoscrivi")){ 33 String host = dis.readutf(); 34 int port = dis.readint(); 35 broker.sottoscrivi(host, port); 36 dos.writeutf("sottoscrizione Effettuata al broker"); 37 } 38 else if (command.equalsignorecase("inoltrarichiesta")){ 39 int id_cliente = dis.readint(); 40 esito = broker.inoltrarichiesta(id_cliente); 41 dos.writeboolean(esito); 4 } 43 44 dos.flush(); 45 dos.close(); 46 dis.close(); 47 48 } catch (IOException e) { 49 e.printstacktrace(); 50 } 51 5 } 53 54 } packbroker/brokerstub.java 3 import java.io.bufferedinputstream; 4 import java.io.bufferedoutputstream; 5 import java.io.datainputstream; 6 import java.io.dataoutputstream; 7 import java.io.ioexception; 8 import java.net.socket; 9 import java.net.unknownhostexception; 10 11 import packinterfacce.isportello; 1 13 public class BrokerStub implements ISportello { 14 15 String host; 16 int port; 17 18 public BrokerStub(String host, int port) { 19 super(); 0 this.host = host; 7 A Cura di Emanuele Cennamo

1 this.port = port; } 3 @Override 4 public boolean servirichiesta(int id_cliente) { 5 6 boolean esito = false; 7 8 try { 9 Socket sock = new Socket(host, port); 30 DataOutputStream dos = new DataOutputStream ( new BufferedOutputStream(sock.getOutputStream())); 31 DataInputStream dis = new DataInputStream ( new BufferedInputStream(sock.getInputStream())); 3 33 dos.writeint(id_cliente); 34 dos.flush(); 35 36 esito = dis.readboolean(); 37 38 dos.close(); 39 dis.close(); 40 sock.close(); 41 4 } catch (UnknownHostException e) { 43 e.printstacktrace(); 44 } catch (IOException e) { 45 e.printstacktrace(); 46 } 47 48 return esito; 49 } 50 51 5 53 } packbroker/oggettosportello.java 3 public class OggettoSportello { 4 5 private String host; 6 private int port; 7 8 public OggettoSportello(String host, int port) { 9 super(); 10 this.host = host; 11 this.port = port; 1 } 13 14 public String gethost() { 15 return host; 16 } 17 18 public int getport() { 19 return port; 0 } 1 8 A Cura di Emanuele Cennamo

} packclient/client.java 1 package packclient; 3 import java.util.random; 4 import java.util.concurrent.executorservice; 5 import java.util.concurrent.executors; 6 7 import packinterfacce.ibroker; 8 9 public class Client { 10 11 private static final int DEFAULT_PORT = 500; 1 private static final String DEFAULT_HOST = "localhost"; 13 14 public static void main(string[] args) { 15 16 if ( args.length >= 3 ){ 17 System.out.println("ERRORE: aggiungere come argomento solo <broker host> e <broker port> "); 18 System.exit(0); 19 } 0 1 IBroker broker = null; int port = 0; 3 4 if(args.length == 0){ 5 System.out.print("Mi sto connettendo al broker '" + DEFAULT_HOST + "'alla porta " + DEFAULT_PORT ); 6 broker = new ClientStub(DEFAULT_HOST, DEFAULT_PORT); 7 } 8 else if ( args.length == 1 ){ 9 System.out.print("Mi sto connettendo al broker '" + args[0] + "' alla porta " + DEFAULT_PORT ); 30 broker = new ClientStub(args[0], DEFAULT_PORT); 31 } 3 else if ( args.length == ){ 33 port = Integer.parseInt(args[1]); 34 System.out.print("Mi sto connettendo al broker '" + args[0] + "' alla porta " + port ); 35 broker = new ClientStub(args[0], port); 36 } 37 38 Random rand = new Random(); 39 int id_cliente; 40 ExecutorService executor = Executors.newCachedThreadPool(); 41 4 for ( int i = 0; i < 10; i++ ){ 43 id_cliente = rand.nextint(100)+1; 44 executor.execute(new ClientThread(id_cliente, i, broker)); 45 } 46 47 } 48 49 } 9 A Cura di Emanuele Cennamo