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 sensix;
00036
00037 import java.util.*;
00038 import sensix.sensing.*;
00039
00040
00041 public class SenseServer implements Request, Client
00042 {
00043 public static final String appname = "SENSIX";
00044 public static boolean TINYOS_CLIENT = true;
00045 public static final int MAX_MOTE_CLIENTS = 1;
00046
00047
00048 public SensixNetworking net;
00049 public SensixMarshalling marshal;
00050 public SortedMap<Byte,Capability> local_capabilities;
00051 protected Logger logger;
00052 protected boolean own_net;
00053 protected TaskTracking track;
00054 protected SenseClient local_client;
00055 protected LevelOneClient[] motes;
00056
00057
00058 public SenseServer(int id, int level, byte debug_level, String sim)
00059 throws Exception {
00060 this(id, level, appname, debug_level, sim);
00061 }
00062
00063 public SenseServer(int id, int level, String app,
00064 byte debug_level, String sim)
00065 throws Exception {
00066 logger = new Logger(app, debug_level);
00067
00068 local_client = null;
00069 track = new TaskTracking();
00070 local_capabilities = Collections.synchronizedSortedMap(
00071 new TreeMap<Byte,Capability>());
00072 motes = new LevelOneClient[MAX_MOTE_CLIENTS];
00073 for (int i = 0; i < MAX_MOTE_CLIENTS; i++)
00074 motes[i] = null;
00075
00076 marshal = new SensixMarshalling(id, (byte)level, this,
00077 app, debug_level);
00078 net = new SensixNetworking(marshal, app, debug_level);
00079 own_net = true;
00080
00081 if (level > 1) {
00082 local_client = new SenseClient(net, app, debug_level);
00083 local_client.linkServer(this);
00084 }
00085 if (level == 2) {
00086 if (TINYOS_CLIENT) {
00087 motes[0] = new TinyOSClient(this, app, debug_level, sim);
00088 }
00089
00090 }
00091 }
00092
00093 public void apply(Functor f) {
00094 if (local_client != null)
00095 apply(f, local_client);
00096 }
00097
00098
00099 public void send_up(int what, int size, byte msg[]) {
00100
00101 }
00102
00103 public void bcast(int what, int size, byte msg[]) {
00104
00105 }
00106
00107 public void send_dn(int what, int size, byte msg[]) {
00108 for (int i = 0; i < MAX_MOTE_CLIENTS; i++){
00109 if (motes[i] != null)
00110 motes[i].send_dn(what, size, msg);
00111 }
00112 }
00113
00114
00115 public int identity() {
00116 return net.identifier();
00117 }
00118
00119 public double check_energy() {
00120 return 1.0;
00121 }
00122
00123 public TaskTracking tracker() {
00124 return track;
00125 }
00126
00127 public SortedMap<Byte,Capability> capabilities() {
00128 return local_capabilities;
00129 }
00130
00131
00132 public void log(Logger.LOG type, String msg) {
00133 logger.log(type, msg);
00134 }
00135
00136 public void run() {
00137
00138 }
00139
00140 public void shutdown() {
00141 if (own_net)
00142 net.shutdown();
00143
00144 if (local_client != null)
00145 local_client.shutdown();
00146
00147 for (int i = 0; i < MAX_MOTE_CLIENTS; i++) {
00148 if (motes[i] != null)
00149 motes[i].shutdown();
00150 }
00151
00152 log(Logger.LOG.INFO, "Shutdown");
00153 }
00154
00155
00156
00162 public void addCapability(byte cap, Capability obj) {
00163 try {
00164
00165 synchronized(this) {
00166 local_capabilities.put(new Byte(cap), obj);
00167 }
00168 log(Logger.LOG.DEBUG,
00169 "Added " + SenseUtil.capabilityToString(cap));
00170 } catch (Exception e) {
00171 log(Logger.LOG.ERROR, "Exception while registering " +
00172 "capability: " + e);
00173 }
00174 }
00175
00176
00177
00184 public void apply(Functor funct, Response callback) {
00185 if (funct == null) {
00186 log(Logger.LOG.ERROR, "Apply on a null functor");
00187 return;
00188 }
00189 Task task = new Task(null, funct, callback);
00190 boolean run;
00191
00192 log(Logger.LOG.DEBUG, "apply: " + funct.asString());
00193 synchronized(this) {
00194 track.add(task);
00195 track.num_peers(funct, 1);
00196 if (local_client != null)
00197 local_client.devolve(funct);
00198
00199 for (int i = 0; i < MAX_MOTE_CLIENTS; i++) {
00200 if (motes[i] != null)
00201 motes[i].apply(funct);
00202 }
00203
00204 Sensory f_s = (Sensory)funct;
00205 Byte key = new Byte(f_s.sensor());
00206 if (local_capabilities.containsKey(key))
00207 ((Capability)local_capabilities.get(key)).acquire(funct);
00208
00209 run = (track.num_peers(funct) > 0 &&
00210 track.cancelled(funct) == false);
00211 }
00212
00213 if (local_client == null) {
00214 if (callback != null)
00215 callback.aggregate(funct);
00216 track.remove(funct);
00217 }
00218 }
00219
00220
00225 public void cancel(Functor funct) {
00226 if (funct == null) {
00227 log(Logger.LOG.ERROR, "Cancel on a null functor");
00228 return;
00229 }
00230 log(Logger.LOG.DEBUG, "cancel: " + funct.asString());
00231 synchronized(this) {
00232 track.cancelled(funct, true);
00233 }
00234 if (local_client != null)
00235 local_client.detask(funct);
00236 for (int i = 0; i < MAX_MOTE_CLIENTS; i++) {
00237 if (motes[i] != null)
00238 motes[i].cancel(funct);
00239 }
00240 synchronized(this) {
00241 track.remove(funct);
00242 }
00243 }
00244
00245
00250 public void dataready(Functor f) {
00251 if (f == null) {
00252 log(Logger.LOG.ERROR, "Dataready on a null functor");
00253 return;
00254 }
00255 Sensory funct = (Sensory)f;
00256 log(Logger.LOG.DEBUG, "dataready: " + funct.asString());
00257 Task task = null;
00258 synchronized(this) {
00259 task = track.get(funct);
00260 if (task != null && !track.cancelled(funct)) {
00261 track.num_peers(funct, track.num_peers(funct) - 1);
00262 if (track.num_peers(funct) <= 0) {
00263 if (task.callback() != null)
00264 task.callback().aggregate(funct);
00265 track.remove(funct);
00266 }
00267 }
00268 }
00269 }
00270
00271
00272 public static void main(String[] args)
00273 {
00274 SenseUtil su = new SenseUtil(appname, "sensix.jar", args);
00275 SenseServer server = null;
00276 try {
00277 server = new SenseServer(su.id, su.level,
00278 su.debug_level, su.simulator);
00279 }
00280 catch (Exception e) {
00281 System.err.println(SenseCtrl.appname + ": Unhandled exception: ");
00282 e.printStackTrace(System.err);
00283 System.exit(1);
00284 }
00285
00286 server.log(Logger.LOG.INFO, "daemon started - node ID " + su.id +
00287 " at level " + su.level);
00288 }
00289 }