00001 /* 00002 * Copyright (C) 2012 Google Inc. 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 00005 * use this file except in compliance with the License. You may obtain a copy of 00006 * the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 00012 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 00013 * License for the specific language governing permissions and limitations under 00014 * the License. 00015 */ 00016 00017 package org.ros.internal.node.server.master; 00018 00019 import com.google.common.base.Preconditions; 00020 import com.google.common.collect.ImmutableSet; 00021 import com.google.common.collect.Sets; 00022 00023 import org.ros.master.client.TopicSystemState; 00024 import org.ros.namespace.GraphName; 00025 import org.ros.node.topic.Subscriber; 00026 00027 import java.util.Set; 00028 00034 public class TopicRegistrationInfo { 00035 00039 private final GraphName topicName; 00040 00047 private String messageType; 00048 00052 private boolean isPublisherDefinedMessageType; 00053 00057 private final Set<NodeRegistrationInfo> publishers; 00058 00062 private final Set<NodeRegistrationInfo> subscribers; 00063 00064 public TopicRegistrationInfo(GraphName topicName) { 00065 this.topicName = topicName; 00066 publishers = Sets.newHashSet(); 00067 subscribers = Sets.newHashSet(); 00068 isPublisherDefinedMessageType = false; 00069 } 00070 00074 public GraphName getTopicName() { 00075 return topicName; 00076 } 00077 00083 public String getMessageType() { 00084 return messageType; 00085 } 00086 00092 public boolean hasPublishers() { 00093 return !publishers.isEmpty(); 00094 } 00095 00101 public boolean hasSubscribers() { 00102 return !subscribers.isEmpty(); 00103 } 00104 00110 public boolean hasRegistrations() { 00111 return hasPublishers() || hasSubscribers(); 00112 } 00113 00119 public Set<NodeRegistrationInfo> getPublishers() { 00120 return ImmutableSet.copyOf(publishers); 00121 } 00122 00131 public void addPublisher(NodeRegistrationInfo publisher, String messageType) { 00132 Preconditions.checkNotNull(publisher); 00133 00134 publishers.add(publisher); 00135 setMessageType(messageType, true); 00136 } 00137 00146 public boolean removePublisher(NodeRegistrationInfo publisher) { 00147 return publishers.remove(publisher); 00148 } 00149 00155 public Set<NodeRegistrationInfo> getSubscribers() { 00156 return ImmutableSet.copyOf(subscribers); 00157 } 00158 00167 public void addSubscriber(NodeRegistrationInfo subscriber, String messageType) { 00168 Preconditions.checkNotNull(subscriber); 00169 subscribers.add(subscriber); 00170 00171 setMessageType(messageType, false); 00172 } 00173 00182 public boolean removeSubscriber(NodeRegistrationInfo subscriber) { 00183 return subscribers.remove(subscriber); 00184 } 00185 00197 private void setMessageType(String topicMessageType, boolean isPublisher) { 00198 // The most recent association of topic name to message type wins. 00199 // However, subscription associations are always trumped by publisher 00200 // associations. 00201 if (isPublisher) { 00202 // Publishers always trump. 00203 messageType = topicMessageType; 00204 isPublisherDefinedMessageType = true; 00205 } else { 00206 // Is a subscriber. 00207 if (!Subscriber.TOPIC_MESSAGE_TYPE_WILDCARD.equals(topicMessageType)) { 00208 if (messageType != null) { 00209 // if has only been subscribers giving the type, register it. 00210 if (!isPublisherDefinedMessageType) { 00211 messageType = topicMessageType; 00212 } 00213 } else { 00214 // Not registered yet, so no worry about trumping. 00215 messageType = topicMessageType; 00216 } 00217 } 00218 } 00219 } 00220 00221 @Override 00222 public int hashCode() { 00223 final int prime = 31; 00224 int result = 1; 00225 result = prime * result + topicName.hashCode(); 00226 return result; 00227 } 00228 00229 @Override 00230 public boolean equals(Object obj) { 00231 if (this == obj) 00232 return true; 00233 if (obj == null) 00234 return false; 00235 if (getClass() != obj.getClass()) 00236 return false; 00237 TopicRegistrationInfo other = (TopicRegistrationInfo) obj; 00238 if (!topicName.equals(other.topicName)) 00239 return false; 00240 return true; 00241 } 00242 }