summaryrefslogtreecommitdiff
path: root/CommonAPI-Examples/E07Mainloop/src/E07MainloopClient.cpp
blob: 70873a8a48af006c1643139118eb468bf7686a18 (plain)
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/* Copyright (C) 2015 BMW Group
 * Author: Lutz Bichler (lutz.bichler@bmw.de)
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include <iostream>

#ifndef WIN32
#include <unistd.h>
#endif

#include <sstream>

#include <glib.h>
#include <gio/gio.h>

#include <CommonAPI/CommonAPI.hpp>
#include <v1_0/commonapi/examples/E07MainloopProxy.hpp>

using namespace v1_0::commonapi::examples;

GIOChannel* channel;

std::future<CommonAPI::CallStatus> gFutureCallStatus;
std::future<CommonAPI::CallStatus> gFutureCallStatusISIA;
std::future<CommonAPI::CallStatus> gFutureCallStatusGetAttrX;

int32_t gValueForX = 428394;

void myAttrXCallback(const CommonAPI::CallStatus& callStatus, const int32_t& val) {
    std::cout << "Receive callback for Attribute x: " << val << std::endl;
}

void mySayHelloCallback(const CommonAPI::CallStatus& _callStatus, const std::string& _returnMessage) {

	if (_callStatus != CommonAPI::CallStatus::SUCCESS) {
		std::cerr << "Remote call failed!\n";
		return;
	}
	std::cout << "Got message: '" << _returnMessage << "'\n";

}

gboolean callSetAttrX(void* proxy) {

	std::cout << "callSetAttrX called ..." << std::endl;

	E07MainloopProxy<>* myProxy = static_cast<E07MainloopProxy<>*>(proxy);
	myProxy->getXAttribute().setValueAsync(gValueForX , myAttrXCallback);

	return false;
}

gboolean callGetAttrX(void* proxy) {

	std::cout << "callGetAttrX called ..." << std::endl;

	E07MainloopProxy<>* myProxy = static_cast<E07MainloopProxy<>*>(proxy);
	myProxy->getXAttribute().getValueAsync(myAttrXCallback);

	return false;
}

gboolean callSayHello(void* proxy) {

	std::cout << "callSayHello called ..." << std::endl;

	static int number = 1;

	std::stringstream stream;
	stream << "World (" << number << ")";
    const std::string name = stream.str();

	E07MainloopProxy<>* myProxy = static_cast<E07MainloopProxy<>*>(proxy);
	gFutureCallStatus = myProxy->sayHelloAsync(name, mySayHelloCallback);

	number++;

    return true;
}

class GDispatchWrapper: public GSource {
 public:
    GDispatchWrapper(CommonAPI::DispatchSource* dispatchSource): dispatchSource_(dispatchSource) {}
    CommonAPI::DispatchSource* dispatchSource_;
};

gboolean dispatchPrepare ( GSource* source, gint* timeout ) {

	bool result = false;
	int64_t eventTimeout;

	result = static_cast<GDispatchWrapper*>(source)->dispatchSource_->prepare(eventTimeout);

	*timeout = eventTimeout;

    return result;
}

gboolean dispatchCheck ( GSource* source ) {

	return static_cast<GDispatchWrapper*>(source)->dispatchSource_->check();
}

gboolean dispatchExecute ( GSource* source, GSourceFunc callback, gpointer userData ) {

	static_cast<GDispatchWrapper*>(source)->dispatchSource_->dispatch();
    return true;
}

static GSourceFuncs standardGLibSourceCallbackFuncs = {
	dispatchPrepare,
	dispatchCheck,
	dispatchExecute,
	NULL
};

gboolean gWatchDispatcher ( GIOChannel *source, GIOCondition condition, gpointer userData ) {

	CommonAPI::Watch* watch = static_cast<CommonAPI::Watch*>(userData);

#ifdef WIN32
	condition = static_cast<GIOCondition>(POLLIN);
#endif

	watch->dispatch(condition);
    return true;
}

gboolean gTimeoutDispatcher ( void* userData ) {

	return static_cast<CommonAPI::DispatchSource*>(userData)->dispatch();
}

void watchAddedCallback ( CommonAPI::Watch* watch, const CommonAPI::DispatchPriority dispatchPriority ) {
	const pollfd& fileDesc = watch->getAssociatedFileDescriptor();

#ifdef WIN32
	channel = g_io_channel_win32_new_socket(fileDesc.fd);
	GSource* gWatch = g_io_create_watch(channel, GIOCondition::G_IO_IN);
#else
	channel = g_io_channel_unix_new(fileDesc.fd); 
	GSource* gWatch = g_io_create_watch(channel, static_cast<GIOCondition>(fileDesc.events));
#endif

    g_source_set_callback(gWatch, reinterpret_cast<GSourceFunc>(&gWatchDispatcher), watch, NULL);

    const auto& dependentSources = watch->getDependentDispatchSources();
    for (auto dependentSourceIterator = dependentSources.begin();
            dependentSourceIterator != dependentSources.end();
            dependentSourceIterator++) {
        GSource* gDispatchSource = g_source_new(&standardGLibSourceCallbackFuncs, sizeof(GDispatchWrapper));
        static_cast<GDispatchWrapper*>(gDispatchSource)->dispatchSource_ = *dependentSourceIterator;

        g_source_add_child_source(gWatch, gDispatchSource);

    }
    int source = g_source_attach(gWatch, NULL);
}

void watchRemovedCallback ( CommonAPI::Watch* watch ) {

	g_source_remove_by_user_data(watch);

    if(channel) {
        g_io_channel_unref(channel);
        channel = NULL;
    }
}

int main() {
	CommonAPI::Runtime::setProperty("LogContext", "E07C");
	CommonAPI::Runtime::setProperty("LibraryBase", "E07Mainloop");

    std::shared_ptr < CommonAPI::Runtime > runtime = CommonAPI::Runtime::get();

    std::string domain = "local";
    std::string instance = "commonapi.examples.Mainloop";
    std::string connection = "client-sample";

    std::shared_ptr<CommonAPI::MainLoopContext> mainloopContext = std::make_shared<CommonAPI::MainLoopContext>(connection);

	std::function<void(CommonAPI::Watch*, const CommonAPI::DispatchPriority)> f_watchAddedCallback = watchAddedCallback;
	std::function<void(CommonAPI::Watch*)> f_watchRemovedCallback = watchRemovedCallback;
	mainloopContext->subscribeForWatches(f_watchAddedCallback, f_watchRemovedCallback);

    std::shared_ptr<E07MainloopProxy<>> myProxy = runtime->buildProxy<E07MainloopProxy>(domain,
    		instance, mainloopContext);

    std::cout << "Checking availability" << std::flush;
    static 
		#ifndef WIN32
			constexpr
		#endif
	bool mayBlock = false;
		
    int count = 0;
    while (!myProxy->isAvailable()) {
    	if (count % 10 == 0)
    		std::cout << "." << std::flush;
    	g_main_context_iteration(NULL, mayBlock);
    	usleep(50000);
    }
    std::cout << "done." << std::endl;

    GMainLoop* mainloop = NULL;
    mainloop = g_main_loop_new(NULL, FALSE);

	void *proxyPtr = (void*)myProxy.get();
	g_timeout_add(100, callSayHello, proxyPtr);
	g_timeout_add(5000, callGetAttrX, proxyPtr);
	g_timeout_add(9000, callSetAttrX, proxyPtr);

    g_main_loop_run (mainloop);
    g_main_loop_unref (mainloop);

    return 0;
}