Project: bml_realizer License: BSD Dependencies:
Used by:
None |
bml_realizer/src/edu/wpi/hri/bml/realizer/BMLComm.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.bml.realizer; 00035 00036 import java.util.ArrayList; 00037 import java.util.List; 00038 00039 import ros.NodeHandle; 00040 import ros.Publisher; 00041 import ros.RosException; 00042 import ros.Subscriber; 00043 import ros.Subscriber.Callback; 00044 import ros.pkg.bml_msgs.msg.BMLEmit; 00045 import ros.pkg.bml_msgs.msg.BMLException; 00046 import ros.pkg.bml_msgs.msg.BMLPerformance; 00047 import ros.pkg.bml_msgs.msg.Behavior; 00048 import ros.pkg.bml_msgs.msg.Flag; 00049 import edu.wpi.hri.bml.behavior.SyncRef; 00050 import edu.wpi.hri.bml.exec.SchedulerException; 00051 import edu.wpi.hri.log.Logger; 00052 import edu.wpi.hri.log.Logger.Colors; 00053 import edu.wpi.hri.log.Logger.LoggerLevel; 00054 00062 public class BMLComm implements Callback<Behavior> { 00063 00064 private final Logger logger; 00065 private final Publisher<BMLPerformance> performing; 00066 private final Publisher<BMLEmit> emit; 00067 private final Publisher<BMLException> exception; 00068 private final Publisher<BMLException> warning; 00069 private final Subscriber<Behavior> endEmit; 00070 private final List<String> finishedIds; 00071 00085 public BMLComm(Logger logger, NodeHandle handle) throws RosException { 00086 this.logger = logger.sub(Colors.BML, "BML"); 00087 this.performing = handle.advertise("bml/performing", 00088 new BMLPerformance(), 10); 00089 this.emit = handle.advertise("bml/emit", new BMLEmit(), 100); 00090 this.endEmit = handle.subscribe("bml/end_emit", new Behavior(), this, 00091 100); 00092 this.exception = handle.advertise("bml/exception", new BMLException(), 00093 100); 00094 this.warning = handle.advertise("bml/warning", new BMLException(), 100); 00095 00096 this.finishedIds = new ArrayList<String>(); 00097 this.logger.debug(LoggerLevel.INIT, "created BML comm..."); 00098 } 00099 00103 public void shutdown() { 00104 this.endEmit.shutdown(); 00105 } 00106 00116 public void publishPerformance(SyncRef ref, byte status) { 00117 this.logger.debug(LoggerLevel.IO, "publishing performance: " + ref); 00118 BMLPerformance msg = new BMLPerformance(); 00119 msg.behavior.id = ref.getID(); 00120 msg.behavior.synchPoint = ref.getPoint().rosValue; 00121 msg.status.value = status; 00122 this.performing.publish(msg); 00123 } 00124 00137 public void publishEmit(SyncRef ref, String event, String body) { 00138 this.logger.debug(LoggerLevel.IO, "publishing emit: " + ref); 00139 BMLEmit msg = new BMLEmit(); 00140 msg.behavior.id = ref.getID(); 00141 msg.behavior.synchPoint = ref.getPoint().rosValue; 00142 msg.event = event; 00143 msg.body = body; 00144 this.emit.publish(msg); 00145 } 00146 00162 public void publishException(SyncRef ref, byte status, String reason, 00163 boolean warning) { 00164 this.logger.debug(LoggerLevel.IO, "publishing exception: " + ref + " [" 00165 + reason + "] warn = [" + warning + "]"); 00166 BMLException msg = new BMLException(); 00167 msg.behavior.id = ref.getID(); 00168 msg.behavior.synchPoint = ref.getPoint().rosValue; 00169 msg.status.value = status; 00170 msg.reason = reason; 00171 00172 if (warning) 00173 this.warning.publish(msg); 00174 else 00175 this.exception.publish(msg); 00176 } 00177 00184 public void publishException(SchedulerException e) { 00185 this.publishException(e.base, Flag.NOT_DONE, e.getMessage(), e.warning); 00186 } 00187 00195 public void waitForEndEmit(String id) { 00196 synchronized (finishedIds) { 00197 int lastSize = finishedIds.size(); 00198 while (!finishedIds.contains(id)) { 00199 try { 00200 finishedIds.wait(1000); 00201 if (finishedIds.size() == lastSize) 00202 return; 00203 else if (finishedIds.contains(id)) { 00204 finishedIds.remove(id); 00205 return; 00206 } else 00207 lastSize = finishedIds.size(); 00208 00209 } catch (InterruptedException e) { 00210 } 00211 } 00212 finishedIds.remove(id); 00213 } 00214 } 00215 00216 @Override 00217 public void call(Behavior behav) { 00218 logger.debug(LoggerLevel.IO, "Finish emit: " + behav.id); 00219 synchronized (finishedIds) { 00220 finishedIds.add(behav.id); 00221 finishedIds.notifyAll(); 00222 } 00223 } 00224 } |