Main Page | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members

CORBA_quo.java

Go to the documentation of this file.
00001 /*
00002  * CORBA_quo.java
00003  *
00004  * Copyright (c) 2003 The University of Utah and the Flux Group.
00005  * All rights reserved.
00006  *
00007  * This file is licensed under the terms of the GNU Public License.  
00008  * See the file "license.terms" for restrictions on redistribution 
00009  * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
00010  */
00011 
00012 /**
00013  * @file CORBA_quo.java
00014  *
00015  * Contains a QuO enabled communications model for the ATR.
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  * A CORBA CommunicationsModel for the ATR that includes support for QuO.
00054  */
00055 public class CORBA_quo
00056     extends CORBA
00057 {
00058     /**
00059      * Load some native libraries.
00060      */
00061     static {
00062         System.loadLibrary("JavaUnixUtils"); // XXX grr stupid!
00063     }
00064 
00065     /**
00066      * Convert a file:// style IOR into an IOR: style one.
00067      *
00068      * XXX Technically, this shouldn't be needed.
00069      *
00070      * @param ior An IOR to parse and optionally convert.
00071      * @return An IOR that JacORB will like.
00072      */
00073     private static String readFileIOR(String ior)
00074     {
00075         String retval = null;
00076         
00077         if( ior.startsWith("file://") )
00078         {
00079             try
00080             {
00081                 String filename = ior.substring(7);
00082                 byte bits[];
00083                 
00084                 bits = new byte[(int)new File(filename).length()];
00085                 new FileInputStream(filename).read(bits);
00086                 retval = new String(bits);
00087             }
00088             catch(FileNotFoundException e)
00089             {
00090                 e.printStackTrace();
00091             }
00092             catch(IOException e)
00093             {
00094                 e.printStackTrace();
00095             }
00096         }
00097         else
00098         {
00099             retval = ior;
00100         }
00101         return retval;
00102     }
00103 
00104     /**
00105      * The IOR for the Broker::Manager object.
00106      */
00107     private static final String MANAGER_IOR =
00108         readFileIOR(System.getProperty("edu.utah.broker.manager",
00109                                        "file://manager.ior"));
00110 
00111     /**
00112      * The IOR for the Broker's QuoKernel object.
00113      */
00114     private static final String QUO_KERNEL_IOR =
00115         readFileIOR(System.getProperty("edu.utah.broker.quo-kernel",
00116                                        "file://quoKernel.ior"));
00117 
00118     /**
00119      * The IOR for the Broker::TaskFactory object.
00120      */
00121     private static final String TASK_IOR =
00122         readFileIOR(System.getProperty("edu.utah.broker.task"));
00123 
00124     /**
00125      * Arguments to the ORB.
00126      */
00127     private String args[];
00128 
00129     /**
00130      * The main ORB object.
00131      */
00132     private ORB orb;
00133 
00134     /**
00135      * Construct a CORBA_quo object.
00136      *
00137      * @param args The arguments to the ORB.
00138      */
00139     public CORBA_quo(String args[])
00140     {
00141         super(args);
00142         
00143         this.args = args;
00144         this.orb = ORB.init(args, null);
00145     }
00146 
00147     /**
00148      * Attach the given Servant object to the ORB and interpose it with a
00149      * broker delegate.
00150      *
00151      * @param name The name of the object.
00152      * @param servant The servant to activate.
00153      */
00154     protected void runServer(String name, final Servant servant)
00155         throws Exception
00156     {
00157         POA root_poa;
00158 
00159         /* Standard init stuff... */
00160         root_poa = POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
00161         root_poa.the_POAManager().activate();
00162 
00163         /* Make sure requests are single threaded. */
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         /* Activate the real object and */
00175         poa.activate_object(servant);
00176 
00177         /* ... add it to the NameService, but prefix the name with 'real_'. */
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         /* Get references to the broker objects. */
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         /* Wire everything up. */
00210         aw.initSysconds(qk);
00211         aw.initCallbacks();
00212         aw.linkContract(qk);
00213         aw.linkRemoteObject(FrameManip.ProcessorHelper.
00214                             narrow(servant._this_object()));
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         /* Register with the NameService, this time, using the real name. */
00235         namingContext.rebind(namingContext.to_name(name),
00236                              poa.servant_to_reference(aw));
00237         
00238         orb.run();
00239     }
00240 }

Generated on Mon Dec 1 16:29:06 2003 for CPU Broker by doxygen 1.3.4