00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 import java.io.*;
00019 
00020 import imagerec.corba.CORBA;
00021 
00022 import FrameManip.Frame;
00023 
00024 import org.omg.CORBA.ORB;
00025 import org.omg.CORBA.Policy;
00026 import org.omg.PortableServer.POA;
00027 import org.omg.PortableServer.POAHelper;
00028 import org.omg.PortableServer.Servant;
00029 import org.omg.PortableServer.ThreadPolicy;
00030 import org.omg.PortableServer.ThreadPolicyValue;
00031 
00032 import org.omg.CosNaming.NamingContextExt;
00033 import org.omg.CosNaming.NamingContextExtHelper;
00034 import org.omg.CosNaming.NameComponent;
00035 
00036 import omg.org.CosPropertyService.Property;
00037 
00038 import imagerec.graph.ImageData;
00039 
00040 import imagerec.util.ImageDataManip;
00041 
00042 import com.bbn.quo.*;
00043 import com.bbn.quo.corba.*;
00044 
00045 import Broker.Manager;
00046 import Broker.NamedValue;
00047 import Broker.TaskFactory;
00048 import Broker.RealTimeTask;
00049 
00050 import unix.Process;
00051 
00052 
00053 
00054 
00055 public class CORBA_quo
00056     extends CORBA
00057 {
00058 
00059 
00060 
00061     static {
00062         System.loadLibrary("JavaUnixUtils"); 
00063     }
00064 
00065 
00066 
00067 
00068 
00069 
00070 
00071 
00072 
00073 
00074     private static String readFileIOR(String ior)
00075     {
00076         String retval = null;
00077         
00078         if( ior.startsWith("file://") )
00079         {
00080             try
00081             {
00082                 String filename = ior.substring(7);
00083                 byte bits[];
00084                 
00085                 bits = new byte[(int)new File(filename).length()];
00086                 new FileInputStream(filename).read(bits);
00087                 retval = new String(bits);
00088             }
00089             catch(FileNotFoundException e)
00090             {
00091                 e.printStackTrace();
00092             }
00093             catch(IOException e)
00094             {
00095                 e.printStackTrace();
00096             }
00097         }
00098         else
00099         {
00100             retval = ior;
00101         }
00102         return retval;
00103     }
00104 
00105 
00106 
00107 
00108     private static final String MANAGER_IOR =
00109         readFileIOR(System.getProperty("edu.utah.broker.manager",
00110                                        "file://manager.ior"));
00111 
00112 
00113 
00114 
00115     private static final String QUO_KERNEL_IOR =
00116         readFileIOR(System.getProperty("edu.utah.broker.quo-kernel",
00117                                        "file://quoKernel.ior"));
00118 
00119 
00120 
00121 
00122     private static final String TASK_IOR =
00123         readFileIOR(System.getProperty("edu.utah.broker.task"));
00124 
00125 
00126 
00127 
00128     private String args[];
00129 
00130 
00131 
00132 
00133     private ORB orb;
00134 
00135 
00136 
00137 
00138 
00139 
00140     public CORBA_quo(String args[])
00141     {
00142         super(args);
00143         
00144         this.args = args;
00145         this.orb = ORB.init(args, null);
00146     }
00147 
00148 
00149 
00150 
00151 
00152 
00153 
00154 
00155     protected void runServer(String name, final Servant servant)
00156         throws Exception
00157     {
00158         POA root_poa;
00159 
00160         
00161         root_poa = POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
00162 
00163         
00164         ThreadPolicy thread =
00165             root_poa.create_thread_policy(ThreadPolicyValue.
00166                                           SINGLE_THREAD_MODEL);
00167 
00168         POA poa = root_poa.create_POA("SingleThread",
00169                                       root_poa.the_POAManager(),
00170                                       new Policy[] {
00171                                           thread
00172                                       });
00173 
00174         
00175         byte soid[] = poa.activate_object(servant);
00176 
00177         
00178         NamingContextExt namingContext = namingContext(orb);
00179         namingContext.rebind(namingContext.to_name("real_" + name),
00180                              poa.servant_to_reference(servant));
00181         
00182         RealTimeTask rtt;
00183         Manager manager;
00184         QuoKernel qk;
00185 
00186         
00187         qk = QuoKernelHelper.
00188             narrow(this.orb.string_to_object(QUO_KERNEL_IOR));
00189         
00190         rtt = Broker.RealTimeTaskHelper.
00191             narrow(this.orb.string_to_object(TASK_IOR));
00192 
00193         manager = Broker.ManagerHelper.
00194             narrow(this.orb.string_to_object(MANAGER_IOR));
00195 
00196         org.omg.CORBA.Any period_any = orb.create_any();
00197         period_any.insert_string("500ms");
00198         
00199         org.omg.CORBA.Any pid_any = orb.create_any();
00200         pid_any.insert_long(Process.getProcessID());
00201 
00202         manager.AddTask(rtt, new NamedValue[] {
00203             new NamedValue("period", period_any),
00204             new NamedValue("pid", pid_any),
00205         });
00206         
00207         ATRWrapper aw = new ATRWrapper(rtt, 500000, 500000);
00208 
00209         
00210         aw.initSysconds(qk);
00211         aw.initCallbacks();
00212         aw.linkContract(qk);
00213         aw.linkRemoteObject(FrameManip.ProcessorHelper.
00214                             narrow(poa.id_to_reference(soid)));
00215         
00216         poa.activate_object(aw);
00217 
00218         try
00219         {
00220             ImageData id;
00221 
00222             id = ImageDataManip.create((int)0, (long)0);
00223             aw.process(new Frame(id.time,
00224                                  new Property[0],
00225                                  id.header,
00226                                  ImageDataManip.writePPM(id)));
00227         }
00228         catch(Throwable th)
00229         {
00230             System.gc();
00231             System.gc();
00232         }
00233 
00234         
00235         namingContext.rebind(namingContext.to_name(name),
00236                              poa.servant_to_reference(aw));
00237         
00238         root_poa.the_POAManager().activate();
00239         orb.run();
00240     }
00241 }