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
|
#include <RobotRaconteur.h>
#include "robotraconteur_generated.h"
using namespace RobotRaconteur;
using namespace std;
using namespace com::robotraconteur::testing::TestService1;
using namespace com::robotraconteur::testing::TestService2;
using namespace com::robotraconteur::testing::TestService3;
// TODO: Use GTest
void servicesubscription_connected(const RR_SHARED_PTR<ServiceSubscription>& sub,
const ServiceSubscriptionClientID& noden, const RR_SHARED_PTR<RRObject>& obj)
{
cout << "Subscription Connected: " << noden.NodeID.ToString() << ", " << noden.ServiceName << endl;
}
void servicesubscription_disconnected(const RR_SHARED_PTR<ServiceSubscription>& sub,
const ServiceSubscriptionClientID& noden, const RR_SHARED_PTR<RRObject>& obj)
{
cout << "Subscription Disconnected: " << noden.NodeID.ToString() << ", " << noden.ServiceName << endl;
}
bool subscribertest_filter_predicate(const ServiceInfo2& node)
{
if (node.NodeName == "testprog")
{
return true;
}
return false;
}
int main(int argc, char* argv[])
{
if (argc < 2)
{
cout << "Usage for subscriberfiltertest: subscriberfiltertest servicetype" << endl;
return -1;
}
string servicetype = string(argv[1]);
// vector<string> schemes;
// boost::split(schemes, argv[3], boost::is_from_range(',', ','));
RobotRaconteurNode::s()->SetLogLevelFromEnvVariable();
RR_SHARED_PTR<ServiceSubscriptionFilter> f = RR_MAKE_SHARED<ServiceSubscriptionFilter>();
if (argc >= 3)
{
std::string subcommand = string(argv[2]);
if (subcommand == "nodeid")
{
if (argc < 4)
{
cout << "Usage for subscriberfiltertest: subscriberfiltertest nodeid <nodeid>" << endl;
return -1;
}
RR_SHARED_PTR<ServiceSubscriptionFilterNode> n = RR_MAKE_SHARED<ServiceSubscriptionFilterNode>();
n->NodeID = NodeID(std::string(argv[4]));
f->Nodes.push_back(n);
}
else if (subcommand == "nodename")
{
if (argc < 4)
{
cout << "Usage for subscriberfiltertest: subscriberfiltertest nodename "
"<nodename>"
<< endl;
return -1;
}
RR_SHARED_PTR<ServiceSubscriptionFilterNode> n = RR_MAKE_SHARED<ServiceSubscriptionFilterNode>();
n->NodeName = std::string(argv[3]);
f->Nodes.push_back(n);
}
else if (subcommand == "nodeidscheme")
{
if (argc < 5)
{
cout << "Usage for subscriberfiltertest: subscriberfiltertest nodeidscheme "
"<nodeid> <schemes>"
<< endl;
return -1;
}
RR_SHARED_PTR<ServiceSubscriptionFilterNode> n = RR_MAKE_SHARED<ServiceSubscriptionFilterNode>();
n->NodeID = NodeID(std::string(argv[3]));
f->Nodes.push_back(n);
std::vector<std::string> schemes;
std::string schemes1(argv[4]);
boost::split(schemes, schemes1, boost::is_any_of(","));
f->TransportSchemes = schemes;
}
else if (subcommand == "nodeidauth")
{
if (argc < 6)
{
cout << "Usage for subscriberfiltertest: subscriberfiltertest nodeidauth "
"<nodeid> <username> <password>"
<< endl;
return -1;
}
RR_SHARED_PTR<ServiceSubscriptionFilterNode> n = RR_MAKE_SHARED<ServiceSubscriptionFilterNode>();
n->NodeID = NodeID(std::string(argv[3]));
n->Username = std::string(argv[4]);
RR_INTRUSIVE_PTR<RRMap<std::string, RRValue> > cred = AllocateEmptyRRMap<std::string, RRValue>();
cred->insert(std::make_pair("password", stringToRRArray(argv[5])));
n->Credentials = cred;
f->Nodes.push_back(n);
}
else if (subcommand == "servicename")
{
if (argc < 4)
{
cout << "Usage for subscriberfiltertest: subscriberfiltertest servicename "
"<servicename>"
<< endl;
return -1;
}
f->ServiceNames.push_back(argv[3]);
}
if (subcommand == "predicate")
{
if (argc < 3)
{
cout << "Usage for subscriberfiltertest: subscriberfiltertest predicate" << endl;
return -1;
}
f->Predicate = subscribertest_filter_predicate;
}
else
{
cout << "Unknown subscriberfiltertest command" << endl;
return -1;
}
}
RR_SHARED_PTR<TcpTransport> t = RR_MAKE_SHARED<TcpTransport>();
t->EnableNodeDiscoveryListening();
RobotRaconteurNode::s()->RegisterTransport(t);
RR_SHARED_PTR<LocalTransport> c2 = RR_MAKE_SHARED<LocalTransport>();
c2->EnableNodeDiscoveryListening();
RobotRaconteurNode::s()->RegisterTransport(c2);
RR_SHARED_PTR<HardwareTransport> c5 = RR_MAKE_SHARED<HardwareTransport>();
RobotRaconteurNode::s()->RegisterTransport(c5);
RobotRaconteurNode::s()->RegisterServiceType(RR_MAKE_SHARED<com__robotraconteur__testing__TestService1Factory>());
RobotRaconteurNode::s()->RegisterServiceType(RR_MAKE_SHARED<com__robotraconteur__testing__TestService2Factory>());
std::vector<std::string> servicetypes;
servicetypes.push_back(servicetype);
RR_SHARED_PTR<ServiceSubscription> subscription = RobotRaconteurNode::s()->SubscribeServiceByType(servicetypes, f);
subscription->AddClientConnectListener(servicesubscription_connected);
subscription->AddClientDisconnectListener(servicesubscription_disconnected);
std::map<ServiceSubscriptionClientID, RR_SHARED_PTR<RRObject> > clients = subscription->GetConnectedClients();
typedef std::map<ServiceSubscriptionClientID, RR_SHARED_PTR<RRObject> >::value_type e_type;
BOOST_FOREACH (e_type& e, clients)
{
cout << "Subscribed Node: " << e.first.NodeID.ToString() << " " << e.first.ServiceName << endl;
}
cout << "Press enter to quit" << endl;
getchar();
subscription->Close();
RobotRaconteurNode::s()->Shutdown();
return 0;
}
|