package nserver;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Arrays;
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();
Address address = new Address();
Forwarding forwarding = new Forwarding();
ThreadForwarding odesli = new ThreadForwarding (port, address, forwarding);
ThreadReciving prijem = new ThreadReciving(5000, port, address, forwarding);
}
}
class ThreadReciving extends Thread
{
private Port p;
private Address a;
private int getPort;
private InetAddress getAddress;
private Forwarding forwarding;
private byte send[] = new byte[256];
private byte sendData[] = new byte[256];
private byte[] recived = new byte[256];
DatagramSocket server;
int port;
ThreadReciving(int port,Port p,Address a,Forwarding forwarding)
{
try {
this.forwarding = forwarding;
this.a = a;
this.p = p;
this.server = new DatagramSocket(port);
new Thread(this, "ThreadReciving").start();
} catch (SocketException ex) {
Logger.getLogger(ThreadReciving.class.getName()).log(Level.SEVERE, null, ex);
}
}
@Override
public void run ()
{
try
{
while(true)
{
DatagramPacket recivedPacket = new DatagramPacket(prijata,prijata.length);
server.receive(recivedPacket);
String recivedStr = new String(recivedPacket.getData(),0,recivedPacket.getLength());
send = recived.getBytes();
forwarding.setData(odeslana);
if (recived.equals("add"))
{
getPort = recivedPacket.getPort();
p.setData(getPort);
getAddress = recivedPacket.getAddress();
a.setData(getAddress);
}
System.out.println("client:"+recived);
}
}
catch (IOException ex)
{
Logger.getLogger(ThreadReciving.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
class ThreadForwarding extends Thread
{
private Forwarding forwarding;
private InetAddress client;
private int port;
private DatagramSocket server;
private Port p;
private Address a;
VlaknoPreposilaci(Port p,Address a,Forwarding forwarding)
{
try {
this.forwarding = forwarding;
this.p = p;
this.a = a;
this.server = new DatagramSocket();
new Thread(this, "ThreadForwarding").start();
} catch (SocketException ex) {
Logger.getLogger(ThreadForwarding.class.getName()).log(Level.SEVERE, null, ex);
}
}
@Override
public void run()
{ p.getData();
a.getData();
prepos.getData();
// while(true)
// {
try {
DatagramPacket ForwardingPacket = new DatagramPacket(forwarding.send, forwarding.send.length, a.addressArray[0], p.addressArray[0]);
server.send(ForwardingPacket);
} catch (IOException ex) {
Logger.getLogger(ThreadForwarding.class.getName()).log(Level.SEVERE, null, ex);
}
//}
}
}
class Port {
private int portt;
private int i;
private int n = 0;
private boolean bool = true;
private boolean stop = false;
int portArray[] = new int[10];
public synchronized void setData(int portt){
if(stop)
{
try {
wait();
} catch (InterruptedException ex) {
Logger.getLogger(Port.class.getName()).log(Level.SEVERE, null, ex);
}
}
stop=true;
Arrays.fill(portArray, n);
for(int x=0;x<10;x++)
{
if((portArray[x]==n)&&(bool==true))
{
this.portArray[x]=portt;
bool = false;
}
}
System.out.println(this.portArray[0]);
notify();
}
public synchronized int[] getData(){
if(!stop)
{
try {
wait();
} catch (InterruptedException ex) {
Logger.getLogger(Port.class.getName()).log(Level.SEVERE, null, ex);
}
}
stop=false;
notify();
return portArray;
}
}
class Adrress {
private boolean stop=false;
private boolean bool=true;
private int i;
byte[] ip={Network.ip(100),Network.ip(100),Network.ip(100),Network.ip(100)};
InetAddress addressAray[]=new InetAddress[10];
public synchronized void setData(InetAddress address){
try {
InetAddress plnAddress = InetAddress.getByAddress(ip);
if (stop) {
try {
wait();
} catch (InterruptedException ex) {
Logger.getLogger(Address.class.getName()).log(Level.SEVERE, null, ex);
}
}
stop = true;
Arrays.fill(addressAray, plnAdres);
for (i = 0; i < 10; i++) {
if ((addressAray[i] == plnAddress) && (bool)) {
this.addressAray[i] = address;
bool = false;
}
}
System.out.println(this.addressArray[0]);
notify();
} catch (UnknownHostException ex) {
Logger.getLogger(Address.class.getName()).log(Level.SEVERE, null, ex);
}
}
public synchronized InetAddress[] getData(){
if(!stop)
{
try {
wait();
} catch (InterruptedException ex) {
Logger.getLogger(Address.class.getName()).log(Level.SEVERE, null, ex);
}
}
stop=false;
notify();
return addressAray;
}
}
class Forwarding {
private boolean stop=false;
byte[] send = new byte[256];
private int i;
public synchronized void setData(byte send[]){
if(stop)
{
try {
wait();
} catch (InterruptedException ex) {
Logger.getLogger(Forwarding.class.getName()).log(Level.SEVERE, null, ex);
}
}
stop=true;
this.send=send;
notify();
}
public synchronized byte[] getData(){
if(!stop)
{
try {
wait();
} catch (InterruptedException ex) {
Logger.getLogger(Forwarding.class.getName()).log(Level.SEVERE, null, ex);
}
}
stop=false;
notify();
return send;
}
}
class Network {
public static byte ip(int i) {
return (byte) ((i > 127) ? i - 256 : i);
}
}