Merge pull request #58 from tripzero/master
[profile/ivi/automotive-message-broker.git] / tests / testProtocol.cpp
1 #include "testProtocolCommon.h"
2
3 #include <debugout.h>
4 #include <jsonprotocol.h>
5
6 #include <QLocalSocket>
7 #include <QLocalServer>
8 #include <QCoreApplication>
9 #include <QObject>
10
11 #include <memory>
12
13 class Server : public amb::AmbRemoteServer
14 {
15 public:
16         Server(QLocalSocket* socketConnection)
17                 : amb::AmbRemoteServer(new DomainSocket(socketConnection), nullptr),
18                   speed(100), engineSpeed(1999)
19         {}
20
21
22         // AmbRemoteServer interface
23 protected:
24         void list(amb::ListMethodCall::Ptr call)
25         {
26                 DebugOut(0) << "list called" << endl;
27
28                 amb::Object::Object::Ptr interface1(new amb::Object("interface1"));
29                 amb::Object::Object::Ptr interface2( new amb::Object("interface2"));
30
31                 interface1->emplace("vehicleSpeed", std::shared_ptr<AbstractPropertyType>(new VehicleProperty::VehicleSpeedType(speed)));
32                 interface1->emplace("engineSpeed", std::shared_ptr<AbstractPropertyType>(new VehicleProperty::EngineSpeedType(engineSpeed)));
33
34                 interface2->emplace("engineSpeed", std::shared_ptr<AbstractPropertyType>(new VehicleProperty::EngineSpeedType(3099)));
35
36                 call->objectNames.push_back(interface1);
37                 call->objectNames.push_back(interface2);
38                 amb::MethodReply<amb::ListMethodCall> reply(call, true);
39
40                 send(reply);
41         }
42         void get(amb::GetMethodCall::Ptr get)
43         {
44                 DebugOut(0) << "get called" << endl;
45
46                 if(get->value->interfaceName == "interface1")
47                 {
48                         amb::Object::Object::Ptr interface1(new amb::Object("interface1"));
49
50                         interface1->emplace("vehicleSpeed", std::shared_ptr<AbstractPropertyType>(new VehicleProperty::VehicleSpeedType(100)));
51                         interface1->emplace("engineSpeed", std::shared_ptr<AbstractPropertyType>(new VehicleProperty::EngineSpeedType(1999)));
52                         get->value = interface1;
53                         amb::MethodReply<amb::GetMethodCall> reply(get, true);
54                         send(reply);
55                 }
56                 else if(get->value->interfaceName == "interface2")
57                 {
58                         amb::Object::Object::Ptr interface2(new amb::Object("interface2"));
59                         interface2->emplace("engineSpeed", std::shared_ptr<AbstractPropertyType>(new VehicleProperty::EngineSpeedType(3099)));
60                         get->value = interface2;
61                         amb::MethodReply<amb::GetMethodCall> reply(get, true);
62                         send(reply);
63                 }
64         }
65         void set(amb::SetMethodCall::Ptr set)
66         {
67                 if(set->value->interfaceName == "interface1")
68                 {
69                         speed = set->value->at("vehicleSpeed")->value<uint16_t>();
70
71                         DebugOut(0) << "Speed set to " << speed << endl;
72                         amb::MethodReply<amb::SetMethodCall> reply (set, true);
73                         send(reply);
74                 }
75                 else
76                 {
77                         amb::MethodReply<amb::SetMethodCall> reply (set, false);
78                         send(reply);
79                 }
80         }
81         void subscribe(amb::SubscribeMethodCall::Ptr call)
82         {
83                 DebugOut(0) << "subcribe to interface " << call->interfaceName << " zone: " << call->zone << " source" << call->sourceUuid << endl;
84
85                 if(call->interfaceName == "interface1")
86                 {
87                         amb::Object::Object::Ptr interface1(new amb::Object("interface1"));
88
89                         interface1->emplace("vehicleSpeed", std::shared_ptr<AbstractPropertyType>(new VehicleProperty::VehicleSpeedType(++speed)));
90                         interface1->emplace("engineSpeed", std::shared_ptr<AbstractPropertyType>(new VehicleProperty::EngineSpeedType(++engineSpeed)));
91
92                         amb::PropertyChangeEvent event;
93                         event.value = interface1;
94                         event.sourceUuid = call->sourceUuid;
95                         event.zone = call->zone;
96
97                         send(event);
98                 }
99         }
100
101         uint16_t speed;
102         uint16_t engineSpeed;
103 };
104
105 int main(int argc, char** argv)
106 {
107         DebugOut::setDebugThreshhold(7);
108         DebugOut::setThrowErr(true);
109         DebugOut::setThrowWarn(false);
110
111         DebugOut(0) << "Testing AMB json server/client" << endl;
112
113         QCoreApplication app(argc, argv);
114
115         QLocalServer server;
116
117         QLocalServer::removeServer("/tmp/amb");
118
119         if(!server.listen("/tmp/amb"))
120         {
121                 DebugOut(DebugOut::Error) << server.errorString().toStdString() << endl;
122         }
123
124
125         DebugOut(0) << "parent waiting for new connection..." << endl;
126         server.waitForNewConnection(-1);
127         QLocalSocket *clientSocket = server.nextPendingConnection();
128
129         g_assert(clientSocket);
130
131         Server *s = new Server(clientSocket);
132
133         app.exec();
134 }