package nserver; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.net.SocketException; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; /** * * @author Lolek */ public class Main { /** * @param args the command line arguments */ public static void main(String[] args){ Port port = new Port(); Adresa adresa = new Adresa(); Preposilani prepos = new Preposilani(); PreposilaniData pd = new PreposilaniData(); VlaknoPrijimaci prijem = new VlaknoPrijimaci(5000, port, adresa, prepos,pd); VlaknoPreposilaci odesli = new VlaknoPreposilaci(port, adresa, prepos,prijem.server,pd); } } class VlaknoPrijimaci implements Runnable { private Port p; private Adresa a; private int getPort; private InetAddress getAddress; private Preposilani prepos; private byte odeslana[] = new byte[256]; private byte odeslanaData[] = new byte[256]; private byte prijata[] = new byte[256]; private PreposilaniData pd; DatagramSocket server; int port; VlaknoPrijimaci(int port,Port p,Adresa a,Preposilani prepos, PreposilaniData pd) { try { this.pd = pd; this.prepos = prepos; this.a = a; this.p = p; this.server = new DatagramSocket(port); new Thread(this, "VlaknoPrijimaci").start(); } catch (SocketException ex) { Logger.getLogger(VlaknoPrijimaci.class.getName()).log(Level.SEVERE, null, ex); } } public DatagramSocket getSocket() { return this.server; } @Override public void run () { try { while(true) { DatagramPacket prijatyPaket = new DatagramPacket(prijata,prijata.length); server.receive(prijatyPaket); String prijaty = new String(prijatyPaket.getData(),0,prijatyPaket.getLength()); System.out.println("Od klienta:"+prijaty); if (prijaty.equals("add")) { getPort = prijatyPaket.getPort(); p.setData(getPort); getAddress = prijatyPaket.getAddress(); a.setData(getAddress); } else { odeslana = prijaty.getBytes(); prepos.setData(odeslana); pd.setData(prijatyPaket); } } } catch (IOException ex) { Logger.getLogger(VlaknoPrijimaci.class.getName()).log(Level.SEVERE, null, ex); } } } class VlaknoPreposilaci implements Runnable { private Preposilani prepos; private InetAddress klient; private int port; private DatagramSocket server; private Port p; private Adresa a; private int olo=0; private PreposilaniData pd; private int getPort; private InetAddress getAddress; private Integer port1; private Integer port2; private InetAddress adresa1; private InetAddress adresa2; private boolean bool = true; Thread preposilaci; VlaknoPreposilaci(Port p,Adresa a,Preposilani prepos,DatagramSocket pomA,PreposilaniData pd) { this.pd = pd; this.prepos = prepos; this.p = p; this.a = a; this.server = pomA; new Thread(this, "VlaknoPreposilaci").start(); } @Override public void run() { try { //Thread.sleep(10000); System.out.println(p.getData().get(0)); System.out.println(a.getData().get(0)); System.out.println(p.getData().get(1)); System.out.println(a.getData().get(1)); port1 = (Integer) p.getData().get(0); port2 = (Integer) p.getData().get(1); adresa1 = (InetAddress) a.getData().get(0); adresa2 = (InetAddress) a.getData().get(1); // while(true){ getPort = pd.getData().getPort(); getAddress = pd.getData().getAddress(); if (port1 == getPort) { DatagramPacket odeslat1 = new DatagramPacket(prepos.getData(),prepos.getData().length,adresa1,port1); server.send(odeslat1); System.out.println("port1"); } else if (port2 == getPort) { DatagramPacket odeslat2 = new DatagramPacket(prepos.getData(),prepos.getData().length,adresa2,port2); server.send(odeslat2); System.out.println("port2"); } // } } catch (IOException ex) { Logger.getLogger(VlaknoPreposilaci.class.getName()).log(Level.SEVERE, null, ex); }// catch (InterruptedException ex) { // Logger.getLogger(VlaknoPreposilaci.class.getName()).log(Level.SEVERE, null, ex); // } } } class Port { List<Integer> listPort = Collections.synchronizedList(new ArrayList<Integer>(2)); public synchronized void setData(int portt){ listPort.add(portt); } public synchronized List getData(){ return this.listPort; } } class Adresa { private List<InetAddress> listAddress = Collections.synchronizedList(new ArrayList<InetAddress>(2)); public synchronized void setData(InetAddress adresa){ listAddress.add(adresa); } public synchronized List getData(){ return this.listAddress; } } class Preposilani { private boolean obsazeno=false; byte[] odeslat = new byte[256]; private int i; public synchronized void setData(byte odeslat[]){ this.odeslat=odeslat; } public synchronized byte[] getData(){ return this.odeslat; } } class PreposilaniData { private byte[] odeslat = new byte[256]; private DatagramPacket preposilanyPacket = new DatagramPacket(odeslat,odeslat.length); public synchronized void setData(DatagramPacket kOdeslani){ this.preposilanyPacket=kOdeslani; } public synchronized DatagramPacket getData(){ return this.preposilanyPacket; } } class Shromazdovac { }