Name Space Implementation and Name Resolution
Problem Statement
Write a program to implement/simulate a name space and resolve names with below structure by iterative method of name resolution. The program consists of three servers and a client. The client sends request to the Server: One. The Server One then resolves the name and responds to the client. If the server is unable to resolve the name it responds with message of the containing the next server which can resolve the name, which is decided on the base of a give name-space tree structure, so on and so forth. The client and servers should display every message send and received.
Description
A Name Space can be represented as a labeled, directed graph with two types of nodes.
A leaf node represents a named entity and has the property that it had no outgoing edges.
A directory node has a number of outgoing edges, each labeled with a name.
Name Space offer a convenient mechanism for storing and retrieving information about entities by means of names.
The process of looking up a name is called Name Resolution.
Source Code
NSServerOne.java
NSServerTwo.java
NSServerThree.java
NSClient.java
OUTPUT:
Client
C:\Documents and Settings\Admin\Desktop\NameSpace>java NSClient
Client 1....
Enter the data
edu/uni/comp/emp.txt
udcs01/edu/uni/comp/emp.txt/
udcs02/uni/comp/emp.txt/
udcs02/uni/comp/emp.txt/
udcs03/comp/emp.txt/
udcs03/comp/emp.txt/
emp.txt
emp.txt
Enter the data
Server1
C:\Documents and Settings\Admin\Desktop\NameSpace>java NSServerOne
Server 1....
udcs01/edu/uni/comp/emp.txt/
udcs01
edu
uni
comp
emp.txt
udcs01 edu udcs01edu
true udcs01 edu
udcs01 edu eduuni
udcs01 edu uniudcs02
udcs01 edu eduiit
udcs01 edu iitemp.txt
udcs01 edu udcs01com
udcs01 edu comgoogle
udcs01 edu googleudcs02
edu uni udcs01edu
edu uni eduuni
true edu uni
edu uni uniudcs02
edu uni eduiit
edu uni iitemp.txt
edu uni udcs01com
edu uni comgoogle
edu uni googleudcs02
uni comp udcs01edu
uni comp eduuni
uni comp uniudcs02
udcs02/
udcs02/uni/comp/emp.txt/ udcs02/uni/comp/emp.txt/
udcs02/uni/comp/emp.txt/
Server 2
C:\Documents and Settings\Admin\Desktop\NameSpace>java NSServerTwo
Server 2....
udcs02/uni/comp/emp.txt/
udcs02
uni
comp
emp.txt
udcs02 uni udcs02uni
true udcs02 uni
udcs02 uni unicomp
udcs02 uni compudcs03
udcs02 uni unieco
udcs02 uni ecoemp.txt
udcs02 uni eduiit
udcs02 uni iitemp.txt
udcs02 uni udcs02google
udcs02 uni googleemp.txt
uni comp udcs02uni
uni comp unicomp
true uni comp
uni comp compudcs03
uni comp unieco
uni comp ecoemp.txt
uni comp eduiit
uni comp iitemp.txt
uni comp udcs02google
uni comp googleemp.txt
comp emp.txt udcs02uni
comp emp.txt unicomp
comp emp.txt compudcs03
udcs03/
udcs03/comp/emp.txt/ udcs03/comp/emp.txt/
udcs03/comp/emp.txt/
Waiting to send....
Server 3
C:\Documents and Settings\Admin\Desktop\NameSpace>java NSServerThre
Server 3....
udcs03/comp/emp.txt/
udcs03
comp
emp.txt
udcs03 comp udcs03comp
true udcs03 comp
udcs03 comp compemp.txt
comp emp.txt udcs03comp
comp emp.txt compemp.txt
true comp emp.txt
emp.txt
emp.txt
Problem Statement
Write a program to implement/simulate a name space and resolve names with below structure by iterative method of name resolution. The program consists of three servers and a client. The client sends request to the Server: One. The Server One then resolves the name and responds to the client. If the server is unable to resolve the name it responds with message of the containing the next server which can resolve the name, which is decided on the base of a give name-space tree structure, so on and so forth. The client and servers should display every message send and received.
Description
A Name Space can be represented as a labeled, directed graph with two types of nodes.
A leaf node represents a named entity and has the property that it had no outgoing edges.
A directory node has a number of outgoing edges, each labeled with a name.
Name Space offer a convenient mechanism for storing and retrieving information about entities by means of names.
The process of looking up a name is called Name Resolution.
Source Code
NSServerOne.java
import java.io.*;
import java.net.*;
import java.util.*;
class Server1
{
InetAddress IAHost;
int sendPort,recPort;
String dataStr="";
public static LinkedList objNSList = new LinkedList();
Server1(InetAddress IAHost)
{
this.IAHost=IAHost;
}
void setSendPort(int sendPort)
{
this.sendPort=sendPort;
}
void setRecPort(int recPort)
{
this.recPort=recPort;
}
void sendData() throws Exception
{
BufferedReader objBr;
DatagramSocket sendDS;
DatagramPacket sendDP;
sendDS = new DatagramSocket(sendPort);
sendDP = new DatagramPacket
(dataStr.getBytes(),dataStr.length(),IAHost,sendPort-1000);
sendDS.send(sendDP);
sendDS.close();
}
void recData() throws Exception
{
byte bBuffer[] = new byte[256];
int iStartVal = 0,iEndVal = 0;
String msgStr,newMsgStr="";
DatagramSocket recDS;
DatagramPacket recDP;
LinkedList objLblList = new LinkedList();
recDS = new DatagramSocket(recPort);
recDP = new DatagramPacket(bBuffer,bBuffer.length);
recDS.receive(recDP);
recDS.close();
msgStr = new String(recDP.getData(),0,recDP.getLength());
System.out.println(msgStr);
while (true)
{
iEndVal = msgStr.indexOf('/', iStartVal);
if(iEndVal == -1)
break;
char chTempArr[] = new char[20];
msgStr.getChars(iStartVal,iEndVal,chTempArr,0);
String lblStr = new String(chTempArr);
iStartVal = iEndVal + 1;
lblStr= lblStr.trim();
objLblList.add(lblStr);
System.out.println(lblStr);
}
boolean b=false;
one: for (int iLblListCntr = 0; iLblListCntr < objLblList.size()-1;iLblListCntr++)
{
String lblMsgStr = String.valueOf(objLblList.get(iLblListCntr));
String nextLblMsgStr = String.valueOf(objLblList.get(iLblListCntr + 1));
lblMsgStr = lblMsgStr.trim();
nextLblMsgStr = nextLblMsgStr.trim();
lblMsgStr = lblMsgStr.toLowerCase();
nextLblMsgStr = nextLblMsgStr.toLowerCase();
b=false;
sec: for (int iNSListCntr = 0; iNSListCntr < objNSList.size(); iNSListCntr++
)
{
String lblNSNodeStr = String.valueOf(objNSList.get(iNSListCntr));
System.out.println(lblMsgStr+" "+nextLblMsgStr+" "+lblNSNodeStr);
lblNSNodeStr = lblNSNodeStr.trim();
lblNSNodeStr = lblNSNodeStr.toLowerCase();
if(lblNSNodeStr.startsWith(lblMsgStr))
{
if(lblNSNodeStr.endsWith(nextLblMsgStr))
{
b=true;
System.out.println(b+" "+lblMsgStr+" "+nextLblMsgStr);
}
if(lblNSNodeStr.endsWith("udcs02"))
{
newMsgStr="udcs02/";
System.out.println(newMsgStr);
for (; iLblListCntr < objLblList.size();iLblListCntr++)
newMsgStr = newMsgStr +
String.valueOf(objLblList.get(iLblListCntr))+"/";
dataStr = newMsgStr.trim();
System.out.println(newMsgStr+" "+dataStr);
break one;
}
else if(lblNSNodeStr.endsWith(".txt"))
{
dataStr = nextLblMsgStr;
System.out.println(dataStr);
break one;
}
}
}
if (b==false)
{
dataStr="n";
System.out.println(b);
break;
}
}
IAHost = InetAddress.getLocalHost();
System.out.println(dataStr);
setSendPort(9002);
sendData();
}
static void createNS() throws Exception
{
String parStr="",childStr="";
BufferedReader objBr;
objNSList.add("udcs01edu");
objNSList.add("eduuni");
objNSList.add("uniudcs02");
objNSList.add("eduiit");
objNSList.add("iitemp.txt");
objNSList.add("udcs01com");
objNSList.add("comgoogle");
objNSList.add("googleudcs02");
}
};
class NSServerOne
{
public static void main(String[] args) throws Exception
{
String dataStr;
BufferedReader objBr;
InetAddress IAHost;
Server1 objClnt;
Server1.createNS();
IAHost = InetAddress.getLocalHost();
objClnt = new Server1(IAHost);
System.out.println("Server 1....");
while (true)
{
objClnt.setSendPort(9002);
objClnt.setRecPort(8001);
objClnt.recData();
objClnt.sendData();
}
}
}
import java.net.*;
import java.util.*;
class Server1
{
InetAddress IAHost;
int sendPort,recPort;
String dataStr="";
public static LinkedList objNSList = new LinkedList();
Server1(InetAddress IAHost)
{
this.IAHost=IAHost;
}
void setSendPort(int sendPort)
{
this.sendPort=sendPort;
}
void setRecPort(int recPort)
{
this.recPort=recPort;
}
void sendData() throws Exception
{
BufferedReader objBr;
DatagramSocket sendDS;
DatagramPacket sendDP;
sendDS = new DatagramSocket(sendPort);
sendDP = new DatagramPacket
(dataStr.getBytes(),dataStr.length(),IAHost,sendPort-1000);
sendDS.send(sendDP);
sendDS.close();
}
void recData() throws Exception
{
byte bBuffer[] = new byte[256];
int iStartVal = 0,iEndVal = 0;
String msgStr,newMsgStr="";
DatagramSocket recDS;
DatagramPacket recDP;
LinkedList objLblList = new LinkedList();
recDS = new DatagramSocket(recPort);
recDP = new DatagramPacket(bBuffer,bBuffer.length);
recDS.receive(recDP);
recDS.close();
msgStr = new String(recDP.getData(),0,recDP.getLength());
System.out.println(msgStr);
while (true)
{
iEndVal = msgStr.indexOf('/', iStartVal);
if(iEndVal == -1)
break;
char chTempArr[] = new char[20];
msgStr.getChars(iStartVal,iEndVal,chTempArr,0);
String lblStr = new String(chTempArr);
iStartVal = iEndVal + 1;
lblStr= lblStr.trim();
objLblList.add(lblStr);
System.out.println(lblStr);
}
boolean b=false;
one: for (int iLblListCntr = 0; iLblListCntr < objLblList.size()-1;iLblListCntr++)
{
String lblMsgStr = String.valueOf(objLblList.get(iLblListCntr));
String nextLblMsgStr = String.valueOf(objLblList.get(iLblListCntr + 1));
lblMsgStr = lblMsgStr.trim();
nextLblMsgStr = nextLblMsgStr.trim();
lblMsgStr = lblMsgStr.toLowerCase();
nextLblMsgStr = nextLblMsgStr.toLowerCase();
b=false;
sec: for (int iNSListCntr = 0; iNSListCntr < objNSList.size(); iNSListCntr++
)
{
String lblNSNodeStr = String.valueOf(objNSList.get(iNSListCntr));
System.out.println(lblMsgStr+" "+nextLblMsgStr+" "+lblNSNodeStr);
lblNSNodeStr = lblNSNodeStr.trim();
lblNSNodeStr = lblNSNodeStr.toLowerCase();
if(lblNSNodeStr.startsWith(lblMsgStr))
{
if(lblNSNodeStr.endsWith(nextLblMsgStr))
{
b=true;
System.out.println(b+" "+lblMsgStr+" "+nextLblMsgStr);
}
if(lblNSNodeStr.endsWith("udcs02"))
{
newMsgStr="udcs02/";
System.out.println(newMsgStr);
for (; iLblListCntr < objLblList.size();iLblListCntr++)
newMsgStr = newMsgStr +
String.valueOf(objLblList.get(iLblListCntr))+"/";
dataStr = newMsgStr.trim();
System.out.println(newMsgStr+" "+dataStr);
break one;
}
else if(lblNSNodeStr.endsWith(".txt"))
{
dataStr = nextLblMsgStr;
System.out.println(dataStr);
break one;
}
}
}
if (b==false)
{
dataStr="n";
System.out.println(b);
break;
}
}
IAHost = InetAddress.getLocalHost();
System.out.println(dataStr);
setSendPort(9002);
sendData();
}
static void createNS() throws Exception
{
String parStr="",childStr="";
BufferedReader objBr;
objNSList.add("udcs01edu");
objNSList.add("eduuni");
objNSList.add("uniudcs02");
objNSList.add("eduiit");
objNSList.add("iitemp.txt");
objNSList.add("udcs01com");
objNSList.add("comgoogle");
objNSList.add("googleudcs02");
}
};
class NSServerOne
{
public static void main(String[] args) throws Exception
{
String dataStr;
BufferedReader objBr;
InetAddress IAHost;
Server1 objClnt;
Server1.createNS();
IAHost = InetAddress.getLocalHost();
objClnt = new Server1(IAHost);
System.out.println("Server 1....");
while (true)
{
objClnt.setSendPort(9002);
objClnt.setRecPort(8001);
objClnt.recData();
objClnt.sendData();
}
}
}
NSServerTwo.java
import java.io.*;
import java.net.*;
import java.util.*;
class Server2
{
InetAddress IAHost;
int sendPort,recPort;
String dataStr="";
public static LinkedList objNSList = new LinkedList();
Server2(InetAddress IAHost)
{
this.IAHost=IAHost;
}
void setSendPort(int sendPort)
{
this.sendPort=sendPort;
}
void setRecPort(int recPort)
{
this.recPort=recPort;
}
void sendData() throws Exception
{
BufferedReader objBr;
DatagramSocket sendDS;
DatagramPacket sendDP;
sendDS = new DatagramSocket(sendPort);
sendDP = new DatagramPacket (dataStr.getBytes(),dataStr.length(),IAHost,sendPort-1000);
sendDS.send(sendDP);
sendDS.close();
}
void recData() throws Exception
{
byte bBuffer[] = new byte[256];
int iStartVal = 0,iEndVal = 0;
String msgStr,newMsgStr="";
DatagramSocket recDS;
DatagramPacket recDP;
LinkedList objLblList = new LinkedList();
recDS = new DatagramSocket(recPort);
recDP = new DatagramPacket(bBuffer,bBuffer.length);
recDS.receive(recDP);
recDS.close();
msgStr = new String(recDP.getData(),0,recDP.getLength());
System.out.println(msgStr);
while (true)
{
iEndVal = msgStr.indexOf('/', iStartVal);
if(iEndVal == -1)
break;
char chTempArr[] = new char[20];
msgStr.getChars(iStartVal,iEndVal,chTempArr,0);
String lblStr = new String(chTempArr);
iStartVal = iEndVal + 1;
lblStr= lblStr.trim();
objLblList.add(lblStr);
System.out.println(lblStr);
}
boolean b=false;
one: for (int iLblListCntr = 0; iLblListCntr < objLblList.size()-1;iLblListCntr++)
{
String lblMsgStr = String.valueOf(objLblList.get(iLblListCntr));
String nextLblMsgStr = String.valueOf(objLblList.get(iLblListCntr + 1));
lblMsgStr = lblMsgStr.trim();
nextLblMsgStr = nextLblMsgStr.trim();
lblMsgStr = lblMsgStr.toLowerCase();
nextLblMsgStr = nextLblMsgStr.toLowerCase();
b=false;
sec: for (int iNSListCntr = 0; iNSListCntr < objNSList.size(); iNSListCntr++ )
{
String lblNSNodeStr = String.valueOf(objNSList.get(iNSListCntr));
System.out.println(lblMsgStr+" "+nextLblMsgStr+" "+lblNSNodeStr);
lblNSNodeStr = lblNSNodeStr.trim();
lblNSNodeStr = lblNSNodeStr.toLowerCase();
if(lblNSNodeStr.startsWith(lblMsgStr))
{
if(lblNSNodeStr.endsWith(nextLblMsgStr))
{
b=true;
System.out.println(b+" "+lblMsgStr+" "+nextLblMsgStr);
}
if(lblNSNodeStr.endsWith("udcs03"))
{
newMsgStr="udcs03/";
System.out.println(newMsgStr);
for (; iLblListCntr < objLblList.size();iLblListCntr++)
newMsgStr = newMsgStr + String.valueOf(objLblList.get(iLblListCntr))+"/";
dataStr = newMsgStr.trim();
System.out.println(newMsgStr+" "+dataStr);
break one;
}
else if(lblNSNodeStr.endsWith(".txt"))
{
dataStr = nextLblMsgStr;
System.out.println(dataStr);
break one;
}
}
}
if (b==false)
{
dataStr="n";
System.out.println(b);
break;
}
}
IAHost = InetAddress.getLocalHost();
System.out.println(dataStr);
System.out.println("Waiting to send....");
setSendPort(9004);
sendData();
}
static void createNS() throws Exception
{
String parStr="",childStr="";
BufferedReader objBr;
objNSList.add("udcs02uni");
objNSList.add("unicomp");
objNSList.add("compudcs03");
objNSList.add("unieco");
objNSList.add("ecoemp.txt");
objNSList.add("eduiit");
objNSList.add("iitemp.txt");
objNSList.add("udcs02google");
objNSList.add("googleemp.txt");
}
};
class NSServerTwo
{
public static void main(String[] args) throws Exception
{
String dataStr;
BufferedReader objBr;
InetAddress IAHost;
Server2 objClnt;
Server2.createNS();
IAHost = InetAddress.getLocalHost();
objClnt = new Server2(IAHost);
System.out.println("Server 2....");
while (true)
{
objClnt.setSendPort(9004);
objClnt.setRecPort(8003);
objClnt.recData();
objClnt.sendData();
}
}
}
import java.net.*;
import java.util.*;
class Server2
{
InetAddress IAHost;
int sendPort,recPort;
String dataStr="";
public static LinkedList objNSList = new LinkedList();
Server2(InetAddress IAHost)
{
this.IAHost=IAHost;
}
void setSendPort(int sendPort)
{
this.sendPort=sendPort;
}
void setRecPort(int recPort)
{
this.recPort=recPort;
}
void sendData() throws Exception
{
BufferedReader objBr;
DatagramSocket sendDS;
DatagramPacket sendDP;
sendDS = new DatagramSocket(sendPort);
sendDP = new DatagramPacket (dataStr.getBytes(),dataStr.length(),IAHost,sendPort-1000);
sendDS.send(sendDP);
sendDS.close();
}
void recData() throws Exception
{
byte bBuffer[] = new byte[256];
int iStartVal = 0,iEndVal = 0;
String msgStr,newMsgStr="";
DatagramSocket recDS;
DatagramPacket recDP;
LinkedList objLblList = new LinkedList();
recDS = new DatagramSocket(recPort);
recDP = new DatagramPacket(bBuffer,bBuffer.length);
recDS.receive(recDP);
recDS.close();
msgStr = new String(recDP.getData(),0,recDP.getLength());
System.out.println(msgStr);
while (true)
{
iEndVal = msgStr.indexOf('/', iStartVal);
if(iEndVal == -1)
break;
char chTempArr[] = new char[20];
msgStr.getChars(iStartVal,iEndVal,chTempArr,0);
String lblStr = new String(chTempArr);
iStartVal = iEndVal + 1;
lblStr= lblStr.trim();
objLblList.add(lblStr);
System.out.println(lblStr);
}
boolean b=false;
one: for (int iLblListCntr = 0; iLblListCntr < objLblList.size()-1;iLblListCntr++)
{
String lblMsgStr = String.valueOf(objLblList.get(iLblListCntr));
String nextLblMsgStr = String.valueOf(objLblList.get(iLblListCntr + 1));
lblMsgStr = lblMsgStr.trim();
nextLblMsgStr = nextLblMsgStr.trim();
lblMsgStr = lblMsgStr.toLowerCase();
nextLblMsgStr = nextLblMsgStr.toLowerCase();
b=false;
sec: for (int iNSListCntr = 0; iNSListCntr < objNSList.size(); iNSListCntr++ )
{
String lblNSNodeStr = String.valueOf(objNSList.get(iNSListCntr));
System.out.println(lblMsgStr+" "+nextLblMsgStr+" "+lblNSNodeStr);
lblNSNodeStr = lblNSNodeStr.trim();
lblNSNodeStr = lblNSNodeStr.toLowerCase();
if(lblNSNodeStr.startsWith(lblMsgStr))
{
if(lblNSNodeStr.endsWith(nextLblMsgStr))
{
b=true;
System.out.println(b+" "+lblMsgStr+" "+nextLblMsgStr);
}
if(lblNSNodeStr.endsWith("udcs03"))
{
newMsgStr="udcs03/";
System.out.println(newMsgStr);
for (; iLblListCntr < objLblList.size();iLblListCntr++)
newMsgStr = newMsgStr + String.valueOf(objLblList.get(iLblListCntr))+"/";
dataStr = newMsgStr.trim();
System.out.println(newMsgStr+" "+dataStr);
break one;
}
else if(lblNSNodeStr.endsWith(".txt"))
{
dataStr = nextLblMsgStr;
System.out.println(dataStr);
break one;
}
}
}
if (b==false)
{
dataStr="n";
System.out.println(b);
break;
}
}
IAHost = InetAddress.getLocalHost();
System.out.println(dataStr);
System.out.println("Waiting to send....");
setSendPort(9004);
sendData();
}
static void createNS() throws Exception
{
String parStr="",childStr="";
BufferedReader objBr;
objNSList.add("udcs02uni");
objNSList.add("unicomp");
objNSList.add("compudcs03");
objNSList.add("unieco");
objNSList.add("ecoemp.txt");
objNSList.add("eduiit");
objNSList.add("iitemp.txt");
objNSList.add("udcs02google");
objNSList.add("googleemp.txt");
}
};
class NSServerTwo
{
public static void main(String[] args) throws Exception
{
String dataStr;
BufferedReader objBr;
InetAddress IAHost;
Server2 objClnt;
Server2.createNS();
IAHost = InetAddress.getLocalHost();
objClnt = new Server2(IAHost);
System.out.println("Server 2....");
while (true)
{
objClnt.setSendPort(9004);
objClnt.setRecPort(8003);
objClnt.recData();
objClnt.sendData();
}
}
}
NSServerThree.java
import java.io.*;
import java.net.*;
import java.util.*;
class Server3
{
InetAddress IAHost;
int sendPort,recPort;
String dataStr="";
public static LinkedList objNSList = new LinkedList();
Server3(InetAddress IAHost)
{
this.IAHost=IAHost;
}
void setSendPort(int sendPort)
{
this.sendPort=sendPort;
}
void setRecPort(int recPort)
{
this.recPort=recPort;
}
void sendData() throws Exception
{
BufferedReader objBr;
DatagramSocket sendDS;
DatagramPacket sendDP;
sendDS = new DatagramSocket(sendPort);
sendDP = new DatagramPacket (dataStr.getBytes(),dataStr.length(),IAHost,sendPort-1000);
sendDS.send(sendDP);
sendDS.close();
}
void recData() throws Exception
{
byte bBuffer[] = new byte[256];
int iStartVal = 0,iEndVal = 0;
String msgStr,newMsgStr="";
DatagramSocket recDS;
DatagramPacket recDP;
LinkedList objLblList = new LinkedList();
recDS = new DatagramSocket(recPort);
recDP = new DatagramPacket(bBuffer,bBuffer.length);
recDS.receive(recDP);
recDS.close();
msgStr = new String(recDP.getData(),0,recDP.getLength());
System.out.println(msgStr);
while (true)
{
iEndVal = msgStr.indexOf('/', iStartVal);
if(iEndVal == -1)
break;
char chTempArr[] = new char[20];
msgStr.getChars(iStartVal,iEndVal,chTempArr,0);
String lblStr = new String(chTempArr);
iStartVal = iEndVal + 1;
lblStr= lblStr.trim();
objLblList.add(lblStr);
System.out.println(lblStr);
}
boolean b=false;
one: for (int iLblListCntr = 0; iLblListCntr < objLblList.size()-1;iLblListCntr++)
{
String lblMsgStr = String.valueOf(objLblList.get(iLblListCntr));
String nextLblMsgStr = String.valueOf(objLblList.get(iLblListCntr + 1));
lblMsgStr = lblMsgStr.trim();
nextLblMsgStr = nextLblMsgStr.trim();
lblMsgStr = lblMsgStr.toLowerCase();
nextLblMsgStr = nextLblMsgStr.toLowerCase();
b=false;
sec: for (int iNSListCntr = 0; iNSListCntr < objNSList.size(); iNSListCntr++ )
{
String lblNSNodeStr = String.valueOf(objNSList.get(iNSListCntr));
System.out.println(lblMsgStr+" "+nextLblMsgStr+" "+lblNSNodeStr);
lblNSNodeStr = lblNSNodeStr.trim();
lblNSNodeStr = lblNSNodeStr.toLowerCase();
if(lblNSNodeStr.startsWith(lblMsgStr))
{
if(lblNSNodeStr.endsWith(nextLblMsgStr))
{
b=true;
System.out.println(b+" "+lblMsgStr+" "+nextLblMsgStr);
}
if(lblNSNodeStr.endsWith("udcs04"))
{
newMsgStr="udcs04/";
System.out.println(newMsgStr);
for (; iLblListCntr < objLblList.size();iLblListCntr++)
newMsgStr = newMsgStr + String.valueOf(objLblList.get(iLblListCntr))+"/"; dataStr = newMsgStr.trim();
System.out.println(newMsgStr+" "+dataStr);
break one;
}
else if(lblNSNodeStr.endsWith(".txt"))
{
dataStr = nextLblMsgStr;
System.out.println(dataStr);
break one;
}
}
}
if (b==false)
{
dataStr="n";
System.out.println(b);
break;
}
}
IAHost = InetAddress.getLocalHost();
System.out.println(dataStr);
setSendPort(9006);
sendData();
}
static void createNS() throws Exception
{
String parStr="",childStr="";
BufferedReader objBr;
objNSList.add("udcs03comp");
objNSList.add("compemp.txt");
}
};
class NSServerThree
{
public static void main(String[] args) throws Exception
{
String dataStr;
BufferedReader objBr;
InetAddress IAHost;
Server3 objClnt;
Server3.createNS();
IAHost = InetAddress.getLocalHost();
objClnt = new Server3(IAHost);
System.out.println("Server 3....");
while (true)
{
objClnt.setSendPort(9006);
objClnt.setRecPort(8005);
objClnt.recData();
objClnt.sendData();
}
}
}
import java.net.*;
import java.util.*;
class Server3
{
InetAddress IAHost;
int sendPort,recPort;
String dataStr="";
public static LinkedList objNSList = new LinkedList();
Server3(InetAddress IAHost)
{
this.IAHost=IAHost;
}
void setSendPort(int sendPort)
{
this.sendPort=sendPort;
}
void setRecPort(int recPort)
{
this.recPort=recPort;
}
void sendData() throws Exception
{
BufferedReader objBr;
DatagramSocket sendDS;
DatagramPacket sendDP;
sendDS = new DatagramSocket(sendPort);
sendDP = new DatagramPacket (dataStr.getBytes(),dataStr.length(),IAHost,sendPort-1000);
sendDS.send(sendDP);
sendDS.close();
}
void recData() throws Exception
{
byte bBuffer[] = new byte[256];
int iStartVal = 0,iEndVal = 0;
String msgStr,newMsgStr="";
DatagramSocket recDS;
DatagramPacket recDP;
LinkedList objLblList = new LinkedList();
recDS = new DatagramSocket(recPort);
recDP = new DatagramPacket(bBuffer,bBuffer.length);
recDS.receive(recDP);
recDS.close();
msgStr = new String(recDP.getData(),0,recDP.getLength());
System.out.println(msgStr);
while (true)
{
iEndVal = msgStr.indexOf('/', iStartVal);
if(iEndVal == -1)
break;
char chTempArr[] = new char[20];
msgStr.getChars(iStartVal,iEndVal,chTempArr,0);
String lblStr = new String(chTempArr);
iStartVal = iEndVal + 1;
lblStr= lblStr.trim();
objLblList.add(lblStr);
System.out.println(lblStr);
}
boolean b=false;
one: for (int iLblListCntr = 0; iLblListCntr < objLblList.size()-1;iLblListCntr++)
{
String lblMsgStr = String.valueOf(objLblList.get(iLblListCntr));
String nextLblMsgStr = String.valueOf(objLblList.get(iLblListCntr + 1));
lblMsgStr = lblMsgStr.trim();
nextLblMsgStr = nextLblMsgStr.trim();
lblMsgStr = lblMsgStr.toLowerCase();
nextLblMsgStr = nextLblMsgStr.toLowerCase();
b=false;
sec: for (int iNSListCntr = 0; iNSListCntr < objNSList.size(); iNSListCntr++ )
{
String lblNSNodeStr = String.valueOf(objNSList.get(iNSListCntr));
System.out.println(lblMsgStr+" "+nextLblMsgStr+" "+lblNSNodeStr);
lblNSNodeStr = lblNSNodeStr.trim();
lblNSNodeStr = lblNSNodeStr.toLowerCase();
if(lblNSNodeStr.startsWith(lblMsgStr))
{
if(lblNSNodeStr.endsWith(nextLblMsgStr))
{
b=true;
System.out.println(b+" "+lblMsgStr+" "+nextLblMsgStr);
}
if(lblNSNodeStr.endsWith("udcs04"))
{
newMsgStr="udcs04/";
System.out.println(newMsgStr);
for (; iLblListCntr < objLblList.size();iLblListCntr++)
newMsgStr = newMsgStr + String.valueOf(objLblList.get(iLblListCntr))+"/"; dataStr = newMsgStr.trim();
System.out.println(newMsgStr+" "+dataStr);
break one;
}
else if(lblNSNodeStr.endsWith(".txt"))
{
dataStr = nextLblMsgStr;
System.out.println(dataStr);
break one;
}
}
}
if (b==false)
{
dataStr="n";
System.out.println(b);
break;
}
}
IAHost = InetAddress.getLocalHost();
System.out.println(dataStr);
setSendPort(9006);
sendData();
}
static void createNS() throws Exception
{
String parStr="",childStr="";
BufferedReader objBr;
objNSList.add("udcs03comp");
objNSList.add("compemp.txt");
}
};
class NSServerThree
{
public static void main(String[] args) throws Exception
{
String dataStr;
BufferedReader objBr;
InetAddress IAHost;
Server3 objClnt;
Server3.createNS();
IAHost = InetAddress.getLocalHost();
objClnt = new Server3(IAHost);
System.out.println("Server 3....");
while (true)
{
objClnt.setSendPort(9006);
objClnt.setRecPort(8005);
objClnt.recData();
objClnt.sendData();
}
}
}
NSClient.java
import java.io.*;
import java.net.*;
class Client
{
InetAddress IAHost;
int sendPort,recPort;
static int iDestCntr;
String dataStr="";
static String uniStr;
Client(InetAddress IAHost)
{
this.IAHost=IAHost;
}
void setSendPort(int sendPort)
{
this.sendPort=sendPort;
}
void setRecPort(int recPort)
{
this.recPort=recPort;
}
void sendData() throws Exception
{
BufferedReader objBr;
DatagramSocket sendDS;
DatagramPacket sendDP;
if(iDestCntr==0)
{
System.out.println("Enter the data");
objBr = new BufferedReader(new InputStreamReader(System.in));
dataStr=objBr.readLine();
dataStr = "udcs01/"+dataStr +"/";
System.out.println(dataStr);
}
else
dataStr=uniStr;
sendDS = new DatagramSocket(sendPort);
sendDP = new DatagramPacket (dataStr.getBytes(),dataStr.length(),IAHost,sendPort-1000);
sendDS.send(sendDP);
sendDS.close();
}
void recData() throws Exception
{
byte bBuffer[] = new byte[256];
String msgStr;
DatagramSocket recDS;
DatagramPacket recDP;
recDS = new DatagramSocket(recPort);
recDP = new DatagramPacket(bBuffer,bBuffer.length);
recDS.receive(recDP);
recDS.close();
msgStr = new String(recDP.getData(),0,recDP.getLength());
msgStr = msgStr.trim();
System.out.println(msgStr);
dataStr = msgStr;
uniStr= msgStr;
dataStr = dataStr.trim();
System.out.println(dataStr);
if (dataStr.endsWith("/"))
iDestCntr = iDestCntr +1;
else
iDestCntr=0;
}
};
class NSClient
{
public static void main(String[] args) throws Exception
{
String dataStr;
BufferedReader objBr;
InetAddress IAHost;
Client objClnt;
System.out.println("Client 1....");
while (true)
{
IAHost = InetAddress.getLocalHost();
objClnt = new Client(IAHost);
if (Client.iDestCntr == 0)
{
objClnt.setSendPort(9001);
objClnt.setRecPort(8002);
}
else if (Client.iDestCntr == 1)
{
objClnt.setSendPort(9003);
objClnt.setRecPort(8004);
}
else if (Client.iDestCntr == 2)
{
objClnt.setSendPort(9005);
objClnt.setRecPort(8006);
}
objClnt.sendData();
objClnt.recData();
}
}
}
import java.net.*;
class Client
{
InetAddress IAHost;
int sendPort,recPort;
static int iDestCntr;
String dataStr="";
static String uniStr;
Client(InetAddress IAHost)
{
this.IAHost=IAHost;
}
void setSendPort(int sendPort)
{
this.sendPort=sendPort;
}
void setRecPort(int recPort)
{
this.recPort=recPort;
}
void sendData() throws Exception
{
BufferedReader objBr;
DatagramSocket sendDS;
DatagramPacket sendDP;
if(iDestCntr==0)
{
System.out.println("Enter the data");
objBr = new BufferedReader(new InputStreamReader(System.in));
dataStr=objBr.readLine();
dataStr = "udcs01/"+dataStr +"/";
System.out.println(dataStr);
}
else
dataStr=uniStr;
sendDS = new DatagramSocket(sendPort);
sendDP = new DatagramPacket (dataStr.getBytes(),dataStr.length(),IAHost,sendPort-1000);
sendDS.send(sendDP);
sendDS.close();
}
void recData() throws Exception
{
byte bBuffer[] = new byte[256];
String msgStr;
DatagramSocket recDS;
DatagramPacket recDP;
recDS = new DatagramSocket(recPort);
recDP = new DatagramPacket(bBuffer,bBuffer.length);
recDS.receive(recDP);
recDS.close();
msgStr = new String(recDP.getData(),0,recDP.getLength());
msgStr = msgStr.trim();
System.out.println(msgStr);
dataStr = msgStr;
uniStr= msgStr;
dataStr = dataStr.trim();
System.out.println(dataStr);
if (dataStr.endsWith("/"))
iDestCntr = iDestCntr +1;
else
iDestCntr=0;
}
};
class NSClient
{
public static void main(String[] args) throws Exception
{
String dataStr;
BufferedReader objBr;
InetAddress IAHost;
Client objClnt;
System.out.println("Client 1....");
while (true)
{
IAHost = InetAddress.getLocalHost();
objClnt = new Client(IAHost);
if (Client.iDestCntr == 0)
{
objClnt.setSendPort(9001);
objClnt.setRecPort(8002);
}
else if (Client.iDestCntr == 1)
{
objClnt.setSendPort(9003);
objClnt.setRecPort(8004);
}
else if (Client.iDestCntr == 2)
{
objClnt.setSendPort(9005);
objClnt.setRecPort(8006);
}
objClnt.sendData();
objClnt.recData();
}
}
}
OUTPUT:
Client
C:\Documents and Settings\Admin\Desktop\NameSpace>java NSClient
Client 1....
Enter the data
edu/uni/comp/emp.txt
udcs01/edu/uni/comp/emp.txt/
udcs02/uni/comp/emp.txt/
udcs02/uni/comp/emp.txt/
udcs03/comp/emp.txt/
udcs03/comp/emp.txt/
emp.txt
emp.txt
Enter the data
Server1
C:\Documents and Settings\Admin\Desktop\NameSpace>java NSServerOne
Server 1....
udcs01/edu/uni/comp/emp.txt/
udcs01
edu
uni
comp
emp.txt
udcs01 edu udcs01edu
true udcs01 edu
udcs01 edu eduuni
udcs01 edu uniudcs02
udcs01 edu eduiit
udcs01 edu iitemp.txt
udcs01 edu udcs01com
udcs01 edu comgoogle
udcs01 edu googleudcs02
edu uni udcs01edu
edu uni eduuni
true edu uni
edu uni uniudcs02
edu uni eduiit
edu uni iitemp.txt
edu uni udcs01com
edu uni comgoogle
edu uni googleudcs02
uni comp udcs01edu
uni comp eduuni
uni comp uniudcs02
udcs02/
udcs02/uni/comp/emp.txt/ udcs02/uni/comp/emp.txt/
udcs02/uni/comp/emp.txt/
Server 2
C:\Documents and Settings\Admin\Desktop\NameSpace>java NSServerTwo
Server 2....
udcs02/uni/comp/emp.txt/
udcs02
uni
comp
emp.txt
udcs02 uni udcs02uni
true udcs02 uni
udcs02 uni unicomp
udcs02 uni compudcs03
udcs02 uni unieco
udcs02 uni ecoemp.txt
udcs02 uni eduiit
udcs02 uni iitemp.txt
udcs02 uni udcs02google
udcs02 uni googleemp.txt
uni comp udcs02uni
uni comp unicomp
true uni comp
uni comp compudcs03
uni comp unieco
uni comp ecoemp.txt
uni comp eduiit
uni comp iitemp.txt
uni comp udcs02google
uni comp googleemp.txt
comp emp.txt udcs02uni
comp emp.txt unicomp
comp emp.txt compudcs03
udcs03/
udcs03/comp/emp.txt/ udcs03/comp/emp.txt/
udcs03/comp/emp.txt/
Waiting to send....
Server 3
C:\Documents and Settings\Admin\Desktop\NameSpace>java NSServerThre
Server 3....
udcs03/comp/emp.txt/
udcs03
comp
emp.txt
udcs03 comp udcs03comp
true udcs03 comp
udcs03 comp compemp.txt
comp emp.txt udcs03comp
comp emp.txt compemp.txt
true comp emp.txt
emp.txt
emp.txt
0 comments:
Confused? Feel free to ask
Post a Comment