forked from robotology/yarp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcallback.cpp
86 lines (70 loc) · 1.5 KB
/
callback.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
/*
* Copyright: (C) 2010 RobotCub Consortium
* Authors: Lorenzo Natale
* CopyPolicy: Released under the terms of the LGPLv2.1 or later, see LGPL.TXT
*/
#include <stdio.h>
#include <yarp/os/Network.h>
#include <yarp/os/Semaphore.h>
#include <yarp/os/Property.h>
#include <yarp/os/Thread.h>
#include <yarp/os/BufferedPort.h>
#include <yarp/os/Time.h>
using namespace yarp::os;
class Callback:public BufferedPort<Bottle>
{
private:
Semaphore mutex;
Bottle Datum;
public:
Callback()
{
Datum.clear();
Datum.add("null bottle");
}
void onRead(Bottle &v)
{
mutex.wait();
Datum=v;
//Time::delay(5);
mutex.post();
fprintf(stderr, "Callback got: %s\n",Datum.toString().c_str());
}
void lock()
{
mutex.wait();
}
void unlock()
{
mutex.post();
}
Bottle get()
{
return Datum;
}
};
int main(int argc, char **argv)
{
Network yarp;
Property parameters;
parameters.fromCommand(argc, argv);
Callback cback;
cback.open("/callback");
cback.useCallback();
bool done=false;
while(!done)
{
Time::delay(1);
cback.lock();
Bottle b=cback.get();
cback.unlock();
fprintf(stderr, "Main got: %s\n", b.toString().c_str());
if (b.get(0).asString()=="quit")
done=true;
}
fprintf(stderr, "Closing the port...\n");
cback.close();
fprintf(stderr, "done\n");
fprintf(stderr, "Now returning from main\n");
return 0;
}