19-02-2013, 03:29 PM
import java.net.*;
import java.io.*;
import java.lang.*;
import java.util.*;
public class Sink
{
public static ServerSocket ss=null;
public static Socket cs[]=new Socket[3];
public static Socket mobilesoc=null;
public static int i,index;
public static int hop[]={3,2,4};
public static int table[][]={ {1,3},{2,2},{3,4} };
public static boolean flagS[]={true,true,true};
public static boolean flagcomplete[]={true,true,true};
public static Sink sk;
public static WithServer ws;
public static String ack;
public static PrintWriter pw;
public static BufferedReader br;
public static InputStream in[]=new InputStream[3];
public static OutputStream out[]=new OutputStream[3];
public static byte dumy[][]=new byte[2][8];
public static byte key[][]=new byte[2][8];
public static void main(String arg[])
{
sk=new Sink();
try
{
ss=new ServerSocket(3000);
System.out.println("SINK IS LISTENING...\n\n");
new GetConnectionFromSk(ss);
System.out.println("\nSINK CONNECTION COMPLETED:");
Thread.sleep(8000);
System.out.println("\nTOWERS FLAG STATUS:\n*\n");
int i=0;
while(i<3)
{
new GetTowerS(i).start();
i++;
}
Thread.sleep(5000);
ws=new WithServer();
}
catch(Exception e)
{
//System.out.println("error at :"+e);
}
}
public void sendMessage(byte packet[][])
{
try
{
int j=0;
boolean complete=false;
String mss="dumydumy";
ByteArrayOutputStream f=new ByteArrayOutputStream();
byte buf[]=mss.getBytes();
f.write(buf);
dumy[0]=f.toByteArray();
while(j<3)
{
index=9;
index=sk.checkHopIndex();
if(index==9)
{
System.out.println("ALL NEIGBOUR TOWERS ARE FAULT");
//ws.replyToSender("NACK");
System.exit(1);
}
out[index].write(packet[0]);
System.out.println("PACKET IS SENDED TO TOWER:"+table[index][0]);
int i=0;
while(i<3)
{
if( i!=index && flagS[i] )
{
out[i].write(dumy[0]);
//System.out.println("dumy sended"+i);
}
i++;
}
ack=sk.getAck(index);
System.out.println("\nGETING ACKNOWLEDGE: "+ack+"\n\n");
if(ack.equalsIgnoreCase("ACK"))
{
complete=true;
//ws.replyToSender("ACK");
break;
}
else
{
if(index==1)
{
System.out.println(" Forwarding TOWERS ARE GET FAULT");
//ws.replyToSender("NACK");
System.exit(1);
}
flagcomplete[index]=false;
j++;
}
}//while(j)
if(!complete)
{
System.out.println("ALL NEIGHBOUR TOWERS ARE GET FAULT");
//ws.replyToSender("NACK");
System.exit(1);
}
}
catch(Exception e)
{}
} //send message
public String getAck(int index)
{
String ack="";
try
{
br=new BufferedReader(new InputStreamReader(cs[index].getInputStream()));
ack=br.readLine();
}
catch(Exception e)
{}
return(ack);
}
public int checkHopIndex()
{
int hop2=0,i=0;
while(i<3)
{
if(flagS[i] && flagcomplete[i])
{
hop2=table[i][1];
index=i;
break;
}
else
i++;
}
for( i=0;i<3;i++)
{
if(flagS[i] && flagcomplete[i])
{
if(hop2>=table[i][1])
{
hop2=table[i][1];
index=i;
}
}
}
return(index);
}
}
class GetConnectionFromSk
{
ServerSocket soc=null;
GetConnectionFromSk(ServerSocket ss)
{
try
{
soc=ss;
int i=0;
while(i<3)
{
Sink.cs[i]=soc.accept();
Sink.in[i]=Sink.cs[i].getInputStream();
Sink.out[i]=Sink.cs[i].getOutputStream();
System.out.println("Sink is connected with Tower:"+(i+1));
i++;
}
}
catch(Exception ex)
{
//System.out.println("error in getconnectionfrom :"+ex);
}
}
}
class GetTowerS extends Thread
{
BufferedReader br;
String inform;
int i;
GetTowerS(int i)
{
this.i=i;
}
public void run()
{
try
{
br=new BufferedReader(new InputStreamReader(Sink.cs[i].getInputStream()));
inform=br.readLine();
if(inform.equalsIgnoreCase("Y"))
Sink.flagS[i]=false;
System.out.println("FLAG OF TOWER:"+(i+1)+" is :"+Sink.flagS[i]);
}
catch(Exception er)
{
//System.out.println("error at gettowers :"+er);
}
}
}
class WithServer //extends Thread
{
private ServerSocket s;
private Socket soc;
private BufferedReader in;
private PrintWriter out;
Sink sk=new Sink();
private int count=0;
private static int packetcount=0;
public static ByteArrayOutputStream baos=new ByteArrayOutputStream();
WithServer()
{
try
{
s=new ServerSocket(3535);
soc=s.accept();
System.out.println("\nWaiting for mobile Connection...");
System.out.println("\nSINK IS CONNECTED TO SENDER CELL phone\n");
in = new BufferedReader(new InputStreamReader(soc.getInputStream()));
int messcount=1;
while(true)
{
out = new PrintWriter(soc.getOutputStream(),true);
String cont=in.readLine();
if(count<3)
{
if(cont.equalsIgnoreCase(""))
{
count++;
}
else
{
System.out.println("MESSAGE "+(messcount++) +" :"+cont);
int length=cont.length();
System.out.println("MESSAGE LENGTH :"+length);
byte packet[][]=new byte[2][100];
int len=(length/8)+1;
byte buf[]=cont.getBytes();
baos.write(buf);
packet[0]=baos.toByteArray();
sk.sendMessage(packet);
int l=0;
while(l<len)
{
System.out.println("PACKETS NO:"+(l+1));
packetcount++;
l++;
}
System.out.println("\n\n");
baos.reset();
count=0;
}
}
}
}
catch(Exception q){}
System.out.println("TOTAL PACKETS TRANSMITTED: "+packetcount);
}
public void replyToSender(String ack)throws Exception
{
out=new PrintWriter(soc.getOutputStream(),true);
out.println(ack);
System.out.println("reply");
}
}