import java.util.*;
import java.io.*;
import org.omg.CORBA.ORB;
import org.omg.PortableServer.POA;
import org.omg.PortableServer.POAHelper;
public class MessageApp {
private static void usage(){
System.out.println(
"gmake -f <makefile> ARGS=\"<OPTIONS>\":\n" +
"OPTIONS:\n" +
" -cr <receiver object reference file>: Run CORBA message receiver.\n" +
" -nr <domainId>: Run RTI DDS message receiver within specified domain.\n" +
" -s <receiver object reference file> <domainId>: Run client used to send messages to CORBA and RTI DDS receivers within specified domain.\n"
);
}
public static void main(String[] args) {
int domainId = 0;
if (args.length < 1){
usage();
return;
}
Properties p = new Properties();
p.put("org.omg.CORBA.ORBClass", "org.jacorb.orb.ORB");
p.put("org.omg.CORBA.ORBSingletonClass", "org.jacorb.orb.ORBSingleton");
if (args[0].equals("-nr")) {
domainId = Integer.valueOf(args[1]).intValue();
MessageDDSSubscriber ddsSubscriber = new MessageDDSSubscriber(domainId);
if (!ddsSubscriber.run()) {
System.out.println("Error running RTI DDS subscriber");
}
} else if (args[0].equals("-cr")) {
if (args.length != 2) {
usage();
return;
}
try
{
File iorFile = new File(args[1]);
ORB theOrb = ORB.init(args, p);
POA poa = POAHelper.narrow(
theOrb.resolve_initial_references("RootPOA"));
poa.the_POAManager().activate();
MessageReceiverImpl msgReceiver = new MessageReceiverImpl();
org.omg.CORBA.Object o = poa.servant_to_reference(
msgReceiver);
PrintWriter ps = new PrintWriter(
new FileOutputStream(iorFile));
ps.println( theOrb.object_to_string( o ) );
ps.close();
System.out.println("CORBA Servant is now ready to receive requests...");
while(!msgReceiver.close){
if (theOrb.work_pending()){
theOrb.perform_work();
} else {
Thread.sleep(10);
}
}
theOrb.shutdown(true);
} catch ( Exception e ) {
System.out.println(e.getMessage());
}
} else {
Calendar calendar = null;
boolean end = false;
if (args.length != 3) {
usage();
return;
}
try {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
domainId = Integer.valueOf(args[2]).intValue();
MessageDDSPublisher ddsPublisher = new MessageDDSPublisher(domainId);
Message msg = new Message();
msg.time = new DateTime();
if (!ddsPublisher.start()) {
throw new Exception("Error starting DDS publisher");
}
ORB theOrb = ORB.init(args, p);
File iorFile = new File(args[1]);
BufferedReader iorReader = new BufferedReader(new FileReader(iorFile));
String ior = iorReader.readLine();
iorReader.close();
MessageReceiver corbaReceiver = MessageReceiverHelper.narrow(
theOrb.string_to_object(ior));
while (!end) {
System.out.print("sender> ");
msg.msg = in.readLine();
if (!msg.msg.equals("")) {
calendar = new GregorianCalendar();
msg.time.month = calendar.get(Calendar.MONTH) + 1;
msg.time.day = calendar.get(Calendar.DAY_OF_MONTH);
msg.time.year = calendar.get(Calendar.YEAR);
msg.time.hour = calendar.get(Calendar.HOUR_OF_DAY);
msg.time.minute = calendar.get(Calendar.MINUTE);
msg.time.second = calendar.get(Calendar.SECOND);
if (!ddsPublisher.publish(msg)) {
throw new Exception("Error publishing to DDS");
}
corbaReceiver.sendMessage(msg);
if (msg.msg.equals("quit")) {
end = true;
}
}
}
Thread.sleep(1000);
ddsPublisher.stop();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
}