00001
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 package gov.lanl.isr.sensix;
00036
00037 import java.util.*;
00038 import org.omg.CORBA.*;
00039 import org.omg.PortableServer.*;
00040 import org.omg.PortableServer.POAManagerPackage.*;
00041 import gov.lanl.isr.sensix.discovery.*;
00042 import gov.lanl.isr.sensix.sensing.*;
00043
00044
00045 public class SenseServer extends RequestPOA
00046 {
00047 public static boolean EXTERNAL_DISCOVERY = false;
00048 public static final String appname = "SENSIX";
00049 public static final boolean TINYOS_CLIENT = true;
00050 public static final int MAX_MOTE_CLIENTS = 1;
00051
00052 public ORB orb;
00053 public POA poa;
00054 public DiscoveryService discover;
00055 public SortedMap<Byte,Capability> local_capabilities;
00056 protected TaskTracking track;
00057 protected Logger logger;
00058 protected boolean own_discover;
00059 protected SenseClient local_client;
00060 protected LevelOneClient[] motes;
00061
00062
00063 public SenseServer(int id, int level, String[] args, Properties props,
00064 String app, byte debug_level, String l_one)
00065 throws org.omg.CORBA.SystemException, org.omg.CORBA.UserException,
00066 DiscoveryException {
00067 logger = new Logger(app, debug_level);
00068 orb = ORB.init(args, props);
00069 poa = POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
00070 poa.the_POAManager().activate();
00071
00072 local_client = null;
00073 track = new TaskTracking();
00074 local_capabilities = Collections.synchronizedSortedMap(
00075 new TreeMap<Byte,Capability>());
00076 motes = new LevelOneClient[MAX_MOTE_CLIENTS];
00077 for (int i = 0; i < MAX_MOTE_CLIENTS; i++)
00078 motes[i] = null;
00079
00080 if (EXTERNAL_DISCOVERY) {
00081 org.omg.CORBA.Object objref =
00082 orb.resolve_initial_references("DiscoveryService");
00083 discover = (DiscoveryService)SelfHelper.narrow(objref);
00084 own_discover = false;
00085 }
00086 else {
00087 discover = new DiscoveryService(orb, id, (byte)level, app,
00088 debug_level);
00089 own_discover = true;
00090 }
00091
00092 if (level > 1) {
00093 local_client = new SenseClient(orb, poa, discover, app,
00094 debug_level);
00095 local_client.linkServer(this);
00096 }
00097 if (l_one != null && level == 2) {
00098 if (TINYOS_CLIENT) {
00099 motes[0] = new TinyOSClient(this, app, debug_level, l_one);
00100 }
00102 }
00103 }
00104
00105
00106 public void log(Logger.LOG type, String msg) {
00107 logger.log(type, msg);
00108 }
00109
00110 public void run() {
00111 orb.run();
00112 }
00113
00114 public void shutdown() {
00115 log(Logger.LOG.DEBUG, "Shutting down the ORB");
00116 if (own_discover)
00117 discover.shutdown();
00118
00119 if (local_client != null)
00120 local_client.shutdown();
00121
00122 for (int i = 0; i < MAX_MOTE_CLIENTS; i++) {
00123 if (motes[i] != null)
00124 motes[i].shutdown();
00125 }
00126
00127 try {
00128 poa.the_POAManager().deactivate(false, false);
00129 } catch (AdapterInactive e) {}
00130 orb.shutdown(true);
00131 orb.destroy();
00132
00133 log(Logger.LOG.INFO, "Shutdown");
00134 }
00135
00136
00137
00143 public void addCapability(byte cap, Capability obj) {
00144 try {
00145 org.omg.CORBA.Object ref = poa.servant_to_reference(this);
00146 gov.lanl.isr.sensix.Request req = RequestHelper.narrow(ref);
00147
00148 discover.registerObject(cap, req);
00149 synchronized(this) {
00150 local_capabilities.put(new Byte(cap), obj);
00151 }
00152 log(Logger.LOG.DEBUG,
00153 "Added " + SenseUtil.capabilityToString(cap));
00154 } catch (DiscoveryException de) {
00155 log(Logger.LOG.ERROR, "Discovery exception while registering " +
00156 "capability: " + de.description);
00157 } catch (org.omg.CORBA.UserException ue) {
00158 log(Logger.LOG.ERROR, "CORBA exception from POA while " +
00159 "registering capability:");
00160 ue.printStackTrace(System.err);
00161 }
00162 }
00163
00164
00165
00172 public void apply(Functor funct, Response callback) {
00173 if (funct == null) {
00174 log(Logger.LOG.ERROR, "Apply on a null functor");
00175 return;
00176 }
00177 TaskImpl task = new TaskImpl(null, funct, callback);
00178
00179 log(Logger.LOG.DEBUG, "apply: " + funct.asString());
00180 synchronized(this) {
00181 track.add(task);
00182 track.num_peers(funct, 1);
00183 if (local_client != null)
00184 local_client.devolve(funct);
00185
00186 for (int i = 0; i < MAX_MOTE_CLIENTS; i++) {
00187 if (motes[i] != null)
00188 motes[i].apply(funct, callback);
00189 }
00190
00191 Sensory f_s = SensoryHelper.narrow(funct);
00192 Byte key = new Byte(f_s.sensor());
00193 if (local_capabilities.containsKey(key))
00194 ((Capability)local_capabilities.get(key)).acquire(funct);
00195 }
00196
00197 if (local_client == null) {
00198 if (callback != null)
00199 callback.aggregate(funct);
00200 track.remove(funct);
00201 }
00202 }
00203
00204
00209 public void cancel(Functor funct) {
00210 if (funct == null) {
00211 log(Logger.LOG.ERROR, "Cancel on a null functor");
00212 return;
00213 }
00214 log(Logger.LOG.DEBUG, "cancel: " + funct.asString());
00215 synchronized(this) {
00216 track.cancelled(funct, true);
00217 }
00218 if (local_client != null)
00219 local_client.detask(funct);
00220 for (int i = 0; i < MAX_MOTE_CLIENTS; i++) {
00221 if (motes[i] != null)
00222 motes[i].cancel(funct);
00223 }
00224 synchronized(this) {
00225 track.remove(funct);
00226 }
00227 }
00228
00229
00234 public void dataready(Functor f) {
00235 if (f == null) {
00236 log(Logger.LOG.ERROR, "Dataready on a null functor");
00237 return;
00238 }
00239 Sensory funct = SensoryHelper.narrow(f);
00240 log(Logger.LOG.DEBUG, "dataready: " + funct.asString());
00241 TaskImpl task = null;
00242 synchronized(this) {
00243 task = track.get(funct);
00244 if (task != null && !track.cancelled(funct)) {
00245 track.num_peers(funct, track.num_peers(funct) - 1);
00246 if (track.num_peers(funct) <= 0) {
00247 if (task.callback() != null)
00248 task.callback().aggregate(funct);
00249 track.remove(funct);
00250 }
00251 }
00252 }
00253 }
00254
00255
00256 public static void main(String[] args)
00257 {
00258 SenseUtil su = new SenseUtil(appname, "sensix.jar", args);
00259 SenseServer server = null;
00260 try {
00261 server = new SenseServer(su.id, su.level, su.corba_args,
00262 (Properties)null, appname,
00263 su.debug_level, su.simulator);
00264 }
00265 catch (DiscoveryException e) {
00266 System.err.println(SenseCtrl.appname +
00267 ": Fatal exception accessing the SENSIX Discovery service: ");
00268 e.printStackTrace(System.err);
00269 System.exit(1);
00270 }
00271 catch (org.omg.CORBA.SystemException e) {
00272 System.err.println(SenseCtrl.appname +
00273 ": Fatal CORBA system exception: ");
00274 e.printStackTrace(System.err);
00275 System.exit(1);
00276 }
00277 catch (org.omg.CORBA.UserException e) {
00278 System.err.println(SenseCtrl.appname +
00279 ": Fatal CORBA user exception: ");
00280 e.printStackTrace(System.err);
00281 System.exit(1);
00282 }
00283 catch (Exception e) {
00284 System.err.println(SenseCtrl.appname + ": Unhandled exception: ");
00285 e.printStackTrace(System.err);
00286 System.exit(1);
00287 }
00288
00289 server.log(Logger.LOG.INFO, "daemon started - node ID " + su.id +
00290 " at level " + su.level);
00291 }
00292 }