-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
170 lines (135 loc) · 4.31 KB
/
main.cpp
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
#include <iostream>
#include <stdlib.h>
#include <qi/os.hpp>
#include <boost/shared_ptr.hpp>
#include <alcommon/almodule.h>
#include <alcommon/albroker.h>
#include <alcommon/albrokermanager.h>
#include "NaoRobot/ALBrokerWrapperClass.h"
#include "YarpDev/DeviceManager.h"
#ifdef NaoYARP_IS_REMOTE
# define ALCALL
#else
// when not remote, we're in a dll, so export the entry point
# ifdef _WIN32
# define ALCALL __declspec(dllexport)
# else
# define ALCALL
# endif
#endif
extern "C"
{
ALCALL int _createModule(boost::shared_ptr<AL::ALBroker> broker)
{
// init broker with the main broker instance
// from the parent executable
AL::ALBrokerManager::setInstance(broker->fBrokerManager.lock());
AL::ALBrokerManager::getInstance()->addBroker(broker);
ALBrokerWrapper::Instance().SetBroker ( broker );
//DeviceManager* dev = new DeviceManager;
return 0;
}
ALCALL int _closeModule( )
{
//delete dev;
return 0;
}
} // extern "C"
#ifdef NaoYARP_IS_REMOTE
int main(int argc, char* argv[])
{
// We will try to connect our broker to a running NAOqi
int pport = 9559;
std::string pip = "127.0.0.1";
// command line parse option
// check the number of arguments
if (argc != 1 && argc != 3 && argc != 5)
{
std::cerr << "Wrong number of arguments!" << std::endl;
std::cerr << "Usage: mymodule [--pip robot_ip] [--pport port]" << std::endl;
exit(2);
}
// if there is only one argument it should be IP or PORT
if (argc == 3)
{
if (std::string(argv[1]) == "--pip")
pip = argv[2];
else if (std::string(argv[1]) == "--pport")
pport = atoi(argv[2]);
else
{
std::cerr << "Wrong number of arguments!" << std::endl;
std::cerr << "Usage: mymodule [--pip robot_ip] [--pport port]" << std::endl;
exit(2);
}
}
// Sepcified IP or PORT for the connection
if (argc == 5)
{
if (std::string(argv[1]) == "--pport"
&& std::string(argv[3]) == "--pip")
{
pport = atoi(argv[2]);
pip = argv[4];
}
else if (std::string(argv[3]) == "--pport"
&& std::string(argv[1]) == "--pip")
{
pport = atoi(argv[4]);
pip = argv[2];
}
else
{
std::cerr << "Wrong number of arguments!" << std::endl;
std::cerr << "Usage: mymodule [--pip robot_ip] [--pport port]" << std::endl;
exit(2);
}
}
// Need this to for SOAP serialization of floats to work
setlocale(LC_NUMERIC, "C");
// A broker needs a name, an IP and a port:
const std::string brokerName = "HuSeLNaoBroker";
// FIXME: would be a good idea to look for a free port first
int brokerPort = 54000;
// listen port of the broker (here an anything)
const std::string brokerIp = "0.0.0.0";
// Create your own broker
boost::shared_ptr<AL::ALBroker> broker;
try
{
broker = AL::ALBroker::createBroker(
brokerName,
brokerIp,
brokerPort,
pip,
pport,
0 // you can pass various options for the broker creation,
// but default is fine
);
}
catch(...)
{
std::cerr << "Fail to connect broker to: "
<< pip
<< ":"
<< pport
<< std::endl;
AL::ALBrokerManager::getInstance()->killAllBroker();
AL::ALBrokerManager::kill();
return 1;
}
// Deal with ALBrokerManager singleton (add your borker into NAOqi)
AL::ALBrokerManager::setInstance(broker->fBrokerManager.lock());
AL::ALBrokerManager::getInstance()->addBroker(broker);
// Now it's time to load your module with
// AL::ALModule::createModule<your_module>(<broker_create>, <your_module>);
//AL::ALModule::createModule<MainMod>(broker, "MainMod");
//AL::ALModule::createModule<RPCMod>(broker, "RPCMod");
ALBrokerWrapper::Instance().SetBroker ( broker );
DeviceManager* dev = new DeviceManager;
while (true)
qi::os::sleep(1);
delete dev;
return 0;
}
#endif // NaoYARP_IS_REMOTE