19 #ifndef INCLUDE_RCF_PUBLISHINGSERVICE_HPP
20 #define INCLUDE_RCF_PUBLISHINGSERVICE_HPP
25 #include <boost/shared_ptr.hpp>
27 #include <RCF/ClientStub.hpp>
28 #include <RCF/Export.hpp>
29 #include <RCF/GetInterfaceName.hpp>
30 #include <RCF/PeriodicTimer.hpp>
31 #include <RCF/Service.hpp>
32 #include <RCF/ThreadLibrary.hpp>
33 #include <RCF/Timer.hpp>
40 class ClientTransport;
41 typedef boost::shared_ptr<I_RcfClient> RcfClientPtr;
42 typedef boost::shared_ptr<ClientTransport> ClientTransportPtr;
45 typedef boost::shared_ptr<PublisherBase> PublisherPtr;
46 typedef boost::weak_ptr<PublisherBase> PublisherWeakPtr;
48 class PublishingService;
50 typedef boost::function2<bool, RcfSession &, const std::string &> OnSubscriberConnect;
51 typedef boost::function2<void, RcfSession &, const std::string &> OnSubscriberDisconnect;
53 class RCF_EXPORT PublisherParms
57 void setTopicName(
const std::string & topicName);
58 std::string getTopicName()
const;
59 void setOnSubscriberConnect(OnSubscriberConnect onSubscriberConnect);
60 void setOnSubscriberDisconnect(OnSubscriberDisconnect onSubscriberDisconnect);
64 friend class PublishingService;
66 std::string mTopicName;
67 OnSubscriberConnect mOnSubscriberConnect;
68 OnSubscriberDisconnect mOnSubscriberDisconnect;
72 class RCF_EXPORT PublisherBase : boost::noncopyable
75 PublisherBase(PublishingService & pubService,
const PublisherParms & parms);
78 std::string getTopicName();
83 friend class PublishingService;
87 PublishingService & mPublishingService;
88 PublisherParms mParms;
90 std::string mTopicName;
91 RcfClientPtr mRcfClientPtr;
95 template<
typename Interface>
96 class Publisher :
public PublisherBase
100 typedef typename Interface::RcfClientT RcfClientT;
102 Publisher(PublishingService & pubService,
const PublisherParms & parms) :
103 PublisherBase(pubService, parms),
106 if (mTopicName.empty())
108 mTopicName = getInterfaceName<Interface>();
111 mRcfClientPtr.reset(
new RcfClientT( ClientStub(mTopicName) ) );
112 mpClient =
dynamic_cast<RcfClientT *
>(mRcfClientPtr.get());
117 RcfClientT & publish()
119 RCF_ASSERT(!mClosed);
125 RcfClientT * mpClient;
128 class I_RequestSubscription;
129 template<
typename T>
class RcfClient;
131 class RCF_EXPORT PublishingService :
138 ~PublishingService();
140 template<
typename Interface>
141 boost::shared_ptr< Publisher<Interface> > createPublisher(
142 const PublisherParms & parms)
144 boost::shared_ptr< Publisher<Interface> > publisherPtr(
145 new Publisher<Interface>(*
this, parms) );
147 std::string topicName = publisherPtr->getTopicName();
149 RCF_ASSERT(topicName.size() > 0);
150 Lock lock(mPublishersMutex);
151 mPublishers[topicName] = publisherPtr;
155 void setPingIntervalMs(boost::uint32_t pingIntervalMs);
156 boost::uint32_t getPingIntervalMs()
const;
161 friend class RcfClient<I_RequestSubscription>;
162 friend class PublisherBase;
163 friend class PublishingServicePb;
165 boost::int32_t RequestSubscription(
166 const std::string &subscriptionName);
168 boost::int32_t RequestSubscription(
169 const std::string &subscriptionName,
170 boost::uint32_t subToPubPingIntervalMs,
171 boost::uint32_t & pubToSubPingIntervalMs);
175 void onServiceAdded(RcfServer &server);
176 void onServiceRemoved(RcfServer &server);
177 void onServerStart(RcfServer &server);
178 void onServerStop(RcfServer &server);
180 void addSubscriberTransport(
182 const std::string &publisherName,
183 ClientTransportAutoPtrPtr clientTransportAutoPtrPtr);
185 void closePublisher(
const std::string & name);
189 typedef std::map<std::string, PublisherWeakPtr> Publishers;
191 Mutex mPublishersMutex;
192 Publishers mPublishers;
194 boost::uint32_t mPingIntervalMs;
195 PeriodicTimer mPeriodicTimer;
197 virtual void onTimer();
198 void pingAllSubscriptions();
199 void harvestExpiredSubscriptions();
203 typedef boost::shared_ptr<PublishingService> PublishingServicePtr;
207 #endif // ! INCLUDE_RCF_PUBLISHINGSERVICE_HPP