summaryrefslogtreecommitdiff
path: root/CommonAPI-Examples/E05Manager/src/E05ManagerService.cpp
blob: c32faa0ccf70a8641cc2f36f0594a2ad48cdc39a (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
/* Copyright (C) 2014 BMW Group
 * Author: Manfred Bathelt (manfred.bathelt@bmw.de)
 * Author: Juergen Gehring (juergen.gehring@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 <thread>
#include <iostream>

#include <CommonAPI/CommonAPI.hpp>
#include "E05ManagerStubImpl.h"

using namespace v1_0::commonapi::examples;

static unsigned int cnt = 0; // counter for simulating external events
const static unsigned int maxDeviceNumber = 3;
const static std::string managerInstanceName = "commonapi.examples.Manager";

int main() {
	CommonAPI::Runtime::setProperty("LogContext", "E05S");
	CommonAPI::Runtime::setProperty("LibraryBase", "E05Manager");

    std::shared_ptr<CommonAPI::Runtime> runtime = CommonAPI::Runtime::get();
    std::shared_ptr<E05ManagerStubImpl> myService = std::make_shared < E05ManagerStubImpl > (managerInstanceName);

	bool successfullyRegistered = runtime->registerService("local", managerInstanceName, myService);

	while (!successfullyRegistered) {
		std::cout << "Register Service failed, trying again in 100 milliseconds..." << std::endl;
		std::this_thread::sleep_for(std::chrono::milliseconds(100));
		successfullyRegistered = runtime->registerService("local", managerInstanceName, myService);
	}

	std::cout << "Successfully Registered Service!" << std::endl;

    while (true) {
        // Simulate external events
        if (cnt == 0) {
            myService->specialDeviceDetected(cnt);
        } else if (cnt < maxDeviceNumber) {
            myService->deviceDetected(cnt);
        } else if (cnt == maxDeviceNumber) {
            myService->specialDeviceRemoved(cnt % maxDeviceNumber);
        } else {
            myService->deviceRemoved(cnt % maxDeviceNumber);
        }

        std::this_thread::sleep_for(std::chrono::seconds(1));
        cnt = ++cnt < maxDeviceNumber << 1 ? cnt : 0;
    }
    return 0;
}