1 / 42

Задаци

Задаци. N Body Gravitational Problem.

kadeem
Download Presentation

Задаци

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Задаци

  2. N Body Gravitational Problem У свемиру постоји N небеских тела која међусобно интерагују. Користећи програмски језик Јава потребно је симулирати кретање ових тела користећи више рачунара у паралели. При томе треба узети да је један рачунар задужен за више небеских тела. Број небеских тела је много већи од броја расположивих рачунара.

  3. N Body Gravitational Problem

  4. N Body Gravitational Problem

  5. N Body Gravitational Problem

  6. N Body Gravitational Problem

  7. N Body Gravitational Problem

  8. N Body Gravitational Problem

  9. N Body Gravitational Problem

  10. N Body Gravitational Problem

  11. Клијент-сервер public interface Message { public void setData(Object data); public Object getData(); public void setMessageID(long messageID); public long getMessageID(); } public interface Buffer { public static final int MAXBUFFERSIZE = 150; public Object get(); public void put(Object data); public void remove(Object data); public int size(); public int capacity(); }

  12. Клијент-сервер public interface Protocol { public TextMessage conversation(TextMessage msg) throws NextMsgException; public boolean endConvresation(); public boolean startConvresation(); public void conversation(); public void addCommunicator(Communicator c); public boolean removeCommunicator(Communicator c); public Communicator getCommunicator(int id); }

  13. Клијент-сервер public interface Communicator { public boolean init(); public boolean close(); public TextMessage readMessage(); public void writeMessage(TextMessage m); public Object readObject(); public void writeObject(Object m); public String readString(); public void writeString(String s); public void flush(); }

  14. Клијент-сервер import java.util.*; publicclass ArrayBuffer implements Buffer{ privateint capacity; ArrayList arrayBuffer; public ArrayBuffer(){ capacity = MAXBUFFERSIZE; arrayBuffer = new ArrayList(); } public ArrayBuffer(int newCapacity){ if((newCapacity > 0) && (newCapacity <= MAXBUFFERSIZE )) capacity = newCapacity; else capacity = MAXBUFFERSIZE; arrayBuffer = new ArrayList(); }

  15. Клијент-сервер publicsynchronized Object get() { while (arrayBuffer.size() == 0) { try { wait(); } catch (InterruptedException ex ) {ex.printStackTrace();} } Object o = arrayBuffer.remove(0); notifyAll(); return o; } publicsynchronizedvoid put(Object value) { while (arrayBuffer.size() == capacity) { try { wait(); } catch (InterruptedException ex ) {ex.printStackTrace();} } arrayBuffer.add(value); notifyAll(); }

  16. Клијент-сервер publicsynchronizedvoid remove(Object data){ try{ int index = arrayBuffer.indexOf(data); if(index < 0) return; arrayBuffer.remove(index); }catch (Exception ex ) { System.out.println(ex); ex.printStackTrace();} } publicsynchronizedint size(){ return arrayBuffer.size(); } publicsynchronizedint capacity(){ return capacity; } }

  17. Клијент-сервер publicclass TextMessage implements Serializable, Message{ static long ID = 0; long messageID; String text; public TextMessage(String s, long sentID){ messageID = sentID; text = s; } public TextMessage(TextMessage m){ this(m.getText(), m.getMessageID()); } publicvoid setData(Object data) { if(data instanceofString){ text = (String)data; } } ...

  18. Клијент-сервер import java.net.*; import java.io.*; publicclass SocketCommunicator implements Communicator { Socket client; InputStream in; OutputStream out; ObjectOutputStream oout; ObjectInputStream oin; PrintWriter pout; BufferedReader pin; public SocketCommunicator(Socket client) { this.client = client; } public SocketCommunicator() { }

  19. Клијент-сервер publicboolean init() { boolean ok = true; try { out = client.getOutputStream(); oout = new ObjectOutputStream(out); pout = new PrintWriter(out, true); } catch (Exception e) {ok = false;} try { in = client.getInputStream(); oin = new ObjectInputStream(in); pin = new BufferedReader(new InputStreamReader(in)); } catch (Exception e) {ok = false;} return ok; }

  20. Клијент-сервер publicboolean close() { boolean ok = true; try { pin.close(); oin.close(); in.close(); } catch (IOException ex) { ok = false; } try { pout.close(); out.close(); oout.close(); client.close(); } catch (IOException ex) { ok = false;} return ok; }

  21. Клијент-сервер public TextMessage readMessage() { try { TextMessage m = (TextMessage) oin.readObject(); return m; } catch (Exception ex) { System.out.println(ex); ex.printStackTrace(); } returnnull; } publicvoid writeMessage(TextMessage m) { try { oout.writeObject(m); } catch (Exception ex) { System.out.println(ex); ex.printStackTrace(); } }

  22. Клијент-сервер public Object readObject() { try { Object m = (Object) oin.readObject(); return m; } catch (Exception ex) { System.out.println(ex); ex.printStackTrace(); } returnnull; } publicvoid writeObject(Object m) { try { oout.writeObject(m); } catch (Exception ex) { System.out.println(ex); ex.printStackTrace(); } }

  23. Клијент-сервер publicString readString() { try { String s = pin.readLine(); return s; } catch (Exception ex) { System.out.println(ex); ex.printStackTrace(); } returnnull; } publicvoid writeString(String s) { try { pout.println(s); } catch (Exception ex) { System.out.println(ex); ex.printStackTrace(); } }

  24. Клијент-сервер publicvoid flush() { } publicvoid setClient(Socket client) { this.client = client; init(); } }

  25. Клијент-сервер import java.net.*; publicclass Server implements Runnable{ publicstaticfinalint DEFAULTPORT = 88; staticint ID = 0; int port; private Thread thread = null; public Server(){ port = DEFAULTPORT; } public Server(int port){ this.port = port; } publicvoid start() { if (thread == null) { thread = new Thread(this, "Server"); thread.start(); } }

  26. Клијент-сервер publicvoid run() { try { ServerSocket listener = new ServerSocket( port ); Thread myThread = Thread.currentThread(); while (thread == myThread) { try { Socket client = listener.accept(); proceedRequest(client); }catch (Exception ex1 ) {ex1.printStackTrace();} } listener.close(); }catch (Exception ex ) {ex.printStackTrace();} }

  27. Клијент-сервер publicvoid proceedRequest(Socket client){ new TServer(client, ID++).start(); } publicvoid stop() { Thread stopThread = thread; thread = null; stopThread.interrupt(); } public int getPort() { return port; } publicvoid setPort(int port) { this.port = port; } }

  28. Клијент-сервер import java.net.*; publicclass TServer extends Thread { protected long ID; protected Protocol protocol; protected Communicator communicator; protected Socket client; public TServer() { this(null, 0, new ServerProtocol()); } public TServer(Socket client, long ID) { this(client, ID, new ServerProtocol()); } public TServer(Socket client, long ID, Protocol protocol) { super("Thread Server" + ID); this.ID = ID; this.client = client; this.communicator = new SocketCommunicator(client); this.protocol = protocol; }

  29. Клијент-сервер publicvoid run() { work(); } publicvoid work() { try { if (protocol == null) return; Communicator c = this.getCommunicator(); c.init(); protocol.addCommunicator(c); protocol.conversation(); c.close(); } catch (Exception ex) { System.out.println(ex); ex.printStackTrace(); } }

  30. Клијент-сервер public Socket getClient() { System.out.println("getClient " + this.getClass()); return client; } public Communicator getCommunicator() { returnnew SocketCommunicator(getClient()); } }

  31. Клијент-сервер import java.net.*; publicclass TClient extends TServer{ public TClient() { super(null, 0, new ClientProtocol()); } public TClient(Socket client, long ID) { super(client, ID, new ClientProtocol()); } public TClient(Socket client, long ID, Protocol protocol) { super(client, ID, protocol); } }

  32. Клијент-сервер public class OneCommunicatorProtocol implements Protocol{ Communicator c; public OneCommunicatorProtocol(){} public OneCommunicatorProtocol(Communicator c){ this.c = c; } public TextMessage conversation(TextMessage msg) throws NextMsgException{ return null;} public boolean endConvresation(){return true;} public boolean startConvresation(){ return false;} public void conversation(){} public void addCommunicator(Communicator c){ this.c = c; }

  33. Клијент-сервер public boolean removeCommunicator(Communicator c) { if(this.c.equals(c)){ c = null; return true; } return false; } public Communicator getCommunicator(int id){ return c; } }

  34. Клијент-сервер publicclass ClientProtocol extends OneCommunicatorProtocol{ public ClientProtocol(){} public ClientProtocol(Communicator c){ this.c = c; } publicvoid conversation(){ c.writeString("Hello!"); System.out.println("sent Hello!"); String response = c.readString(); System.out.println("received " + response); TextMessage m = new TextMessage("Mesage teks" + response); c.writeObject(m); } }

  35. Клијент-сервер publicclass ServerProtocol extends OneCommunicatorProtocol{ public ServerProtocol(){} public ServerProtocol(Communicator c){ this.c = c; } publicvoid conversation(){ String response = c.readString(); System.out.println("received " + response); c.writeString("Goodbye!"); System.out.println("sent Goodbye! "); TextMessage m = (TextMessage)c.readObject(); System.out.println(m); } }

  36. Клијент-сервер import java.net.*; public class ClientTest1{ static final int port = 88; static final String host = "127.0.0.1"; public static void main(String [] args) { for(int i = 0; i < 10; i ++){ try { Socket server = new Socket(host, port); TClient tc = new TClient( server, i, new ClientProtocol()); tc.start(); tc.join(); server.close(); Thread.sleep(5000+(int)(Math.random()*1000)); } catch(InterruptedException e) {e.printStackTrace();} } } }

  37. Клијент-сервер publicclass ServerTest1{ publicstaticvoid main(String [] args) { Server s = new BufferServer(new ServerProtocol()); s.start(); } }

  38. Клијент-сервер import java.net.*; publicclass OneThreadServer extends Server{ public OneThreadServer(){} publicvoid proceedRequest(Socket client){ try{ TServer tserver = new TServer(client, ID++); tserver.start(); tserver.join(); }catch (Exception ex ) { System.out.println(ex); ex.printStackTrace();} } }

  39. Клијент-сервер import java.net.*; publicclass BufferServer extends Server{ publicstaticfinalint numThreads = 10; Buffer buffer; public BufferServer(int num, Protocol protocol, int port){ super(port); buffer = new ArrayBuffer(); for(int i = 0; i < num; i++){ TServer t = new BufferTServer(buffer, protocol); t.setDaemon(true); t.start(); } } publicvoid proceedRequest(Socket client){ try{ buffer.put(client); }catch (Exception ex ) {ex.printStackTrace();} } }

  40. Клијент-сервер import java.net.*; publicclass BufferTServer extends TServer{ Buffer buffer; public BufferTServer(Buffer buffer, Protocol protocol){ this.buffer = buffer; this.protocol = protocol; } publicvoid run() { while(true){ work(); } } public Socket getClient(){ return (Socket)buffer.get(); } }

  41. N Body Gravitational Problem

  42. Питања? Захарије Радивојевић Електротехнички Факултет Универзитет у Београду zaki@etf.bg.ac.yu

More Related