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

CORBA_quo.java

Go to the documentation of this file.
00001 /*
00002  * CORBA_quo.java
00003  *
00004  * Copyright (c) 2003, 2004 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, but JacORB does not like file
00069      * URIs.
00070      *
00071      * @param ior An IOR to parse and optionally convert.
00072      * @return An IOR that JacORB will like.
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      * The IOR for the Broker::Manager object.
00107      */
00108     private static final String MANAGER_IOR =
00109         readFileIOR(System.getProperty("edu.utah.broker.manager",
00110                                        "file://manager.ior"));
00111 
00112     /**
00113      * The IOR for the Broker's QuoKernel object.
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      * The IOR for the Broker::TaskFactory object.
00121      */
00122     private static final String TASK_IOR =
00123         readFileIOR(System.getProperty("edu.utah.broker.task"));
00124 
00125     /**
00126      * Arguments to the ORB.
00127      */
00128     private String args[];
00129 
00130     /**
00131      * The main ORB object.
00132      */
00133     private ORB orb;
00134 
00135     /**
00136      * Construct a CORBA_quo object.
00137      *
00138      * @param args The arguments to the ORB.
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      * Attach the given Servant object to the ORB and interpose it with a
00150      * broker delegate.
00151      *
00152      * @param name The name of the object.
00153      * @param servant The servant to activate.
00154      */
00155     protected void runServer(String name, final Servant servant)
00156         throws Exception
00157     {
00158         POA root_poa;
00159 
00160         /* Standard init stuff... */
00161         root_poa = POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
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         byte soid[] = 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(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         /* 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         root_poa.the_POAManager().activate();
00239         orb.run();
00240     }
00241 }

Generated on Fri Oct 22 07:50:24 2004 for CPU Broker by  doxygen 1.3.9.1