09/11/07 16:35
ybor
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!
Seguendo il corso, con l'aiuto del prof abbiamo preparato queste classi!
Qualsiasi consiglio ed aiuto e bene accetto
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!
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
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
/** * */ package org.ml.pr0708.da; import java.io.IOException; import java.net.ConnectException; import java.net.InetAddress; import java.net.ServerSocket; import java.net.Socket; import java.net.SocketAddress; import java.nio.ByteBuffer; import java.nio.channels.Channel; import java.nio.channels.SelectionKey; import java.nio.channels.Selector; import java.nio.channels.ServerSocketChannel; import java.nio.channels.SocketChannel; import java.util.Iterator; import java.util.LinkedList; import org.ml.pr0708.da.SyncServer.ServerThread; /** * @author loreti * */ public class AsyncServer implements Server { private static int RETRY=10; private LinkedList<Packet> queue; private SocketAddress address; private PacketFactory factory; public AsyncServer( PacketFactory factory , SocketAddress address ) { this.address = address; this.queue = new LinkedList<Packet>(); this.factory = factory; } private synchronized void addPacket( Packet p ) { queue.add(p); notifyAll(); } public synchronized void read( Packet p ) throws InterruptedException { while (true) { for (Packet b : queue) { if (p.match(b)) { queue.remove(b); return ; } } wait(); } } public void send( Packet p , SocketAddress dest ) throws IOException { boolean flag = true; SocketChannel c = null; while (flag) { try { c = SocketChannel.open(dest); flag = false; } catch (ConnectException e) { } } ByteBuffer b = p.createBuffer(); c.write(b); c.close(); } public void start() { try { Selector selector = Selector.open(); ServerSocketChannel ssc = ServerSocketChannel.open(); ssc.configureBlocking(false); ssc.socket().bind(address); ssc.register(selector, SelectionKey.OP_ACCEPT); Thread t = new Thread(new ServerThread(selector)); t.start(); } catch (IOException e) { e.printStackTrace(); } } public class ServerThread implements Runnable { private Selector selector; public ServerThread(Selector selector) { this.selector = selector; } public void run() { try { while (true) { selector.select(); Iterator<SelectionKey> it = selector.selectedKeys().iterator(); while (it.hasNext()) { SelectionKey key = it.next(); it.remove(); handleKey( key ); } } } catch (IOException e) { e.printStackTrace(); } } private void handleKey(SelectionKey key) throws IOException { if (key.isValid()&&key.isAcceptable()) { ServerSocketChannel ssc = (ServerSocketChannel) key.channel(); SocketChannel client = ssc.accept(); client.configureBlocking(false); client.register(selector, SelectionKey.OP_READ); return ; } if (key.isValid()&&key.isReadable()) { SocketChannel client = (SocketChannel) key.channel(); ByteBuffer b = factory.getByteBuffer(); client.read(b); b.flip(); addPacket(factory.createPacket(b)); client.close(); } } } }
package org.ml.pr0708.da; public class Controller { private static Controller c = null; public static Controller getInstance() { if (c==null) { c = new Controller(); } return c; } private boolean free = true; private int current = 0; public synchronized void enter(int id) { if (free) { free = false; current = id; } else { System.err.println("Nodes ( "+current+" ) and ( "+id+" ) are both in critical section!!!!"); System.exit(-1); } } public synchronized void exit() { free = true; current = -1; } }
/** * */ package org.ml.pr0708.da; import java.nio.ByteBuffer; /** * @author loreti * */ public abstract class Packet { protected abstract int packetSize(); protected abstract boolean match( Packet p ); public abstract ByteBuffer createBuffer(); }
/** * */ package org.ml.pr0708.da; import java.nio.ByteBuffer; /** * @author loreti * */ public interface PacketFactory { public ByteBuffer getByteBuffer(); public Packet createPacket(ByteBuffer b); }
package org.ml.pr0708.da; import java.io.IOException; import java.net.SocketAddress; public interface Server { public abstract void read(Packet p) throws InterruptedException; public abstract void send(Packet p, SocketAddress dest) throws IOException; public abstract void start(); }
/** * */ package org.ml.pr0708.da; import java.io.IOException; import java.net.ConnectException; import java.net.InetAddress; import java.net.ServerSocket; import java.net.Socket; import java.net.SocketAddress; import java.nio.ByteBuffer; import java.nio.channels.ServerSocketChannel; import java.nio.channels.SocketChannel; import java.util.LinkedList; /** * @author loreti * */ public class SyncServer implements Server { private static int RETRY=10; private LinkedList<Packet> queue; private SocketAddress address; private PacketFactory factory; public SyncServer( PacketFactory factory , SocketAddress address ) { this.address = address; this.queue = new LinkedList<Packet>(); this.factory = factory; } private synchronized void addPacket( Packet p ) { queue.add(p); notifyAll(); } /* (non-Javadoc) * @see org.ml.pr0708.da.Server#read(org.ml.pr0708.da.Packet) */ public synchronized void read( Packet p ) throws InterruptedException { while (true) { for (Packet b : queue) { if (p.match(b)) { queue.remove(b); return ; } } wait(); } } /* (non-Javadoc) * @see org.ml.pr0708.da.Server#send(org.ml.pr0708.da.Packet, java.net.SocketAddress) */ public void send( Packet p , SocketAddress dest ) throws IOException { boolean flag = true; SocketChannel c = null; while (flag) { try { c = SocketChannel.open(dest); flag = false; } catch (ConnectException e) { } } ByteBuffer b = p.createBuffer(); c.write(b); c.close(); } /* (non-Javadoc) * @see org.ml.pr0708.da.Server#start() */ public void start() { Thread t = new Thread(new ServerThread()); t.start(); } public class ServerThread implements Runnable { public void run() { try { ServerSocketChannel ssc = ServerSocketChannel.open(); ssc.socket().bind(address); while (true) { SocketChannel client = ssc.accept(); Thread t = new Thread( new Reader(client) ); t.start(); } } catch (IOException e) { e.printStackTrace(); } } } public class Reader implements Runnable { private SocketChannel channel; public Reader(SocketChannel channel) { this.channel = channel; } public void run() { ByteBuffer b = factory.getByteBuffer(); try { channel.read(b); b.flip(); addPacket(factory.createPacket(b)); channel.close(); } catch (IOException e) { e.printStackTrace(); } } } }
Ultima modifica effettuata da pierotofy 01/12/07 21:05
aaa