fertesta (owner)

Revisions

gist: 217185 Download_button fork
public
Description:
async calls with ACE
Public Clone URL: git://gist.github.com/217185.git
Embed All Files: show embed
ace_async_call.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
/*
This code shows how to perform a async/wait-for-result method, with ACE Activation Queues.
Works on windows.
*/
 
#include <stdio.h>
#include <ace/Task.h>
#include <ace/Activation_Queue.h>
#include <ace/Method_Request.h>
#include <ace/Future.h>
#include <iostream>
#include <string>
 
using namespace std;
 
class Executor;
 
typedef ACE_Future<int> future_result;
 
struct MethodProxy : public ACE_Method_Request
{
//friend Executor;
int call() { return 0; }
future_result * future_;
public:
MethodProxy() : future_(NULL) {}
virtual int call(Executor*)=0;
};
 
struct Play : public MethodProxy
{
string filename_;
Play(string filename, future_result * f=NULL) : filename_(filename) {
future_=f;
}
int call(Executor * thiz_) {
cout << "Playing " << filename_ << endl;
Sleep(1000);
return 666;
}
};
 
 
class Executor : public ACE_Task_Base
{
ACE_Activation_Queue queue_;
 
int svc()
{
int rc=0;
ACE_Method_Request * method=NULL;
MethodProxy * mp=NULL;
while(true)
{
mp=(MethodProxy*)queue_.dequeue();
if(NULL==mp) continue;
rc=mp->call(this);
if(mp->future_)
mp->future_->set(rc);
delete mp; // we are responsible for MethodProxy objects, but not for futures!
}
}
 
public:
int enqueue(MethodProxy * method)
{
return queue_.enqueue(method);
}
};
 
 
int main(int argc, char* argv[])
{
Executor exec;
exec.activate();
 
future_result * future = new future_result;
Play *play = new Play("c:\\hello.wav",future);
exec.enqueue(play);
int rc=-1;
future->get(rc);
delete future; future=NULL; // after retrieving, delete the object!
cout << "Got result = "<<rc<<endl;
return 0;
}