Project: bml_lib License: BSD Dependencies: Used by: |
bml_lib/src/edu/wpi/hri/comm/MasterSpinner.javaGo to the documentation of this file.00001 /* 00002 * Software License Agreement (BSD License) 00003 * 00004 * Copyright (c) 2010, Worcester Polytechnic Institute 00005 * All rights reserved. 00006 * 00007 * Redistribution and use in source and binary forms, with or without 00008 * modification, are permitted provided that the following conditions 00009 * are met: 00010 * 00011 * * Redistributions of source code must retain the above copyright 00012 * notice, this list of conditions and the following disclaimer. 00013 * * Redistributions in binary form must reproduce the above 00014 * copyright notice, this list of conditions and the following 00015 * disclaimer in the documentation and/or other materials provided 00016 * with the distribution. 00017 * * Neither the name of Worcester Polytechnic Institute. nor the names 00018 * of its contributors may be used to endorse or promote products 00019 * derived from this software without specific prior written permission. 00020 * 00021 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00022 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00023 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 00024 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 00025 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 00026 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 00027 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00028 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00029 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00030 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 00031 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00032 * POSSIBILITY OF SUCH DAMAGE. 00033 */ 00034 package edu.wpi.hri.comm; 00035 00036 import java.util.ArrayList; 00037 00038 import ros.Ros; 00039 import edu.wpi.hri.log.Logger; 00040 import edu.wpi.hri.log.Logger.LoggerLevel; 00041 00051 public class MasterSpinner { 00052 00053 private Thread master; 00054 private final Logger logger; 00055 private final ArrayList<Thread> oldMasters; 00056 private final Thread endThread; 00057 00066 public MasterSpinner(Logger logger, boolean autoEnd) { 00067 oldMasters = new ArrayList<Thread>(); 00068 this.logger = logger.sub(Logger.Colors.SPINNER, "SPIN"); 00069 this.createMaster(); 00070 00071 // wait for ROS Master to send a bad signal, which means we may have 00072 // more than one thread spinning at a time, not necessarily a bad thing 00073 final MasterSpinner self = this; 00074 this.endThread = new Thread() { 00075 @Override 00076 public void run() { 00077 Ros.getInstance().spin(); 00078 self.stop(); 00079 } 00080 }; 00081 if (autoEnd) 00082 this.endThread.start(); 00083 } 00084 00089 public synchronized void startNewMaster() { 00090 oldMasters.add(master); 00091 this.createMaster(); 00092 } 00093 00097 private void createMaster() { 00098 this.logger.debug(LoggerLevel.MASTER, "Creating a new Master Spinner"); 00099 // a master thread simply spins once until it's no longer the master 00100 master = new Thread() { 00101 @Override 00102 public void run() { 00103 while (this == master) { 00104 // I'm the master :) 00105 Ros.getInstance().spinOnce(); 00106 try { 00107 Thread.sleep(10); 00108 } catch (Exception e) { 00109 } 00110 } 00111 // I'm not the master :( 00112 } 00113 }; 00114 master.start(); 00115 } 00116 00121 public synchronized void joinOldMasters() { 00122 // use a blocking wait until the stop method notifies this one 00123 while (this.master != null) { 00124 try { 00125 this.wait(10000); 00126 if (oldMasters.size() > 0) { 00127 Thread old = oldMasters.get(0); 00128 old.join(1000); 00129 if (!old.isAlive()) { 00130 this.logger.debug(LoggerLevel.MASTER, 00131 "Stopped an old Spinner"); 00132 oldMasters.remove(old); 00133 } 00134 } 00135 } catch (InterruptedException e) { 00136 this.logger.debug(LoggerLevel.ALL, "Interruption: "); 00137 this.logger.printThrowable(LoggerLevel.ALL, e); 00138 } 00139 } 00140 00141 // then join each thread 00142 for (Thread ms : oldMasters) { 00143 try { 00144 ms.join(); 00145 this.logger.debug(LoggerLevel.MASTER, "Stopped an old Spinner"); 00146 } catch (InterruptedException e) { 00147 this.logger.debug(LoggerLevel.ALL, "Interruption: "); 00148 this.logger.printThrowable(LoggerLevel.ALL, e); 00149 } 00150 } 00151 this.oldMasters.clear(); 00152 } 00153 00158 public synchronized void stop() { 00159 this.master = null; 00160 this.notifyAll(); 00161 } 00162 } |