Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>That's a lot of code so I have to confess I didn't read it through, but from what I have skimmed, I think you could take this approach.</p> <p>a) Treat each devices as "actors" i.e. have a single thread which interacts with the actual devices. This thread will read from the devices, and whenever there is an update, submit it to the "manager-service". You may find <a href="http://guava-libraries.googlecode.com/svn-history/r45/trunk/javadoc/com/google/common/util/concurrent/AbstractService.html" rel="nofollow">AbstractService</a> and <a href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/ScheduledExecutorService.html" rel="nofollow">ExecutorService.newSingleThreadScheduledExecutor()</a> useful for this. (<code>AbstractService</code> gives you a neat interface on start/stopping device. It'll be excellent for your requirements). Let's call these "device-service".</p> <p>b) Have a "manager-service", which keeps a set of "device-service"s in a collection. "manager-service" should be again an actor, having its own threads (one thread sounds fine from what I read). The executor service in the manager-service will take from its queue the updates from device-services and does whatever necessary (send it, etc.). This queue is populated by the device-services and polled by the manager-service (classic producer/consumer pattern). </p> <p>So, like this:</p> <pre><code>class Manager extends AbstractService { final ConcurrentMap&lt;Device, DeviceService&gt; deviceServices = // blah final ExecutorService exec = // single thread exec //called by API user newDevice(Device device){ DeviceService service = new DeviceService(this,device); deviceServices.put(device,service); service.start(); } //called only by device-services void notifyUpdate(final Device device, final Data data){ Runnable whatever = new Runnable(){ public void run(){ sendStuff.(device.name(), data); } } exec.execute(whatever); } // blah blah blah blah.... // called by API user public void stop(Device device){ deviceServices.get(device).stop(); } </code></pre> <p>and</p> <pre><code>class DeviceService extends AbstractService { final Manager mng; final Device device; final ScheduledExecutorService poller = // single thread DeviceService(Manager mng, Device device){ //blah } // schedule a task that polls device and calls "notifyUpdate" of mng // Whenever there is an update. void start(){ poller.scheduleWithFixedDelay( // poll device, and then call mng.notifyUpdate(device, data) // blah blah blah blah.... </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload