1 #include "testProtocolCommon.h"
4 #include <jsonprotocol.h>
6 #include <QLocalSocket>
7 #include <QLocalServer>
8 #include <QCoreApplication>
13 class Server : public amb::AmbRemoteServer
16 Server(QLocalSocket* socketConnection)
17 : amb::AmbRemoteServer(new DomainSocket(socketConnection), nullptr),
18 speed(100), engineSpeed(1999)
22 // AmbRemoteServer interface
24 void list(amb::ListMethodCall::Ptr call)
26 DebugOut(0) << "list called" << endl;
28 amb::Object::Object::Ptr interface1(new amb::Object("interface1"));
29 amb::Object::Object::Ptr interface2( new amb::Object("interface2"));
31 interface1->emplace("vehicleSpeed", std::shared_ptr<AbstractPropertyType>(new VehicleProperty::VehicleSpeedType(speed)));
32 interface1->emplace("engineSpeed", std::shared_ptr<AbstractPropertyType>(new VehicleProperty::EngineSpeedType(engineSpeed)));
34 interface2->emplace("engineSpeed", std::shared_ptr<AbstractPropertyType>(new VehicleProperty::EngineSpeedType(3099)));
36 call->objectNames.push_back(interface1);
37 call->objectNames.push_back(interface2);
38 amb::MethodReply<amb::ListMethodCall> reply(call, true);
42 void get(amb::GetMethodCall::Ptr get)
44 DebugOut(0) << "get called" << endl;
46 if(get->value->interfaceName == "interface1")
48 amb::Object::Object::Ptr interface1(new amb::Object("interface1"));
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);
56 else if(get->value->interfaceName == "interface2")
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);
65 void set(amb::SetMethodCall::Ptr set)
67 if(set->value->interfaceName == "interface1")
69 speed = set->value->at("vehicleSpeed")->value<uint16_t>();
71 DebugOut(0) << "Speed set to " << speed << endl;
72 amb::MethodReply<amb::SetMethodCall> reply (set, true);
77 amb::MethodReply<amb::SetMethodCall> reply (set, false);
81 void subscribe(amb::SubscribeMethodCall::Ptr call)
83 DebugOut(0) << "subcribe to interface " << call->interfaceName << " zone: " << call->zone << " source" << call->sourceUuid << endl;
85 if(call->interfaceName == "interface1")
87 amb::Object::Object::Ptr interface1(new amb::Object("interface1"));
89 interface1->emplace("vehicleSpeed", std::shared_ptr<AbstractPropertyType>(new VehicleProperty::VehicleSpeedType(++speed)));
90 interface1->emplace("engineSpeed", std::shared_ptr<AbstractPropertyType>(new VehicleProperty::EngineSpeedType(++engineSpeed)));
92 amb::PropertyChangeEvent event;
93 event.value = interface1;
94 event.sourceUuid = call->sourceUuid;
95 event.zone = call->zone;
102 uint16_t engineSpeed;
105 int main(int argc, char** argv)
107 DebugOut::setDebugThreshhold(7);
108 DebugOut::setThrowErr(true);
109 DebugOut::setThrowWarn(false);
111 DebugOut(0) << "Testing AMB json server/client" << endl;
113 QCoreApplication app(argc, argv);
117 QLocalServer::removeServer("/tmp/amb");
119 if(!server.listen("/tmp/amb"))
121 DebugOut(DebugOut::Error) << server.errorString().toStdString() << endl;
125 DebugOut(0) << "parent waiting for new connection..." << endl;
126 server.waitForNewConnection(-1);
127 QLocalSocket *clientSocket = server.nextPendingConnection();
129 g_assert(clientSocket);
131 Server *s = new Server(clientSocket);