Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
Java - Programmazione di reti in Java
Forum - Java - Programmazione di reti in Java

Avatar
ybor (Normal User)
Newbie


Messaggi: 4
Iscritto: 07/11/2007

Segnala al moderatore
Postato alle 17:35
Venerdì, 09/11/2007
Salve,
sono disperato!  :( Il prof ci ha dato un esercizio da fare a casa in una settimana ed io sono proprio nel pallone! se non mi date una mano non ce la faro mai!!!!! Se non ce la faccio non potrò dare l'esame!  :'(

Ecco la Traccia!

Testo quotato

Fornire l'implementazione della classe NeilsenMizuno che estende la classe astratta DAComponent
e che implementa l'omonimo algoritmo per la mutua esclusione distribuita.
La suddetta classe deve:

    * far parte del pacchetto org.ml.pr0708.mat[numerodimatricola].nm
    * interagisce con le altri componenti utilizzando i metodi del campo server. Allo scopo dovranno essere implementati le
opportune estensioni delle classi PacketFactory e Packet,

hiamate NMPacketFactory e NMPacket e appartenenti allo stesso della classe fornita, che descrivono
i pacchetti dell'algoritmo richiesto.
    * fornisce un solo costruttore public che prende come argomenti (nell'ordine):
          o un oggetto di tipo Server da utilizzare per invocare il costruttore della classe base
          o un oggetto di tipo SocketAddress che server ad identificare il padre del nodo al momento. Questo valore sarà null se la
componente che viene costruita possiede il token
    * fornisce i metodi doEnter e doLeave che, rispettivamente, implementano le funzionalità per accedere ed uscire dalla sezione critica
    * fornire il metodo doStart che avvia gli opportuni Thread necessari ad implementare l'algoritmo.

I sorgenti devono essere consegnati all'interno di un file jar contenente che:

    * ha nome <matricola>.jar
    * contiene una cartella <matricola> contenente i soli sorgenti sottomessi dallo studente opportunamente strutturati per rispettare la gerarchia dei pacchetti



Seguendo il corso, con l'aiuto del prof abbiamo preparato queste classi!



Qualsiasi consiglio ed aiuto e bene accetto
Codice sorgente - presumibilmente Java

  1. /**
  2.  *
  3.  */
  4. package org.ml.pr0708.da;
  5.  
  6. import java.io.IOException;
  7. import java.net.ConnectException;
  8. import java.net.InetAddress;
  9. import java.net.ServerSocket;
  10. import java.net.Socket;
  11. import java.net.SocketAddress;
  12. import java.nio.ByteBuffer;
  13. import java.nio.channels.Channel;
  14. import java.nio.channels.SelectionKey;
  15. import java.nio.channels.Selector;
  16. import java.nio.channels.ServerSocketChannel;
  17. import java.nio.channels.SocketChannel;
  18. import java.util.Iterator;
  19. import java.util.LinkedList;
  20.  
  21. import org.ml.pr0708.da.SyncServer.ServerThread;
  22.  
  23. /**
  24.  * @author loreti
  25.  *
  26.  */
  27. public class AsyncServer implements Server {
  28.        
  29.         private static int RETRY=10;
  30.         private LinkedList<Packet> queue;
  31.         private SocketAddress address;
  32.         private PacketFactory factory;
  33.        
  34.         public AsyncServer( PacketFactory factory , SocketAddress address ) {
  35.                 this.address = address;
  36.                 this.queue = new LinkedList<Packet>();
  37.                 this.factory = factory;
  38.         }
  39.        
  40.        
  41.         private synchronized void addPacket( Packet p ) {
  42.                 queue.add(p);
  43.                 notifyAll();
  44.         }
  45.        
  46.         public synchronized void read( Packet p ) throws InterruptedException {
  47.                 while (true) {
  48.                         for (Packet b : queue) {
  49.                                 if (p.match(b)) {
  50.                                         queue.remove(b);
  51.                                         return ;
  52.                                 }
  53.                         }
  54.                         wait();
  55.                 }
  56.         }
  57.  
  58.         public void send( Packet p , SocketAddress dest ) throws IOException {
  59.                 boolean flag = true;
  60.                 SocketChannel c = null;
  61.                 while (flag) {
  62.                         try {
  63.                                 c = SocketChannel.open(dest);
  64.                                 flag = false;
  65.                         } catch (ConnectException e) {
  66.                                
  67.                         }
  68.                 }
  69.                 ByteBuffer b = p.createBuffer();
  70.                 c.write(b);
  71.                 c.close();
  72.         }
  73.        
  74.         public void start() {
  75.                 try {
  76.                         Selector selector = Selector.open();
  77.                         ServerSocketChannel ssc = ServerSocketChannel.open();
  78.                         ssc.configureBlocking(false);
  79.                         ssc.socket().bind(address);
  80.                         ssc.register(selector, SelectionKey.OP_ACCEPT);
  81.                         Thread t = new Thread(new ServerThread(selector));
  82.                         t.start();
  83.                 } catch (IOException e) {
  84.                         e.printStackTrace();
  85.                 }
  86.         }
  87.        
  88.         public class ServerThread implements Runnable {
  89.  
  90.                 private Selector selector;
  91.  
  92.                 public ServerThread(Selector selector) {
  93.                         this.selector = selector;
  94.                 }
  95.  
  96.                 public void run() {
  97.                         try {
  98.                                 while (true) {
  99.                                         selector.select();
  100.                                        
  101.                                         Iterator<SelectionKey> it = selector.selectedKeys().iterator();
  102.                                        
  103.                                         while (it.hasNext()) {
  104.                                                 SelectionKey key = it.next();
  105.                                                 it.remove();
  106.                                                
  107.                                                 handleKey( key );
  108.                                                
  109.                                                
  110.                                         }
  111.  
  112.                                 }                              
  113.                         } catch (IOException e) {
  114.                                 e.printStackTrace();
  115.                         }
  116.                 }
  117.  
  118.                 private void handleKey(SelectionKey key) throws IOException {
  119.                         if (key.isValid()&&key.isAcceptable()) {
  120.                                 ServerSocketChannel ssc = (ServerSocketChannel) key.channel();
  121.                                 SocketChannel client = ssc.accept();
  122.                                 client.configureBlocking(false);
  123.                                 client.register(selector, SelectionKey.OP_READ);
  124.                                 return ;
  125.                         }
  126.                         if (key.isValid()&&key.isReadable()) {
  127.                                 SocketChannel client  = (SocketChannel) key.channel();
  128.                                 ByteBuffer b = factory.getByteBuffer();
  129.                                 client.read(b);
  130.                                 b.flip();
  131.                                 addPacket(factory.createPacket(b));
  132.                                 client.close();
  133.                         }
  134.                        
  135.                 }
  136.                
  137.         }
  138.  
  139. }



Codice sorgente - presumibilmente Java

  1. package org.ml.pr0708.da;
  2.  
  3. public class Controller {
  4.        
  5.         private static Controller c = null;
  6.  
  7.         public static Controller getInstance() {
  8.                 if (c==null) {
  9.                         c = new Controller();
  10.                 }
  11.                 return c;
  12.         }
  13.  
  14.         private boolean free = true;
  15.         private int current = 0;
  16.  
  17.         public synchronized void enter(int id) {
  18.                 if (free) {
  19.                         free = false;
  20.                         current = id;
  21.                 } else {
  22.                         System.err.println("Nodes ( "+current+" ) and ( "+id+" ) are both in critical section!!!!");
  23.                         System.exit(-1);
  24.                 }
  25.         }
  26.        
  27.         public synchronized void exit() {
  28.                 free = true;
  29.                 current = -1;
  30.         }
  31.  
  32. }



Codice sorgente - presumibilmente Java

  1. /**
  2.  *
  3.  */
  4. package org.ml.pr0708.da;
  5.  
  6. import java.nio.ByteBuffer;
  7.  
  8. /**
  9.  * @author loreti
  10.  *
  11.  */
  12. public abstract class Packet {
  13.        
  14.         protected abstract int packetSize();
  15.  
  16.         protected abstract boolean match( Packet p );
  17.        
  18.         public abstract ByteBuffer createBuffer();
  19.        
  20. }



Codice sorgente - presumibilmente Java

  1. /**
  2.  *
  3.  */
  4. package org.ml.pr0708.da;
  5.  
  6. import java.nio.ByteBuffer;
  7.  
  8. /**
  9.  * @author loreti
  10.  *
  11.  */
  12. public interface PacketFactory {
  13.  
  14.         public ByteBuffer getByteBuffer();
  15.         public Packet     createPacket(ByteBuffer b);
  16.  
  17. }



Codice sorgente - presumibilmente Java

  1. package org.ml.pr0708.da;
  2.  
  3. import java.io.IOException;
  4. import java.net.SocketAddress;
  5.  
  6. public interface Server {
  7.  
  8.         public abstract void read(Packet p) throws InterruptedException;
  9.  
  10.         public abstract void send(Packet p, SocketAddress dest) throws IOException;
  11.  
  12.         public abstract void start();
  13.  
  14. }



Codice sorgente - presumibilmente Java

  1. /**
  2.  *
  3.  */
  4. package org.ml.pr0708.da;
  5.  
  6. import java.io.IOException;
  7. import java.net.ConnectException;
  8. import java.net.InetAddress;
  9. import java.net.ServerSocket;
  10. import java.net.Socket;
  11. import java.net.SocketAddress;
  12. import java.nio.ByteBuffer;
  13. import java.nio.channels.ServerSocketChannel;
  14. import java.nio.channels.SocketChannel;
  15. import java.util.LinkedList;
  16.  
  17. /**
  18.  * @author loreti
  19.  *
  20.  */
  21. public class SyncServer implements Server {
  22.        
  23.         private static int RETRY=10;
  24.         private LinkedList<Packet> queue;
  25.         private SocketAddress address;
  26.         private PacketFactory factory;
  27.        
  28.         public SyncServer( PacketFactory factory , SocketAddress address ) {
  29.                 this.address = address;
  30.                 this.queue = new LinkedList<Packet>();
  31.                 this.factory = factory;
  32.         }
  33.        
  34.        
  35.         private synchronized void addPacket( Packet p ) {
  36.                 queue.add(p);
  37.                 notifyAll();
  38.         }
  39.        
  40.         /* (non-Javadoc)
  41.          * @see org.ml.pr0708.da.Server#read(org.ml.pr0708.da.Packet)
  42.          */
  43.         public synchronized void read( Packet p ) throws InterruptedException {
  44.                 while (true) {
  45.                         for (Packet b : queue) {
  46.                                 if (p.match(b)) {
  47.                                         queue.remove(b);
  48.                                         return ;
  49.                                 }
  50.                         }
  51.                         wait();
  52.                 }
  53.         }
  54.  
  55.         /* (non-Javadoc)
  56.          * @see org.ml.pr0708.da.Server#send(org.ml.pr0708.da.Packet, java.net.SocketAddress)
  57.          */
  58.         public void send( Packet p , SocketAddress dest ) throws IOException {
  59.                 boolean flag = true;
  60.                 SocketChannel c = null;
  61.                 while (flag) {
  62.                         try {
  63.                                 c = SocketChannel.open(dest);
  64.                                 flag = false;
  65.                         } catch (ConnectException e) {
  66.                                
  67.                         }
  68.                 }
  69.                 ByteBuffer b = p.createBuffer();
  70.                 c.write(b);
  71.                 c.close();
  72.         }
  73.        
  74.         /* (non-Javadoc)
  75.          * @see org.ml.pr0708.da.Server#start()
  76.          */
  77.         public void start() {
  78.                 Thread t = new Thread(new ServerThread());
  79.                 t.start();
  80.         }
  81.        
  82.         public class ServerThread implements Runnable {
  83.  
  84.                 public void run() {
  85.                         try {
  86.                                 ServerSocketChannel ssc = ServerSocketChannel.open();
  87.                                 ssc.socket().bind(address);
  88.                                 while (true) {
  89.                                         SocketChannel client = ssc.accept();
  90.                                         Thread t = new Thread( new Reader(client) );
  91.                                         t.start();
  92.                                 }                              
  93.                         } catch (IOException e) {
  94.                                 e.printStackTrace();
  95.                         }
  96.                 }
  97.                
  98.         }
  99.  
  100.         public class Reader implements Runnable {
  101.  
  102.                 private SocketChannel channel;
  103.  
  104.                 public Reader(SocketChannel channel) {
  105.                         this.channel = channel;
  106.                 }
  107.  
  108.                 public void run() {
  109.                         ByteBuffer b = factory.getByteBuffer();
  110.                         try {
  111.                                 channel.read(b);
  112.                                 b.flip();
  113.                                 addPacket(factory.createPacket(b));
  114.                                 channel.close();
  115.                         } catch (IOException e) {
  116.                                 e.printStackTrace();
  117.                         }
  118.                        
  119.                 }
  120.                
  121.         }
  122. }


Ultima modifica effettuata da pierotofy il 01/12/2007 alle 22:05
PM Quote
Avatar
netarrow (Admin)
Guru^2


Messaggi: 2502
Iscritto: 12/05/2004

Segnala al moderatore
Postato alle 17:51
Sabato, 10/11/2007
ma non capico, dovè il problema? si più preciso nel dire dove incontri difficoltà

PM Quote
Avatar
ybor (Normal User)
Newbie


Messaggi: 4
Iscritto: 07/11/2007

Segnala al moderatore
Postato alle 11:33
Domenica, 11/11/2007
Il problema e che non so da deve iniziare

PM Quote
Avatar
netarrow (Admin)
Guru^2


Messaggi: 2502
Iscritto: 12/05/2004

Segnala al moderatore
Postato alle 19:01
Domenica, 11/11/2007
eh, indubbiamente non saper iniziare è il più brutto dei problemi.

Cmq.. uhm.. in un forum di solito si ha maggior possibilità di ricevere aiuto quando si ha già iniziato e manca la finalizzazione, io oltre che linkarti questo che ho trovato:

http://www.google.it/url?sa=t&ct=res&cd=10&url=http%3A%2F% ...

non saprei come aiutarti, se non sai come iniziare ma hai capito bene la traccia prova a spiegarmela in altro modo.. ad esempio che algoritmo deve implementare il programma non l'ho capito, da quel che so la mutua esclusione distribuita è una tecnica che si applica ma non capisco a che cosa in quella traccia, NeilsenMizuno è il nome dell'algoritmo? Durante il corso non avete visto esempi tipo questo?

Ultima modifica effettuata da netarrow il 11/11/2007 alle 19:02
PM Quote
Avatar
ybor (Normal User)
Newbie


Messaggi: 4
Iscritto: 07/11/2007

Segnala al moderatore
Postato alle 15:51
Lunedì, 12/11/2007
Si è un algoritmo!

Ora Scrivo un po l'algoritmo e poi posto

PM Quote
Avatar
ybor (Normal User)
Newbie


Messaggi: 4
Iscritto: 07/11/2007

Segnala al moderatore
Postato alle 17:06
Lunedì, 12/11/2007
In questo forum non si possono allegare File?

PM Quote
Avatar
pierotofy (Admin)
Guru^2


Messaggi: 6230
Iscritto: 04/12/2003

Segnala al moderatore
Postato alle 17:54
Lunedì, 12/11/2007
Al momento no, non si possono allegare.


Il mio blog: https://piero.dev
PM Quote