/** * Load springclrfactory.properties from OSGi fragments linked to this bundle.. * * @param cl * @return */ public static Properties load(ClassLoader cl) { Properties nonosgiregistryProps = new Properties(); Enumeration<URL> nonosgiregistryProperties = null; try { nonosgiregistryProperties = cl == null ? ClassLoader.getSystemResources(NONOSGIREGISTRY_PROPERTIES) : cl.getResources(NONOSGIREGISTRY_PROPERTIES); } catch (IOException e) { if (DebugHelper.DEBUG) { DebugHelper.logError(e); } } while (nonosgiregistryProperties.hasMoreElements()) { URL url = nonosgiregistryProperties.nextElement(); if (url != null) { try { nonosgiregistryProps.load(url.openStream()); } catch (IOException e) { if (DebugHelper.DEBUG) { DebugHelper.logError(e); } } } } return nonosgiregistryProps; }
@Override public void handleMessage(Message msg) { switch (msg.what) { case MSG_PUSH_DATA: Bundle bundle = (Bundle) msg.obj; int sensor = bundle.getInt(BUNDLE_SENSOR); SeedNode seed = mSeedNodeMap.get(sensor); if (seed == null) { // throw new UnsupportedOperationException("No SeedNode for SensorType: " + sensor); DebugHelper.log(TAG, "No SeedNode for SensorType: " + sensor); return; } int deviceID = bundle.getInt(BUNDLE_DEVICE_ID); if (seed.getAttachedDevice() != mDeviceMap.get(deviceID)) { DebugHelper.log( TAG, "Unmatched seed node and attached device(sensor: " + sensor + ", attempted device ID: " + deviceID); return; } String type = bundle.getString(BUNDLE_TYPE); int length = bundle.getInt(BUNDLE_LENGTH); String name = SensorType.getSensorName(sensor); long timestamp = bundle.getLong(BUNDLE_TIMESTAMP); // show notification showNotification(sensor, name); if (type.equals("double[]")) { seed.input(name, type, bundle.getDoubleArray(BUNDLE_DATA), length, timestamp); } else if (type.equals("double")) { seed.input(name, type, bundle.getDouble(BUNDLE_DATA), length, timestamp); } else if (type.equals("int[]")) { seed.input(name, type, bundle.getIntArray(BUNDLE_DATA), length, timestamp); } else if (type.equals("int")) { seed.input(name, type, bundle.getInt(BUNDLE_DATA), length, timestamp); } else if (type.equals("String")) { seed.input(name, type, bundle.getString(BUNDLE_DATA), length, timestamp); } else { throw new IllegalArgumentException("Unknown data_type: " + type); } break; default: super.handleMessage(msg); break; } }
@Override public void onCreate() { super.onCreate(); DebugHelper.logi(TAG, "Service creating.."); INSTANCE = this; mNotification = new NotificationHelper(this, TAG, this.getClass().getName(), R.drawable.ic_launcher); mNotification.showNotificationNow("FlowEngine starting.."); DebugHelper.startTrace(); }
@Override public void addSensor(int deviceID, int sensor, int sampleInterval) throws RemoteException { synchronized (mDeviceMap) { synchronized (mSeedNodeMap) { Device device = mDeviceMap.get(deviceID); device.addSensor(sensor, sampleInterval); SeedNode existingSeed = mSeedNodeMap.get(sensor); if (existingSeed != null) { existingSeed.attachDevice(device); existingSeed.startSensor(); } else { SeedNode seed = new SeedNode(SensorType.getSensorName(sensor), sensor, device); mSeedNodeMap.put(sensor, seed); mNodeNameMap.put("|" + SensorType.getSensorName(sensor), seed); } DebugHelper.log(TAG, "Added sensor type: " + sensor); /*for (Map.Entry<Integer, SeedNode> entry: mSeedNodeMap.entrySet()) { sensor = entry.getKey(); SeedNode node = entry.getValue(); DebugHelper.log(TAG, node.getClass().getName() + ": " + sensor + " device: " + node.getAttachedDevice()); }*/ } } }
@Override public void onDestroy() { DebugHelper.logi(TAG, "Service destroying.."); DebugHelper.stopTrace(); mNotification.showNotificationNow("FlowEngine destryong.."); for (Map.Entry<Integer, Device> entry : mDeviceMap.entrySet()) { try { entry.getValue().getInterface().kill(); } catch (RemoteException e) { e.printStackTrace(); } } super.onDestroy(); }
@Override public IBinder onBind(Intent intent) { DebugHelper.log(TAG, "Bound by intent " + intent); if (FlowEngine.class.getName().equals(intent.getAction())) { return mDeviceAPI; } else if (intent.getAction().equals("edu.ucla.nesl.flowengine.FlowEngine.application")) { return mApplicationAPI; } else { return null; } }
@Override public int addDevice(DeviceAPI deviceAPI) throws RemoteException { synchronized (mDeviceMap) { Device device = new Device(deviceAPI); int deviceID = mNextDeviceID; mNextDeviceID += 1; mDeviceMap.put(deviceID, device); mNotification.showNotificationNow("Added device ID " + deviceID); DebugHelper.log(TAG, "Added device ID: " + deviceID); return deviceID; } }
@Override public int register(ApplicationInterface appInterface) throws RemoteException { synchronized (mApplicationMap) { Application app = new Application(appInterface); int appID = mNextApplicationID; mNextApplicationID += 1; mApplicationMap.put(appID, app); DebugHelper.log(TAG, "Registered application ID " + appID); mNotification.showNotificationNow("Registered application ID: " + appID); return appID; } }
public void run() { while (isRunning) { if (Node.heartbeatTimeout < System.currentTimeMillis() - lastHeartbeat) { DebugHelper.writeDebug( "Detected crash from " + locationData.getNum() + " (heartbeat)", true, locationData.toString()); // if was leader, elect a new one if (locationData.isLeader()) listener.electNewLeader(); lastHeartbeat = System.currentTimeMillis(); } yield(); // so the while loop doesn't spin too much } }
@Override public void removeDevice(int deviceID) throws RemoteException { synchronized (mDeviceMap) { synchronized (mSeedNodeMap) { Device removedDevice = mDeviceMap.remove(deviceID); if (removedDevice != null) { Sensor[] sensors = removedDevice.getSensorList(); for (Sensor sensor : sensors) { SeedNode seed = mSeedNodeMap.get(sensor.getSensorID()); if (seed.isConnected()) { seed.detachDevice(); } else { mSeedNodeMap.remove(sensor.getSensorID()); mNodeNameMap.remove("|" + SensorType.getSensorName(sensor.getSensorID())); } } DebugHelper.log(TAG, "Removed device ID: " + deviceID); mNotification.showNotificationNow("Removed device ID " + deviceID); } } } // print seed map DebugHelper.log(TAG, "Printing mSeedNodeMap.."); for (Map.Entry<Integer, SeedNode> entry : mSeedNodeMap.entrySet()) { int sensor = entry.getKey(); SeedNode node1 = entry.getValue(); DebugHelper.log( TAG, node1.getClass().getName() + ": " + sensor + " device: " + node1.getAttachedDevice()); } DebugHelper.log(TAG, "Done."); // print node name map DebugHelper.log(TAG, "Printing mNodeNameMap.."); for (Map.Entry<String, DataFlowNode> entry : mNodeNameMap.entrySet()) { String nodeName = entry.getKey(); DataFlowNode node2 = entry.getValue(); DebugHelper.log(TAG, nodeName + ": " + node2.getClass().getName()); } DebugHelper.log(TAG, "Done."); }