SystemStateResultFactory.java
Go to the documentation of this file.
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.response;
00018 
00019 import com.google.common.collect.Maps;
00020 import com.google.common.collect.Sets;
00021 
00022 import org.ros.master.client.SystemState;
00023 import org.ros.master.client.TopicSystemState;
00024 
00025 import java.util.Arrays;
00026 import java.util.HashSet;
00027 import java.util.Map;
00028 import java.util.Map.Entry;
00029 import java.util.Set;
00030 
00037 public class SystemStateResultFactory implements ResultFactory<SystemState> {
00038 
00039         @Override
00040         public SystemState newFromValue(Object value) {
00041                 Object[] vals = (Object[]) value;
00042 
00043                 Map<String, Set<String>> publisherMap = getPublishers(vals[0]);
00044                 Map<String, Set<String>> subscriberMap = getSubscribers(vals[1]);
00045 
00046                 Map<String, TopicSystemState> topics = Maps.newHashMap();
00047 
00048                 for (Entry<String, Set<String>> publisherData : publisherMap.entrySet()) {
00049                         String topicName = publisherData.getKey();
00050 
00051                         Set<String> subscriberNodes = subscriberMap.remove(topicName);
00052 
00053                         // Return empty lists if no subscribers
00054                         if (subscriberNodes == null) {
00055                                 subscriberNodes = Sets.newHashSet();
00056                         }
00057 
00058                         topics.put(topicName,
00059                                         new TopicSystemState(topicName, publisherData.getValue(),
00060                                                         subscriberNodes));
00061                 }
00062 
00063                 for (Entry<String, Set<String>> subscriberData : subscriberMap
00064                                 .entrySet()) {
00065                         // At this point there are no publishers with the same topic name
00066                         HashSet<String> noPublishers = Sets.newHashSet();
00067                         String topicName = subscriberData.getKey();
00068                         topics.put(topicName, new TopicSystemState(topicName,
00069                                         noPublishers, subscriberData.getValue()));
00070                 }
00071 
00072                 // TODO(keith): Get service state in here.
00073 
00074                 return new SystemState(topics.values());
00075         }
00076 
00086         private Map<String, Set<String>> getPublishers(Object pubPairs) {
00087                 Map<String, Set<String>> topicToPublishers = Maps.newHashMap();
00088 
00089                 for (Object topicData : Arrays.asList((Object[]) pubPairs)) {
00090                         String topicName = (String) ((Object[]) topicData)[0];
00091 
00092                         Set<String> publishers =Sets.newHashSet();
00093                         Object[] publisherData = (Object[])((Object[]) topicData)[1];
00094                         for (Object publisher : publisherData) {
00095                                 publishers.add(publisher.toString());
00096                         }
00097 
00098                         topicToPublishers.put(topicName, publishers);
00099                 }
00100 
00101                 return topicToPublishers;
00102         }
00103 
00114         private Map<String, Set<String>> getSubscribers(Object subPairs) {
00115                 Map<String, Set<String>> topicToSubscribers = Maps.newHashMap();
00116 
00117                 for (Object topicData : Arrays.asList((Object[]) subPairs)) {
00118                         String topicName = (String) ((Object[]) topicData)[0];
00119 
00120                         Set<String> subscribers =Sets.newHashSet();
00121                         Object[] subscriberData = (Object[])((Object[]) topicData)[1];
00122                         for (Object subscriber : subscriberData) {
00123                                 subscribers.add(subscriber.toString());
00124                         }
00125 
00126                         topicToSubscribers.put(topicName, subscribers);
00127                 }
00128 
00129                 return topicToSubscribers;
00130         }
00131 }


rosjava_core
Author(s):
autogenerated on Wed Aug 26 2015 16:06:49