📄 stumblerfunnel.java
字号:
} /** * Adds a spotter to be watched by the StumblerFunnel. Independent Spotters are spotters * for which you want to collect all Measurements they can produce, but for which their updates * don't trigger any further response. */ public void addIndependentSpotter(SpotterExtension spotter) { checkRunning(); independentSpotters.addElement(spotter.getSpotter()); shutdowns.addElement(spotter); spotter.addListener(this); } /** * Remove a SpotterExtension of any type from the StumblerFunnel * @param remove the spotter (and) extension to be removed */ public void removeSpotter(SpotterExtension remove) { checkRunning(); triggerSpotters.removeElement(remove); independentSpotters.removeElement(remove); dependentSpotters.removeElement(remove); shutdowns.removeElement(remove); } private void checkRunning() { if(this.isAlive() && !suspend) { throw new IllegalStateException("You can only modify the state of the StumblerFunnel when it is stopped or suspended"); } } /** * Register to be notified after the pulse with the latest Measurements from all the spotters */ public void addUpdateListener(StumblerFunnelUpdateListener sul) { checkRunning(); updateListeners.addElement(sul); } /** * Register to be notified when the StumblerFunnel has been signaled to shutdown. */ public void addShutdownListener(ShutdownListener listener) { checkRunning(); shutdowns.addElement(listener); } public void removeUpdateListener(StumblerFunnelUpdateListener sul) { checkRunning(); updateListeners.removeElement(sul); } public void removeShutdownListener(ShutdownListener listener) { checkRunning(); shutdowns.removeElement(listener); } long lastPulse = System.currentTimeMillis(); protected void pulse() { if(suspend) return; long now = System.currentTimeMillis(); long diff = now - lastPulse; lastPulse = now; //Logger.println("Time since last pulse: " + diff, Logger.MEDIUM); if(timeoutFired) { long start = System.currentTimeMillis(); timeoutFired = false; Hashtable response = new Hashtable(); Enumeration i = dependentSpotters.elements(); while(i.hasMoreElements()) { SpotterExtension sp = (SpotterExtension)i.nextElement(); Measurement mps; try { mps = sp.getLatestMeasurement(); if(mps != null) response.put(sp.getSpotter(), mps); } catch (SpotterException e1) { e1.printStackTrace(); } } //System.out.println("Time to collect dependent: " + (System.currentTimeMillis() - start)); start = System.currentTimeMillis(); this.notifyUpdate(response); //System.out.println("Time to notify: " + (System.currentTimeMillis() - start)); } else { while(updateSenders.size() > 0) { Spotter updateSender = null; synchronized(updateSenders) { updateSender = (Spotter)updateSenders.elementAt(0); updateSenders.removeElementAt(0); } Measurement updateSenderMeasurement = null; synchronized(updateSenderMeasurements) { updateSenderMeasurement = (Measurement)updateSenderMeasurements.elementAt(0); updateSenderMeasurements.removeElementAt(0); } Hashtable response = new Hashtable(); boolean trigger = false; trigger = triggerSpotters.contains(updateSender); if(trigger) { Enumeration i = dependentSpotters.elements(); while(i.hasMoreElements()) { SpotterExtension sp = (SpotterExtension)i.nextElement(); Measurement mps; try { mps = sp.getLatestMeasurement(); if(mps != null) response.put(sp.getSpotter(), mps); } catch (SpotterException e1) { e1.printStackTrace(); } } } if(updateSender != null && updateSenderMeasurement != null) { response.put(updateSender, updateSenderMeasurement); } notifyUpdate(response); } } } protected void notifyUpdate(final Hashtable response) { for(Enumeration it = updateListeners.elements();it.hasMoreElements();) { final StumblerFunnelUpdateListener l = ((StumblerFunnelUpdateListener)it.nextElement()); if(eventSystem != null) { eventSystem.notifyTransientEvent(new EventListener() { public void callback(Object eventType, Object data) { l.stumblerUpdated(response); } }, null); } else { l.stumblerUpdated(response); } } } public void gotMeasurement(Spotter sender, Measurement measurement) { if(triggerSpotters.contains(sender)) timeout.reset(); // drop all measurements while suspended. if (suspend) return; synchronized(updateSenders) { updateSenders.addElement(sender); } synchronized(updateSenderMeasurements) { updateSenderMeasurements.addElement(measurement); } synchronized(this) { doUpdate = true; this.notify(); } } public void suspendListen() { suspend = true; } public boolean isSuspended () { return suspend; } public void resumeListen() { suspend = false; } public void shutdown() { shuttingDown = true; timeout.shutdown(); synchronized(shutdowns) { Enumeration i = shutdowns.elements(); while(i.hasMoreElements()) { Object elem = i.nextElement(); if (elem instanceof Spotter) { Spotter s = (Spotter) elem; s.stopScanning(); try { s.close(); } catch (SpotterException e1) { e1.printStackTrace(); } } if (elem instanceof ShutdownListener) ((ShutdownListener)elem).shutdown(); } } } protected class Timeout extends Thread { long timeout_in_ms; Object syncObject; boolean timeoutPleaseDie = false; boolean rollover = false; public Timeout(Object syncObject, long timeout_in_ms) { this.timeout_in_ms = timeout_in_ms; this.syncObject = syncObject; } public void run() { if(timeout_in_ms < 0) return; while ( timeoutPleaseDie == false ) { synchronized(this) { try { this.wait(timeout_in_ms); } catch (InterruptedException e) { continue; } } if(rollover) { rollover = false; continue; } synchronized(syncObject) { timeoutFired = true; doUpdate = true; syncObject.notify(); } } timeoutPleaseDie = false; } public void setNewTimeout(int newtimeout_in_ms) { timeout_in_ms = newtimeout_in_ms; } public long getTimeout() { return timeout_in_ms; } public void shutdown() { timeoutPleaseDie = true; } public void reset() { rollover = true; synchronized(this) { this.notify(); } } } /* (non-Javadoc) * @see org.placelab.spotter.SpotterListener#spotterExceptionThrown(org.placelab.spotter.SpotterException) */ public void spotterExceptionThrown(Spotter s,SpotterException ex) { // TODO Auto-generated method stub } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -