/*Programmer: Ravi Danda & Satish Jonnalgedda
Team : CompanyBgroupA
Module : Msc
Lang : Java
Version : jdk1.2
*/
import java.io.*;
import java.net.*;
import java.util.*;
public class MscServer
{ public static void main(String[] args )
{ int i = 1;
try
{
/* Registers 4 server sockets which are 8888/9999 for data, the server listens on
8888 and writes on 9999, similarly 7777/6666 are for control messages
like connection requests */
ServerSocket s = new ServerSocket(8888);
ServerSocket s1 =new ServerSocket(9999);
ServerSocket s2 = new ServerSocket(7777);
ServerSocket s3 = new ServerSocket(6666);
for (;;)
{
/* Everything in this for loop takes care of accepting connections from BaseStations and generating socket identifiers for them */
Socket incoming = s.accept( );
System.out.println("Registering BaseStation " + incoming +incoming.getInetAddress());
Socket outgoing = s1.accept();
System.out.println("Registering BaseStation " + outgoing +outgoing.getInetAddress());
Socket control1 = s2.accept();
Socket controlr1 = s3.accept();
Socket incoming2 = s.accept( );
System.out.println("Registering BaseStation " + incoming2 +incoming2.getInetAddress());
Socket outgoing2 = s1.accept();
System.out.println("Registering BaseStation " + outgoing2+outgoing2.getInetAddress());
Socket control2 = s2.accept();
Socket controlr2 = s3.accept();
Socket incoming3 = s.accept( );
System.out.println("Registering BaseStation " + incoming3 +incoming3.getInetAddress());
Socket outgoing3 = s1.accept();
System.out.println("Registering BaseStation " + outgoing3 +outgoing3.getInetAddress());
Socket control3 = s2.accept();
Socket controlr3 = s3.accept();
/* Once all the BaseStations are connected three threads are created one to handle each Base
Station requests. The current program works for 3 BaseStations and can be extended to any number by
modifying this code */
new ThreadedWirelessHandler(incoming,outgoing,incoming2,outgoing2,incoming3,outgoing3,control1,control2,control3,controlr1,controlr2,controlr3,i).start();
i++;
new ThreadedWirelessHandler(incoming2,outgoing2,incoming,outgoing,incoming3,outgoing3,control1,control2,control3,controlr1,controlr2,controlr3,i).start();
i++;
new ThreadedWirelessHandler(incoming3,outgoing3,incoming2,outgoing2,incoming,outgoing,control1,control2,control3,controlr1,controlr2,controlr3,i).start();
i++;
}
}
catch (Exception e)
{ System.out.println(e);
}
}
}
class ThreadedWirelessHandler extends Thread
{
public ThreadedWirelessHandler(Socket i,Socket o,Socket i2, Socket o2, Socket i3, Socket o3,Socket control1,Socket control2, Socket control3,Socket controlr1,Socket controlr2,Socket controlr3, int c)
{ incoming = i;outgoing =o; incoming2 = i2; outgoing2 = o2;incoming3 =i3; outgoing3 =o3;ctr1 =control1;ctr2 =control2;ctr3 =control3;rctr1 =controlr1; rctr2 =controlr2;rctr3=controlr3;counter = c; }
public void run()
{ try
{
BufferedReader in = new BufferedReader
(new InputStreamReader(incoming.getInputStream()));
PrintWriter out = new PrintWriter
(outgoing.getOutputStream(), true );
BufferedReader ctrl1in;
PrintWriter ctrlout,ctrlout2,ctrlout3;
PrintWriter destctrlout;
Hashtable numbers = new Hashtable();
System.out.println(" Created Thread to deal " + incoming + outgoing);
// out.println( " Received Your Request please wait while I
//register you" + incoming.getInetAddress() + "...." + counter );
int conn =1;
int approved =1, different=1,destination=1;
int DestBase =0;
String tonum = " " ;
/* In each of these threads action is taken basing on the BaseStation which is a thread and which is tracked using a thread
number, If the request is from a BaseStation 1, then the counter value is equal to 1
and this particular if loop is executed and similarly basing on the counter value the relevant
if loop is executeed */
if(counter ==1) {
boolean done = false;
ctrl1in = new BufferedReader
(new InputStreamReader(ctr1.getInputStream()));
ctrlout = new PrintWriter
(rctr1.getOutputStream(), true );
ctrlout2 = new PrintWriter
(rctr2.getOutputStream(), true );
ctrlout3 = new PrintWriter
(rctr3.getOutputStream(), true );
while (!done)
{
System.out.println(" I am here in while again");
String Initial = ctrl1in.readLine();
if (Initial == null) done = true;
else
{
System.out.println(Initial);
StringTokenizer tok = new StringTokenizer(Initial);
String BaseId = tok.nextToken(" ");
String fromnum =tok.nextToken(" ");
String Mesgtype = tok.nextToken(" ");
if(Mesgtype.equals("10"))
tonum = tok.nextToken(" ");
if (Mesgtype.equals("0"))
{
System.out.println("Message 0 received in BaseStation handler " + counter);
}
// if it is a call request
/* This is the first message received from Basestation which is a call request */
if(Mesgtype .equals("10") )
{
numbers.put(fromnum, new String(tonum));
numbers.put(tonum, new String(fromnum));
System.out.print("You are " + BaseId + " at" + incoming.getInetAddress()+ " you want me to connect " + fromnum);
System.out.println(" and " + tonum);
/* The basestaion Id, from number and tonumber are parsed to see if it is a
valid number and the Msc has to inform if it is a same BaseStation case or a
different BaseStation case */
StringTokenizer ToAreaCode = new StringTokenizer(tonum);
StringTokenizer FromAreaCode = new StringTokenizer(fromnum);
String ToCode = ToAreaCode.nextToken("-");
String num1 = ToAreaCode.nextToken(" ");
String FromCode =FromAreaCode.nextToken("-");
System.out.println( ToCode + FromCode);
String num2 = FromAreaCode.nextToken(" ");
numbers.put(num2, new Integer(counter));
/* If both the area codes are equal then it is same Base Station case */
if (ToCode.equals(FromCode))
{
approved = 1;
// Le should give a method for approval
if(approved ==1)
ctrlout.println("Approved Same " + fromnum + " " + tonum);
else
ctrlout.println("Denied Same" + fromnum + " " + tonum);
}
/* This is executed if the area codes are different which means a different BaseStation
Case */
else
{
different =1;
approved =1;
if (ToCode.equals("214"))
DestBase = 2;
else if (ToCode.equals("972"))
DestBase =3;
else if(ToCode.equals("817"))
DestBase =1;
numbers.put(num1, new Integer(DestBase));
/* If the number is approved then the Msc needs to send a paging message to the destination
BaseStation which is Mesage number 5. The Msc receives a zero message and it is sent
to the source Basestation once the calle presses talk then the message 16 is generated
by the destination and Msc passes it on to source */
*/
if(approved ==1)
{
ctrlout.println("Approved Different " + fromnum +" " + tonum);
destctrlout = new PrintWriter
(rctr2.getOutputStream(), true );
destctrlout.println( tonum + " 5");
//Thread.sleep(10000);
ctrlout.println( numbers.get(tonum) + " 0");
// Thread.sleep(20000);
ctrlout.println( numbers.get(tonum) + " 16");
//Socket destin = 9000;
// Write a function which returns a socket number basing on the
// phone number given
}
else
ctrlout.println("Denied Different " + fromnum + " " + tonum);
}
// threads to handle conversation
}
// if message type was 14 it ends here
if ( (approved ==1) && (different ==1) )
{
/* Once all the message types are communicated it is time to create
the threads that will handle the conversation between the two baseStations apparently the
the two Handsets */
System.out.println("Recived message 0");
if(DestBase ==2)
{
System.out.println("Destination Detecyed as" + DestBase);
//destctrlout = new PrintWriter
// (rctr2.getOutputStream(), true );
//destctrlout.println(numbers.get(fromnum) + " 16");
new BaseStationHandler(incoming,outgoing2,0,outgoing,outgoing2,outgoing3,fromnum,(String)numbers.get(fromnum),numbers ).start();
new BaseStationHandler(incoming2,outgoing,0,outgoing,outgoing2,outgoing3,(String)numbers.get(fromnum) ,fromnum,numbers ).start();
}
else if(DestBase ==3)
{
System.out.println("Destination Detecyed as" + DestBase);
// destctrlout = new PrintWriter
// (rctr3.getOutputStream(), true );
// destctrlout.println(numbers.get(fromnum) + " 16");
new BaseStationHandler(incoming,outgoing3,0,outgoing,outgoing2,outgoing3,fromnum,(String)numbers.get(fromnum),numbers ).start();
new BaseStationHandler(incoming3,outgoing,0,outgoing,outgoing2,outgoing3,(String)numbers.get(fromnum) ,fromnum,numbers ).start();
}
}
if (Initial.trim().equals("."))
done = true;
}
//else part closing
}
//while loop closes
}
// if contr1 closes
// Listening to the control channel 1
else if(counter ==2)
{
boolean done2 = false;
ctrl1in = new BufferedReader
(new InputStreamReader(ctr2.getInputStream()));
ctrlout = new PrintWriter
(rctr2.getOutputStream(), true );
ctrlout2 = new PrintWriter
(rctr1.getOutputStream(), true );
ctrlout3 = new PrintWriter
(rctr3.getOutputStream(), true );
while (!done2)
{
System.out.println(" I am here in while again");
String Initial2 = ctrl1in.readLine();
if (Initial2 == null) done2 = true;
else
{
System.out.println(Initial2);
StringTokenizer tok = new StringTokenizer(Initial2);
String BaseId = tok.nextToken(" ");
String fromnum =tok.nextToken(" ");
String Mesgtype = tok.nextToken(" ");
if(Mesgtype.equals("10"))
tonum = tok.nextToken(" ");
if (Mesgtype.equals("0"))
{
System.out.println("MessageType0 Received");
//ctrlout.println( numbers.get(fromnum) + Mesgtype);
}
// if it is a call request
if(Mesgtype .equals("10") )
{
numbers.put(fromnum, new String(tonum));
numbers.put(tonum, new String(fromnum));
System.out.print("You are " + BaseId + " at" + incoming.getInetAddress()+ " you want me to connect " + fromnum);
System.out.println(" and " + tonum);
StringTokenizer ToAreaCode = new StringTokenizer(tonum);
StringTokenizer FromAreaCode = new StringTokenizer(fromnum);
String ToCode = ToAreaCode.nextToken("-");
String num1 = ToAreaCode.nextToken(" ");
String FromCode =FromAreaCode.nextToken("-");
String num2 = FromAreaCode.nextToken("");
numbers.put(num2, new Integer(counter));
if (ToCode.equals(FromCode))
{
approved = 1;
// Le should give a method for approval
if(approved ==1)
ctrlout.println("Approved Same " + fromnum + " " + tonum);
else
ctrlout.println("Denied Same " + fromnum + " " + tonum);
}
else
{
different =1;
approved =1;
if (ToCode.equals("214"))
DestBase = 2;
else if (ToCode.equals("972"))
DestBase =3;
else if(ToCode.equals("817"))
DestBase =1;
numbers.put(num1, new Integer(DestBase));
if(approved ==1)
{
ctrlout.println("Approved Different " + fromnum + " " + tonum);
//Socket destin = 9000;
destctrlout = new PrintWriter
(rctr1.getOutputStream(), true );
destctrlout.println( tonum + " 5");
try{
Thread.sleep(8000);
}
catch(InterruptedException e)
{
System.out.println("Our own exception");
}
ctrlout.println( numbers.get(tonum) + " 0");
//Thread.sleep(20000);
ctrlout.println( numbers.get(tonum) + " 16");
// Write a function which returns a socket number basing on the
// phone number given
}
else
ctrlout.println("Denied Different " + fromnum + " " + tonum);
}
}
if ( (approved ==1) && (different ==1) )
{
System.out.println("Recived message 0");
if(DestBase ==1)
{
System.out.println("Destination Detecyed as" + DestBase);
//destctrlout = new PrintWriter
// (rctr1.getOutputStream(), true );
// destctrlout.println(numbers.get(fromnum) + " 16");
new BaseStationHandler(incoming,outgoing2,0,outgoing2,outgoing,outgoing3,fromnum,(String)numbers.get(fromnum),numbers ).start();
new BaseStationHandler(incoming2,outgoing,0,outgoing2,outgoing,outgoing3,(String)numbers.get(fromnum) ,fromnum,numbers ).start();
}
else if(DestBase ==3)
{
System.out.println("Destination Detecyed as" + DestBase);
// destctrlout = new PrintWriter
// (rctr3.getOutputStream(), true );
// destctrlout.println(numbers.get(fromnum) + " 16");
new BaseStationHandler(incoming,outgoing3,0,outgoing,outgoing2,outgoing3,fromnum,(String)numbers.get(fromnum),numbers ).start();
new BaseStationHandler(incoming3,outgoing,0,outgoing,outgoing2,outgoing3,(String)numbers.get(fromnum) ,fromnum,numbers ).start();
}
}
if (Initial2.trim().equals("."))
done2 = true;
}
}
}
// Listening to control channel 2
else if(counter ==3)
{
boolean done3 = false;
ctrl1in = new BufferedReader
(new InputStreamReader(ctr3.getInputStream()));
ctrlout = new PrintWriter(rctr3.getOutputStream(), true );
ctrlout2 = new PrintWriter
(rctr1.getOutputStream(), true );
ctrlout3 = new PrintWriter
(rctr2.getOutputStream(), true );
while (!done3)
{
System.out.println(" I am here in while again");
String Initial3 = ctrl1in.readLine();
if (Initial3 == null) done3 = true;
else
{
System.out.println(Initial3);
StringTokenizer tok = new StringTokenizer(Initial3);
String BaseId = tok.nextToken(" ");
String fromnum =tok.nextToken(" ");
String Mesgtype = tok.nextToken(" ");
if(Mesgtype.equals("10"))
tonum = tok.nextToken(" ");
if (Mesgtype.equals("0"))
{
//System.out.println("Message type 0 received");
//ctrlout2.println( numbers.get(fromnum) + Mesgtype);
}
// if it is a call request
if(Mesgtype .equals("10") )
{
numbers.put(fromnum, new String(tonum));
numbers.put(tonum, new String(fromnum));
System.out.print("You are " + BaseId + " at" + incoming.getInetAddress()+ " you want me to connect " + fromnum);
System.out.println(" and " + tonum);
StringTokenizer ToAreaCode = new StringTokenizer(tonum);
StringTokenizer FromAreaCode = new StringTokenizer(fromnum);
String ToCode = ToAreaCode.nextToken("-");
String num1 = ToAreaCode.nextToken(" ");
String FromCode =FromAreaCode.nextToken("-");
String num2 = ToAreaCode.nextToken(" ");
numbers.put(num2, new Integer(counter));
if (ToCode.equals(FromCode))
{
approved = 1;
// Le should give a method for approval
if(approved ==1)
out.println("Approved Same " + fromnum + " " + tonum);
else
out.println("Denied Same " + fromnum +" " + tonum);
}
else
{
different =1;
approved =1;
if (ToCode.equals("214"))
DestBase = 2;
else if (ToCode.equals("972"))
DestBase =3;
else if(ToCode.equals("817"))
DestBase =1;
numbers.put(num1, new Integer(DestBase));
if(approved ==1)
{
ctrlout.println("Approved Different" + fromnum + " "+ tonum);
//Socket destin = 9000;
destctrlout = new PrintWriter
(rctr1.getOutputStream(), true );
destctrlout.println( tonum + " 5");
//Thread.sleep(10000);
ctrlout.println( numbers.get(tonum) + " 0");
//Thread.sleep(20000);
ctrlout.println( numbers.get(tonum) + " 16");
Mesgtype = "16";
// Write a function which returns a socket number basing on the
// phone number given
}
else
ctrlout.println("Denied Different " + fromnum + " "+ tonum);
}
}
if ( (approved ==1) && (different ==1) )
{
System.out.println("Recived message 0");
if(DestBase ==1)
{
System.out.println("Destination Detecyed as" + DestBase);
// destctrlout = new PrintWriter
// (rctr1.getOutputStream(), true );
// destctrlout.println(numbers.get(fromnum) + " 16");
new BaseStationHandler(incoming,outgoing3,0,outgoing3,outgoing2,outgoing,fromnum,(String)numbers.get(fromnum),numbers ).start();
new BaseStationHandler(incoming3,outgoing,0,outgoing3,outgoing2,outgoing,(String)numbers.get(fromnum) ,fromnum,numbers ).start();
}
else if(DestBase ==2)
{
System.out.println("Destination Detecyed as" + DestBase);
// destctrlout = new PrintWriter
// (rctr2.getOutputStream(), true );
// destctrlout.println(numbers.get(fromnum) + " 16");
new BaseStationHandler(incoming,outgoing2,0,outgoing3,outgoing2,outgoing,fromnum,(String)numbers.get(fromnum),numbers ).start();
new BaseStationHandler(incoming2,outgoing,0,outgoing3,outgoing2,outgoing,(String)numbers.get(fromnum) ,fromnum,numbers ).start();
}
}
if (Initial3.trim().equals("."))
done3 = true;
}
}
}
//Listening to control channel 3
}
catch (IOException e)
{ System.out.println(e);
}
}
private Socket incoming;
private Socket outgoing;
private Socket incoming2;
private Socket outgoing2;
private Socket incoming3;
private Socket outgoing3;
private Socket ctr1;
private Socket ctr2;
private Socket ctr3;
private Socket rctr1;
private Socket rctr2;
private Socket rctr3;
private int counter;
}
/* The following class just knows how to handle the conversation and close it when some one presses a dot on a line by itself */
class BaseStationHandler extends Thread
{
public BaseStationHandler(Socket src,Socket des, int c,Socket out1,Socket out2,Socket out3, String destnum,String srcnum,Hashtable numbe)
{ incoming =src;outgoing= des ;counter = c; outgoing1 = out1;outgoing2 = out2;outgoing3 =out3;dest_num =destnum;src_num =srcnum ;numbers =numbe;}
public void run()
{ try
{
BufferedReader in = new BufferedReader
(new InputStreamReader(incoming.getInputStream()));
PrintWriter out = new PrintWriter
(outgoing.getOutputStream(), true );
boolean done = false;
PrintWriter out1 = new PrintWriter
(outgoing1.getOutputStream(), true );
PrintWriter out2 = new PrintWriter
(outgoing2.getOutputStream(), true );
PrintWriter out3 = new PrintWriter
(outgoing3.getOutputStream(), true );
System.out.println("Created a nesting Thread for " + src_num + dest_num);
while (!done)
{
/* The line is read from 8888 of one BaseStation and written to 9999 of the second basestation
with the destination number attached in front of what it receives from
BAseStation. */
String dest = in.readLine();
System.out.println(" I read a line in this thread that handles" + src_num);
if (dest == null) done = true;
else
{
StringTokenizer tok = new StringTokenizer(dest, " ");
String junk = tok.nextToken(" ");
String src = tok.nextToken(" ");
String mesg = tok.nextToken(";");
src_num= src;
if( src .equals(src_num))
{
out.println(numbers.get(src)+ " "+mesg +";");
if (dest.trim().equals("."))
done = true;
} // end if
else {
String desnum = (String)numbers.get(src_num);
StringTokenizer tok1 =new StringTokenizer(desnum," -");
String areacode = tok1.nextToken("-");
String numb = tok1.nextToken(" ");
Integer basei = (Integer)numbers.get(numb);
if (basei .equals("1"))
out1.println(numbers.get(src)+ " "+ mesg);
if (basei .equals("2"))
out2.println(numbers.get(src)+ " " + mesg);
if (basei.equals("3"))
out3.println(numbers.get(src) +" " + mesg);
} // end else
}
}
} catch (IOException e)
{System.out.println("Error :" + e);}
}
private Socket outgoing;
private Socket incoming;
private int counter;
private Socket outgoing1;
private Socket outgoing2;
private Socket outgoing3;
private String src_num;
private String dest_num;
Hashtable numbers;
}
               (
geocities.com/vienna/7079/src)                   (
geocities.com/vienna/7079)                   (
geocities.com/vienna)