-
Notifications
You must be signed in to change notification settings - Fork 132
Expand file tree
/
Copy pathmain.cpp
More file actions
99 lines (88 loc) · 3.98 KB
/
main.cpp
File metadata and controls
99 lines (88 loc) · 3.98 KB
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
/**
* \file src/main.cpp
* \brief Minimal RPC example to call 'requestHelloService()' of remote object running in the same process, but in other thread.
**/
#include "areg/base/GEGlobal.h"
#include "areg/appbase/Application.hpp"
#include "areg/component/Component.hpp"
#include "areg/component/ComponentLoader.hpp"
#include "areg/component/ComponentThread.hpp"
#include "examples/01_minimalrpc/services/HelloServiceStub.hpp"
#include "examples/01_minimalrpc/services/HelloServiceClientBase.hpp"
// Use these options if compile for Windows with MSVC
// It links with areg library (dynamic or static) and generated static library
#ifdef _MSC_VER
#pragma comment(lib, "areg")
#pragma comment(lib, "01_generated")
#endif // _MSC_VER
//!< Service Provider: ServiceProvider declaration
class ServiceProvider : public Component
, protected HelloServiceStub
{
public:
ServiceProvider(const NERegistry::ComponentEntry& entry, ComponentThread& owner)
: Component(entry, owner)
, HelloServiceStub(static_cast<Component&>(self()))
{ }
//!< The request method of the HelloService Interface
virtual void requestHelloService(void) override
{
std::cout << "\'Hello Service!\'" << std::endl;
Application::signalAppQuit(); // quit application is if received response
}
private:
inline ServiceProvider& self(void)
{ return (*this); }
};
//!< ServiceConsumer declaration
class ServiceConsumer : public Component
, protected HelloServiceClientBase
{
public:
ServiceConsumer(const NERegistry::ComponentEntry & entry, ComponentThread & owner)
: Component ( entry, owner )
, HelloServiceClientBase( entry.mDependencyServices[0].mRoleName, owner )
{ }
//!< Service discovery notification. Called when the "ServiceProvder" is available and unavailable.
//!< The `status` parameter contains availability flag. Return `true` if the service connection notification is relevant.
virtual bool serviceConnected(NEService::eServiceConnection status, ProxyBase& proxy) override
{
if (HelloServiceClientBase::serviceConnected(status, proxy) && NEService::isServiceConnected(status))
requestHelloService(); // Call of method of remote "ServiceProvider" object.
// Return `true` if the service connection notification is relevant.
return true;
}
};
//////////////////////////////////////////////////////////////////////////
// Define the model to load and instantiate threads and objects
//////////////////////////////////////////////////////////////////////////
// Describe model, register the service provider and service consumer in
// 2 different threads "Thread1" and "Thread2"
BEGIN_MODEL("ServiceModel")
// Thread 1 without watchdog, contains a service provider
BEGIN_REGISTER_THREAD( "Thread1" )
BEGIN_REGISTER_COMPONENT( "ServiceProvider", ServiceProvider )
REGISTER_IMPLEMENT_SERVICE( NEHelloService::ServiceName, NEHelloService::InterfaceVersion )
END_REGISTER_COMPONENT( "ServiceProvider" )
END_REGISTER_THREAD( "Thread1" )
// Thread 2 without watchdog, contains a service consumer
BEGIN_REGISTER_THREAD( "Thread2" )
BEGIN_REGISTER_COMPONENT( "ServiceConsumer", ServiceConsumer )
REGISTER_DEPENDENCY( "ServiceProvider" ) /* dependency reference to the remote service*/
END_REGISTER_COMPONENT( "ServiceConsumer" )
END_REGISTER_THREAD( "Thread2" )
// end of model description
END_MODEL("ServiceModel")
//!< main function
int main(void)
{
// Initialize application, enable logging, servicing, routing, timer and watchdog, using default settings.
Application::initApplication();
// load model to initialize components
Application::loadModel("ServiceModel");
// wait until Application quit signal is set.
Application::waitAppQuit(NECommon::WAIT_INFINITE);
// release and cleanup resources of application.
Application::releaseApplication();
return 0;
}