-
Notifications
You must be signed in to change notification settings - Fork 390
Expand file tree
/
Copy pathtopic_manager.cpp
More file actions
1143 lines (962 loc) · 29.7 KB
/
topic_manager.cpp
File metadata and controls
1143 lines (962 loc) · 29.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2009, Willow Garage, Inc.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the names of Willow Garage, Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include "ros/topic_manager.h"
#include "ros/broadcast_manager.h"
#include "ros/xmlrpc_manager.h"
#include "ros/connection_manager.h"
#include "ros/poll_manager.h"
#include "ros/publication.h"
#include "ros/subscription.h"
#include "ros/this_node.h"
#include "ros/network.h"
#include "ros/master.h"
#include "ros/transport/transport_tcp.h"
#include "ros/transport/transport_udp.h"
#include "ros/rosout_appender.h"
#include "ros/init.h"
#include "ros/file_log.h"
#include "ros/subscribe_options.h"
#include "XmlRpc.h"
#include <ros/console.h>
using namespace XmlRpc; // A battle to be fought later
using namespace std; // sigh
/// \todo Locking can be significantly simplified here once the Node API goes away.
namespace ros
{
TopicManagerPtr g_topic_manager;
boost::mutex g_topic_manager_mutex;
const TopicManagerPtr& TopicManager::instance()
{
if (!g_topic_manager)
{
boost::mutex::scoped_lock lock(g_topic_manager_mutex);
if (!g_topic_manager)
{
g_topic_manager = boost::make_shared<TopicManager>();
}
}
return g_topic_manager;
}
TopicManager::TopicManager()
: shutting_down_(false)
{
}
TopicManager::~TopicManager()
{
shutdown();
}
void TopicManager::start()
{
boost::mutex::scoped_lock shutdown_lock(shutting_down_mutex_);
shutting_down_ = false;
poll_manager_ = PollManager::instance();
connection_manager_ = ConnectionManager::instance();
xmlrpc_manager_ = XMLRPCManager::instance();
xmlrpc_manager_->bind("publisherUpdate", boost::bind(&TopicManager::pubUpdateCallback, this, _1, _2));
xmlrpc_manager_->bind("requestTopic", boost::bind(&TopicManager::requestTopicCallback, this, _1, _2));
xmlrpc_manager_->bind("getBusStats", boost::bind(&TopicManager::getBusStatsCallback, this, _1, _2));
xmlrpc_manager_->bind("getBusInfo", boost::bind(&TopicManager::getBusInfoCallback, this, _1, _2));
xmlrpc_manager_->bind("getSubscriptions", boost::bind(&TopicManager::getSubscriptionsCallback, this, _1, _2));
xmlrpc_manager_->bind("getPublications", boost::bind(&TopicManager::getPublicationsCallback, this, _1, _2));
poll_manager_->addPollThreadListener(boost::bind(&TopicManager::processPublishQueues, this));
}
void TopicManager::shutdown()
{
boost::mutex::scoped_lock shutdown_lock(shutting_down_mutex_);
if (shutting_down_)
{
return;
}
{
boost::recursive_mutex::scoped_lock lock1(advertised_topics_mutex_);
boost::mutex::scoped_lock lock2(subs_mutex_);
shutting_down_ = true;
}
xmlrpc_manager_->unbind("publisherUpdate");
xmlrpc_manager_->unbind("requestTopic");
xmlrpc_manager_->unbind("getBusStats");
xmlrpc_manager_->unbind("getBusInfo");
xmlrpc_manager_->unbind("getSubscriptions");
xmlrpc_manager_->unbind("getPublications");
ROSCPP_LOG_DEBUG("Shutting down topics...");
ROSCPP_LOG_DEBUG(" shutting down publishers");
{
boost::recursive_mutex::scoped_lock adv_lock(advertised_topics_mutex_);
for (V_Publication::iterator i = advertised_topics_.begin();
i != advertised_topics_.end(); ++i)
{
if(!(*i)->isDropped())
{
unregisterPublisher((*i)->getName());
}
(*i)->drop();
}
advertised_topics_.clear();
}
// unregister all of our subscriptions
ROSCPP_LOG_DEBUG(" shutting down subscribers");
{
boost::mutex::scoped_lock subs_lock(subs_mutex_);
for (L_Subscription::iterator s = subscriptions_.begin(); s != subscriptions_.end(); ++s)
{
// Remove us as a subscriber from the master
unregisterSubscriber((*s)->getName());
// now, drop our side of the connection
(*s)->shutdown();
}
subscriptions_.clear();
}
}
void TopicManager::checkAndRemoveSHMSegment(std::string topic)
{
if (!ros::ok())
{
return;
}
// Get node info
std::set<std::string> pi = BroadcastManager::instance()->getPubs(topic);
std::set<std::string> si = BroadcastManager::instance()->getSubs(topic);
// Count the num
int count = pi.size() + si.size();
// Remove segment
if (count == 1)
{
sharedmem_transport::SharedMemoryUtil sharedmem_util;
sharedmem_util.remove_segment(topic.c_str());
}
}
void TopicManager::processPublishQueues()
{
boost::recursive_mutex::scoped_lock lock(advertised_topics_mutex_);
V_Publication::iterator it = advertised_topics_.begin();
V_Publication::iterator end = advertised_topics_.end();
for (; it != end; ++it)
{
const PublicationPtr& pub = *it;
pub->processPublishQueue();
}
}
void TopicManager::getAdvertisedTopics(V_string& topics)
{
boost::mutex::scoped_lock lock(advertised_topic_names_mutex_);
topics.resize(advertised_topic_names_.size());
std::copy(advertised_topic_names_.begin(),
advertised_topic_names_.end(),
topics.begin());
}
void TopicManager::getSubscribedTopics(V_string& topics)
{
boost::mutex::scoped_lock lock(subs_mutex_);
topics.reserve(subscriptions_.size());
L_Subscription::const_iterator it = subscriptions_.begin();
L_Subscription::const_iterator end = subscriptions_.end();
for (; it != end; ++it)
{
const SubscriptionPtr& sub = *it;
topics.push_back(sub->getName());
}
}
PublicationPtr TopicManager::lookupPublication(const std::string& topic)
{
boost::recursive_mutex::scoped_lock lock(advertised_topics_mutex_);
return lookupPublicationWithoutLock(topic);
}
SubscriptionPtr TopicManager::lookupSubscription(const std::string& topic)
{
boost::mutex::scoped_lock lock(subs_mutex_);
return lookupSubscriptionWithoutLock(topic);
}
L_Subscription TopicManager::getAllSubscription()
{
boost::mutex::scoped_lock lock(subs_mutex_);
return subscriptions_ ;
}
bool md5sumsMatch(const std::string& lhs, const std::string& rhs)
{
return lhs == "*" || rhs == "*" || lhs == rhs;
}
bool TopicManager::addSubCallback(const SubscribeOptions& ops)
{
// spin through the subscriptions and see if we find a match. if so, use it.
bool found = false;
bool found_topic = false;
SubscriptionPtr sub;
{
if (isShuttingDown())
{
return false;
}
for (L_Subscription::iterator s = subscriptions_.begin();
s != subscriptions_.end() && !found; ++s)
{
sub = *s;
if (!sub->isDropped() && sub->getName() == ops.topic)
{
found_topic = true;
if (md5sumsMatch(ops.md5sum, sub->md5sum()))
{
found = true;
}
break;
}
}
}
if (found_topic && !found)
{
std::stringstream ss;
ss << "Tried to subscribe to a topic with the same name but different md5sum as a topic that was already subscribed [" << ops.datatype << "/" << ops.md5sum << " vs. " << sub->datatype() << "/" << sub->md5sum() << "]";
throw ConflictingSubscriptionException(ss.str());
}
else if (found)
{
if (!sub->addCallback(ops.helper, ops.md5sum, ops.callback_queue, ops.queue_size, ops.tracked_object, ops.allow_concurrent_callbacks))
{
return false;
}
}
return found;
}
// this function has the subscription code that doesn't need to be templated.
bool TopicManager::subscribe(const SubscribeOptions& ops)
{
{
boost::mutex::scoped_lock lock(subs_mutex_);
if (addSubCallback(ops))
{
return true;
}
if (isShuttingDown())
{
return false;
}
if (ops.md5sum.empty())
{
throw InvalidParameterException("Subscribing to topic [" + ops.topic + "] with an empty md5sum");
}
if (ops.datatype.empty())
{
throw InvalidParameterException("Subscribing to topic [" + ops.topic + "] with an empty datatype");
}
if (!ops.helper)
{
throw InvalidParameterException("Subscribing to topic [" + ops.topic + "] without a callback");
}
const std::string& md5sum = ops.md5sum;
std::string datatype = ops.datatype;
SubscriptionPtr s(boost::make_shared<Subscription>(ops.topic, md5sum, datatype, ops.transport_hints));
s->addCallback(ops.helper, ops.md5sum, ops.callback_queue, ops.queue_size, ops.tracked_object, ops.allow_concurrent_callbacks);
if (!registerSubscriber(s, ops.datatype))
{
ROS_WARN("couldn't register subscriber on topic [%s]", ops.topic.c_str());
s->shutdown();
return false;
}
subscriptions_.push_back(s);
}
BroadcastManager::instance()->publisherUpdate(ops.topic);
return true;
}
bool TopicManager::advertise(const AdvertiseOptions& ops, const SubscriberCallbacksPtr& callbacks)
{
if (ops.datatype == "*")
{
std::stringstream ss;
ss << "Advertising with * as the datatype is not allowed. Topic [" << ops.topic << "]";
throw InvalidParameterException(ss.str());
}
if (ops.md5sum == "*")
{
std::stringstream ss;
ss << "Advertising with * as the md5sum is not allowed. Topic [" << ops.topic << "]";
throw InvalidParameterException(ss.str());
}
if (ops.md5sum.empty())
{
throw InvalidParameterException("Advertising on topic [" + ops.topic + "] with an empty md5sum");
}
if (ops.datatype.empty())
{
throw InvalidParameterException("Advertising on topic [" + ops.topic + "] with an empty datatype");
}
if (ops.message_definition.empty())
{
ROS_WARN("Advertising on topic [%s] with an empty message definition. Some tools (e.g. rosbag) may not work correctly.", ops.topic.c_str());
}
PublicationPtr pub;
{
boost::recursive_mutex::scoped_lock lock(advertised_topics_mutex_);
if (isShuttingDown())
{
return false;
}
pub = lookupPublicationWithoutLock(ops.topic);
if (pub && pub->getNumCallbacks() == 0)
{
pub.reset();
}
if (pub)
{
if (pub->getMD5Sum() != ops.md5sum)
{
ROS_ERROR("Tried to advertise on topic [%s] with md5sum [%s] and datatype [%s], but the topic is already advertised as md5sum [%s] and datatype [%s]",
ops.topic.c_str(), ops.md5sum.c_str(), ops.datatype.c_str(), pub->getMD5Sum().c_str(), pub->getDataType().c_str());
return false;
}
pub->addCallbacks(callbacks);
return true;
}
pub = PublicationPtr(boost::make_shared<Publication>(ops.topic, ops.datatype, ops.md5sum, ops.message_definition, ops.queue_size, ops.latch, ops.has_header));
pub->addCallbacks(callbacks);
advertised_topics_.push_back(pub);
}
{
boost::mutex::scoped_lock lock(advertised_topic_names_mutex_);
advertised_topic_names_.push_back(ops.topic);
}
// Check whether we've already subscribed to this topic. If so, we'll do
// the self-subscription here, to avoid the deadlock that would happen if
// the ROS thread later got the publisherUpdate with its own XMLRPC URI.
// The assumption is that advertise() is called from somewhere other
// than the ROS thread.
bool found = false;
SubscriptionPtr sub;
{
boost::mutex::scoped_lock lock(subs_mutex_);
for (L_Subscription::iterator s = subscriptions_.begin();
s != subscriptions_.end() && !found; ++s)
{
if ((*s)->getName() == ops.topic && md5sumsMatch((*s)->md5sum(), ops.md5sum) && !(*s)->isDropped())
{
found = true;
sub = *s;
break;
}
}
}
if(found)
{
sub->addLocalConnection(pub);
}
registerPublisher(ops.topic, ops.datatype);
return true;
}
bool TopicManager::unadvertise(const std::string &topic, const SubscriberCallbacksPtr& callbacks)
{
PublicationPtr pub;
V_Publication::iterator i;
{
boost::recursive_mutex::scoped_lock lock(advertised_topics_mutex_);
if (isShuttingDown())
{
return false;
}
for (i = advertised_topics_.begin();
i != advertised_topics_.end(); ++i)
{
if(((*i)->getName() == topic) && (!(*i)->isDropped()))
{
pub = *i;
break;
}
}
}
if (!pub)
{
return false;
}
pub->removeCallbacks(callbacks);
{
boost::recursive_mutex::scoped_lock lock(advertised_topics_mutex_);
if (pub->getNumCallbacks() == 0)
{
unregisterPublisher(pub->getName());
pub->drop();
advertised_topics_.erase(i);
{
boost::mutex::scoped_lock lock(advertised_topic_names_mutex_);
advertised_topic_names_.remove(pub->getName());
}
}
}
return true;
}
bool TopicManager::unregisterPublisher(const std::string& topic)
{
checkAndRemoveSHMSegment(topic);
BroadcastManager::instance()->unregisterPublisher(topic, xmlrpc_manager_->getServerURI());
return true;
}
bool TopicManager::isTopicAdvertised(const string &topic)
{
for (V_Publication::iterator t = advertised_topics_.begin(); t != advertised_topics_.end(); ++t)
{
if (((*t)->getName() == topic) && (!(*t)->isDropped()))
{
return true;
}
}
return false;
}
bool TopicManager::registerSubscriber(const SubscriptionPtr& s, const string &datatype)
{
BroadcastManager::instance()->registerSubscriber(s->getName(), datatype, xmlrpc_manager_->getServerURI());
#if 0 // add pubs in registerPublisher callback
vector<string> pub_uris;
for (int i = 0; i < payload.size(); i++)
{
if (payload[i] != xmlrpc_manager_->getServerURI())
{
pub_uris.push_back(string(payload[i]));
}
}
#endif
bool self_subscribed = false;
PublicationPtr pub;
const std::string& sub_md5sum = s->md5sum();
// Figure out if we have a local publisher
{
boost::recursive_mutex::scoped_lock lock(advertised_topics_mutex_);
V_Publication::const_iterator it = advertised_topics_.begin();
V_Publication::const_iterator end = advertised_topics_.end();
for (; it != end; ++it)
{
pub = *it;
const std::string& pub_md5sum = pub->getMD5Sum();
if (pub->getName() == s->getName() && !pub->isDropped())
{
if (!md5sumsMatch(pub_md5sum, sub_md5sum))
{
ROS_ERROR("md5sum mismatch making local subscription to topic %s.",
s->getName().c_str());
ROS_ERROR("Subscriber expects type %s, md5sum %s",
s->datatype().c_str(), s->md5sum().c_str());
ROS_ERROR("Publisher provides type %s, md5sum %s",
pub->getDataType().c_str(), pub->getMD5Sum().c_str());
return false;
}
self_subscribed = true;
pub->setSelfPublished(true);
break;
}
}
}
s->setSelfSubscribed(self_subscribed);
//s->pubUpdate(pub_uris);
if (self_subscribed)
{
s->addLocalConnection(pub);
}
return true;
}
bool TopicManager::unregisterSubscriber(const string &topic)
{
checkAndRemoveSHMSegment(topic);
#if 0 // TODO
XmlRpcValue args, result, payload;
args[0] = this_node::getName();
args[1] = topic;
args[2] = xmlrpc_manager_->getServerURI();
master::execute("unregisterSubscriber", args, result, payload, false);
#endif
BroadcastManager::instance()->unregisterSubscriber(topic, xmlrpc_manager_->getServerURI());
return true;
}
bool TopicManager::pubUpdate(const string &topic, const vector<string> &pubs)
{
SubscriptionPtr sub;
{
boost::mutex::scoped_lock lock(subs_mutex_);
if (isShuttingDown())
{
return false;
}
ROS_DEBUG("Received update for topic [%s] (%d publishers)", topic.c_str(), (int)pubs.size());
// find the subscription
for (L_Subscription::const_iterator s = subscriptions_.begin();
s != subscriptions_.end(); ++s)
{
if ((*s)->getName() != topic || (*s)->isDropped())
continue;
sub = *s;
break;
}
}
if (sub)
{
return sub->pubUpdate(pubs);
}
else
{
ROSCPP_LOG_DEBUG("got a request for updating publishers of topic %s, but I " \
"don't have any subscribers to that topic.", topic.c_str());
}
return false;
}
bool TopicManager::requestTopic(const string &topic,
XmlRpcValue &protos,
XmlRpcValue &ret)
{
for (int proto_idx = 0; proto_idx < protos.size(); proto_idx++)
{
XmlRpcValue proto = protos[proto_idx]; // save typing
if (proto.getType() != XmlRpcValue::TypeArray)
{
ROSCPP_LOG_DEBUG( "requestTopic protocol list was not a list of lists");
return false;
}
if (proto[0].getType() != XmlRpcValue::TypeString)
{
ROSCPP_LOG_DEBUG( "requestTopic received a protocol list in which a sublist " \
"did not start with a string");
return false;
}
string proto_name = proto[0];
if (proto_name == string("TCPROS"))
{
XmlRpcValue tcpros_params;
tcpros_params[0] = string("TCPROS");
tcpros_params[1] = network::getHost();
tcpros_params[2] = int(connection_manager_->getTCPPort());
ret[0] = int(1);
ret[1] = string();
ret[2] = tcpros_params;
return true;
}
else if (proto_name == string("UDPROS"))
{
if (proto.size() != 5 ||
proto[1].getType() != XmlRpcValue::TypeBase64 ||
proto[2].getType() != XmlRpcValue::TypeString ||
proto[3].getType() != XmlRpcValue::TypeInt ||
proto[4].getType() != XmlRpcValue::TypeInt)
{
ROSCPP_LOG_DEBUG("Invalid protocol parameters for UDPROS");
return false;
}
std::vector<char> header_bytes = proto[1];
boost::shared_array<uint8_t> buffer(new uint8_t[header_bytes.size()]);
memcpy(buffer.get(), &header_bytes[0], header_bytes.size());
Header h;
string err;
if (!h.parse(buffer, header_bytes.size(), err))
{
ROSCPP_LOG_DEBUG("Unable to parse UDPROS connection header: %s", err.c_str());
return false;
}
PublicationPtr pub_ptr = lookupPublication(topic);
if(!pub_ptr)
{
ROSCPP_LOG_DEBUG("Unable to find advertised topic %s for UDPROS connection", topic.c_str());
return false;
}
std::string host = proto[2];
int port = proto[3];
M_string m;
std::string error_msg;
if (!pub_ptr->validateHeader(h, error_msg))
{
ROSCPP_LOG_DEBUG("Error validating header from [%s:%d] for topic [%s]: %s", host.c_str(), port, topic.c_str(), error_msg.c_str());
return false;
}
int max_datagram_size = proto[4];
int conn_id = connection_manager_->getNewConnectionID();
TransportUDPPtr transport = connection_manager_->getUDPServerTransport()->createOutgoing(host, port, conn_id, max_datagram_size);
if (!transport)
{
ROSCPP_LOG_DEBUG("Error creating outgoing transport for [%s:%d]", host.c_str(), port);
return false;
}
connection_manager_->udprosIncomingConnection(transport, h);
XmlRpcValue udpros_params;
udpros_params[0] = string("UDPROS");
udpros_params[1] = network::getHost();
udpros_params[2] = connection_manager_->getUDPServerTransport()->getServerPort();
udpros_params[3] = conn_id;
udpros_params[4] = max_datagram_size;
m["topic"] = topic;
m["md5sum"] = pub_ptr->getMD5Sum();
m["type"] = pub_ptr->getDataType();
m["callerid"] = this_node::getName();
m["message_definition"] = pub_ptr->getMessageDefinition();
boost::shared_array<uint8_t> msg_def_buffer;
uint32_t len;
Header::write(m, msg_def_buffer, len);
XmlRpcValue v(msg_def_buffer.get(), len);
udpros_params[5] = v;
ret[0] = int(1);
ret[1] = string();
ret[2] = udpros_params;
return true;
}
else
{
ROSCPP_LOG_DEBUG( "an unsupported protocol was offered: [%s]",
proto_name.c_str());
}
}
ROSCPP_LOG_DEBUG( "Currently, roscpp only supports TCPROS. The caller to " \
"requestTopic did not support TCPROS, so there are no " \
"protocols in common.");
return false;
}
void TopicManager::publish(const std::string& topic, const boost::function<SerializedMessage(void)>& serfunc, SerializedMessage& m)
{
boost::recursive_mutex::scoped_lock lock(advertised_topics_mutex_);
if (isShuttingDown())
{
return;
}
PublicationPtr p = lookupPublicationWithoutLock(topic);
if (p->hasSubscribers() || p->isLatching())
{
ROS_DEBUG_NAMED("superdebug", "Publishing message on topic [%s] with sequence number [%d]", p->getName().c_str(), p->getSequence());
// Determine what kinds of subscribers we're publishing to. If they're intraprocess with the same C++ type we can
// do a no-copy publish.
bool nocopy = false;
bool serialize = false;
// We can only do a no-copy publish if a shared_ptr to the message is provided, and we have type information for it
if (m.type_info && m.message)
{
p->getPublishTypes(serialize, nocopy, *m.type_info);
}
else
{
serialize = true;
}
if (!nocopy)
{
m.message.reset();
m.type_info = 0;
}
if (serialize || p->isLatching())
{
SerializedMessage m2 = serfunc();
m.buf = m2.buf;
m.num_bytes = m2.num_bytes;
m.message_start = m2.message_start;
}
p->publish(m);
// If we're not doing a serialized publish we don't need to signal the pollset. The write()
// call inside signal() is actually relatively expensive when doing a nocopy publish.
if (serialize)
{
poll_manager_->getPollSet().signal();
}
}
else
{
p->incrementSequence();
}
}
void TopicManager::incrementSequence(const std::string& topic)
{
PublicationPtr pub = lookupPublication(topic);
if (pub)
{
pub->incrementSequence();
}
}
bool TopicManager::isLatched(const std::string& topic)
{
PublicationPtr pub = lookupPublication(topic);
if (pub)
{
return pub->isLatched();
}
return false;
}
PublicationPtr TopicManager::lookupPublicationWithoutLock(const string &topic)
{
PublicationPtr t;
for (V_Publication::iterator i = advertised_topics_.begin();
!t && i != advertised_topics_.end(); ++i)
{
if (((*i)->getName() == topic) && (!(*i)->isDropped()))
{
t = *i;
break;
}
}
return t;
}
SubscriptionPtr TopicManager::lookupSubscriptionWithoutLock(const string &topic)
{
SubscriptionPtr t;
for (L_Subscription::iterator i = subscriptions_.begin(); i != subscriptions_.end(); ++i)
{
if ((!(*i)->isDropped()) && ((*i)->getName() == topic))
{
t = *i;
break;
}
}
return t;
}
bool TopicManager::unsubscribe(const std::string &topic, const SubscriptionCallbackHelperPtr& helper)
{
SubscriptionPtr sub;
{
boost::mutex::scoped_lock lock(subs_mutex_);
if (isShuttingDown())
{
return false;
}
L_Subscription::iterator it;
for (it = subscriptions_.begin(); it != subscriptions_.end(); ++it)
{
if ((*it)->getName() == topic)
{
sub = *it;
break;
}
}
}
if (!sub)
{
return false;
}
sub->removeCallback(helper);
if (sub->getNumCallbacks() == 0)
{
// nobody is left. blow away the subscription.
{
boost::mutex::scoped_lock lock(subs_mutex_);
L_Subscription::iterator it;
for (it = subscriptions_.begin(); it != subscriptions_.end(); ++it)
{
if ((*it)->getName() == topic)
{
subscriptions_.erase(it);
break;
}
}
if (!unregisterSubscriber(topic))
{
ROSCPP_LOG_DEBUG("Couldn't unregister subscriber for topic [%s]", topic.c_str());
}
}
sub->shutdown();
return true;
}
return true;
}
size_t TopicManager::getNumSubscribers(const std::string &topic)
{
boost::recursive_mutex::scoped_lock lock(advertised_topics_mutex_);
if (isShuttingDown())
{
return 0;
}
PublicationPtr p = lookupPublicationWithoutLock(topic);
if (p)
{
return p->getNumSubscribers();
}
return 0;
}
size_t TopicManager::getNumSubscriptions()
{
boost::mutex::scoped_lock lock(subs_mutex_);
return subscriptions_.size();
}
size_t TopicManager::getNumPublishers(const std::string &topic)
{
boost::mutex::scoped_lock lock(subs_mutex_);
if (isShuttingDown())
{
return 0;
}
for (L_Subscription::const_iterator t = subscriptions_.begin();
t != subscriptions_.end(); ++t)
{
if (!(*t)->isDropped() && (*t)->getName() == topic)
{
return (*t)->getNumPublishers();
}
}
return 0;
}
void TopicManager::getBusStats(XmlRpcValue &stats)
{
XmlRpcValue publish_stats, subscribe_stats, service_stats;
// force these guys to be arrays, even if we don't populate them
publish_stats.setSize(0);
subscribe_stats.setSize(0);
service_stats.setSize(0);
uint32_t pidx = 0;
{
boost::recursive_mutex::scoped_lock lock(advertised_topics_mutex_);
for (V_Publication::iterator t = advertised_topics_.begin();
t != advertised_topics_.end(); ++t)
{
publish_stats[pidx++] = (*t)->getStats();
}
}
{
uint32_t sidx = 0;
boost::mutex::scoped_lock lock(subs_mutex_);
for (L_Subscription::iterator t = subscriptions_.begin(); t != subscriptions_.end(); ++t)
{
subscribe_stats[sidx++] = (*t)->getStats();
}
}
stats[0] = publish_stats;
stats[1] = subscribe_stats;
stats[2] = service_stats;
}
void TopicManager::getBusInfo(XmlRpcValue &info)
{
// force these guys to be arrays, even if we don't populate them
info.setSize(0);
{
boost::recursive_mutex::scoped_lock lock(advertised_topics_mutex_);
for (V_Publication::iterator t = advertised_topics_.begin();
t != advertised_topics_.end(); ++t)
{
(*t)->getInfo(info);