Write a program in CORBA which takes a long sentence from the client (at least 7 words), passes to the server, server separates out each word
and sends them back to client in reverse order, and displays the words on the client side.
Code for program which takes a long sentence from the client passes to the server,server separates out each word and sends them back to client in reverse order in Java
----------------------------------------------------------------------------------
IDL
----------------------------------------------------------------------------------
module ReverseWordsApp
{
interface ReverseWords
{
string sendString(instring s);
oneway void shutdown();
};
};
--------------------------------------------------------------------------------------------------
Server
--------------------------------------------------------------------------------------------------
import ReverseWordsApp.*;
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.*;
import org.omg.PortableServer.*;
import org.omg.PortableServer.POA;
import java.util.Properties;
import java.util.StringTokenizer;
class ReverseWordsImpl extends ReverseWordsPOA
{
private ORB orb;
publicvoid setORB(ORB orb_val)
{
orb = orb_val;
}
public String sendString(String s)
{
String words = new String();
String newstr = new String();
StringTokenizer st = new StringTokenizer(s, " ");
while (st.hasMoreTokens())
{
words = st.nextToken();
StringBuffer sb = new StringBuffer(words);
newstr = newstr + " " + sb.reverse();
}
return newstr;
}
publicvoid shutdown()
{
orb.shutdown(false);
}
}
publicclass ReverseWordsServer
{
publicstaticvoid main(String args[])
{
try
{
ORB orb = ORB.init(args, null);
POA rootpoa = POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
rootpoa.the_POAManager().activate();
ReverseWordsImpl reverseWordsImpl = new ReverseWordsImpl();
reverseWordsImpl.setORB(orb);
org.omg.CORBA.Object ref = rootpoa.servant_to_reference(reverseWordsImpl);
ReverseWords href = ReverseWordsHelper.narrow(ref);
org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService");
NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);
String name = "ReverseWords";
NameComponent path[] = ncRef.to_name( name );
ncRef.rebind(path, href);
System.out.println("ReverseWordsServer ready and waiting ...");
orb.run();
}
catch (Exception e)
{
System.err.println("ERROR: " + e);
e.printStackTrace(System.out);
}
System.out.println("ReverseWordsServer Exiting ...");
}
}
--------------------------------------------------------------------------------------------------
Client
--------------------------------------------------------------------------------------------------
import ReverseWordsApp.*;
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.*;
import java.util.StringTokenizer;
import java.io.*;
publicclass ReverseWordsClient
{
public String getText() throws IOException
{
String s = new String();
try
{
System.out.print("Enter the Text : ");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
s = br.readLine();
}
catch (IOException e)
{
System.out.println(e);
}
return s;
}
publicstaticvoid main(String args[])
{
ReverseWords reverseWordsImpl = null;
ReverseWordsClient cc = new ReverseWordsClient();
try
{
ORB orb = ORB.init(args, null);
org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService");
NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);
String name = "ReverseWords";
reverseWordsImpl = ReverseWordsHelper.narrow(ncRef.resolve_str(name));
String s = cc.getText();
String str = reverseWordsImpl.sendString(s);
StringTokenizer st = new StringTokenizer(str, " ");
String words = new String();
while (st.hasMoreTokens())
{
words = st.nextToken();
System.out.println(words);
}
reverseWordsImpl.shutdown();
}
catch (Exception e)
{
System.out.println("ERROR : " + e) ;
e.printStackTrace(System.out);
}
}
}
-------------------------------------------------------------------------------------------------
Output
-------------------------------------------------------------------------------------------------
Enter the Text : This is CORBA Program
sihT
si
ABROC
margorP