From: rtrimana Date: Wed, 18 Jan 2017 20:02:08 +0000 (-0800) Subject: Perfecting IoTSlave for C++; Found one issue with SSH/Java process execution in which... X-Git-Url: http://plrg.eecs.uci.edu/git/?a=commitdiff_plain;h=4d76dd9bf966df1b7fa8acfbcb14911d0bc5fdd0;p=iot2.git Perfecting IoTSlave for C++; Found one issue with SSH/Java process execution in which process is killed if we print something through cout in the C++ program --- diff --git a/benchmarks/Cpp/Lifxtest/Lifxtest.cpp b/benchmarks/Cpp/Lifxtest/Lifxtest.cpp index dbdfc82..53cb425 100644 --- a/benchmarks/Cpp/Lifxtest/Lifxtest.cpp +++ b/benchmarks/Cpp/Lifxtest/Lifxtest.cpp @@ -6,80 +6,120 @@ #include "LifxLightBulb.cpp" #include "Iterator.hpp" -Lifxtest::Lifxtest() { +// External create, destroy, and init functions +extern "C" void* createLifxtest(void** params) { + // Arguments: IoTSet* lifx_light_bulb + return new Lifxtest((IoTSet*) params[0]); +} + + +extern "C" void destroyLifxtest(void* t) { + Lifxtest* lt = (Lifxtest*) t; + delete lt; +} + + +extern "C" void initLifxtest(void* t) { + Lifxtest* lt = (Lifxtest*) t; + lt->init(); } -/*Lifxtest::Lifxtest(IoTSet _lifx_light_bulb) { +Lifxtest::Lifxtest() { + log.open("Lifxtest_object_cpp.log"); + log << "lifx_light_bulb initialized!" << endl; +} +Lifxtest::Lifxtest(IoTSet* _lifx_light_bulb) { + + log.open("Lifxtest_object_cpp.log"); lifx_light_bulb = _lifx_light_bulb; -}*/ + log << "lifx_light_bulb initialized!" << endl; +} Lifxtest::Lifxtest(void** args) { - //lifx_light_bulb = *(IoTSet*) args[0]; - lifx_light_bulb = *(IoTSet*) args[0]; + log.open("Lifxtest_object_cpp.log"); + lifx_light_bulb = (IoTSet*) args[0]; + log << "lifx_light_bulb initialized!" << endl; } + Lifxtest::~Lifxtest() { } void Lifxtest::init() { - //unordered_set* bulbSet = lifx_light_bulb.values(); - unordered_set* bulbSet = lifx_light_bulb.values(); - //for(LightBulbTest* lifx : *bulbSet) { - for(LightBulb* lifx : *bulbSet) { - + unordered_set* bulbSet = lifx_light_bulb->values(); + //for (unordered_set::const_iterator itr = bulbSet->begin(); itr != bulbSet->end(); ++itr) { + log << "Get into Lifxtest init()!" << endl; + for (auto itr = bulbSet->begin(); itr != bulbSet->end(); ++itr) { + log << "Iteration init()!" << endl; + //LightBulb* lifx = (LightBulb*) *itr; + LightBulbTest* lifx = (LightBulbTest*) *itr; + log << "Getting object!" << endl; lifx->init(); + log << "Executing init!" << endl; this_thread::sleep_for (chrono::milliseconds(1000)); - for (int i = 0; i < 1; i++) { + for (int i = 0; i < 5; i++) { lifx->turnOff(); - cout << "Turning off!" << endl; + //cout << "Turning off!" << endl; + log << "Turning off!" << endl; this_thread::sleep_for (chrono::milliseconds(1000)); lifx->turnOn(); - cout << "Turning on!" << endl; + //cout << "Turning on!" << endl; + log << "Turning on!" << i << endl; this_thread::sleep_for (chrono::milliseconds(1000)); } + -/* for (int i = 2500; i < 9000; i += 100) { - cout << "Adjusting Temp: " << i << endl; + for (int i = 2500; i < 9000; i += 100) { + //cout << "Adjusting Temp: " << i << endl; + log << "Adjusting Temp: " << i << endl; lifx->setTemperature(i); this_thread::sleep_for (chrono::milliseconds(100)); } for (int i = 9000; i > 2500; i -= 100) { - cout << "Adjusting Temp: " << i << endl; + //cout << "Adjusting Temp: " << i << endl; + log << "Adjusting Temp: " << i << endl; lifx->setTemperature(i); this_thread::sleep_for (chrono::milliseconds(100)); } for (int i = 100; i > 0; i -= 10) { - cout << "Adjusting Brightness: " << i << endl; + //cout << "Adjusting Brightness: " << i << endl; + log << "Adjusting Brightness: " << i << endl; lifx->setColor(lifx->getHue(), lifx->getSaturation(), i); this_thread::sleep_for (chrono::milliseconds(500)); } for (int i = 0; i < 100; i += 10) { - cout << "Adjusting Brightness: " << i << endl; + //cout << "Adjusting Brightness: " << i << endl; + log << "Adjusting Brightness: " << i << endl; lifx->setColor(lifx->getHue(), lifx->getSaturation(), i); this_thread::sleep_for (chrono::milliseconds(500)); - }*/ + } + lifx->turnOff(); } + + log << "End of iteration.. closing!" << endl; + log.close(); + while(true) { } // Testing infinite loop - will need to do "pkill IoTSlave" } -int main(int argc, char *argv[]) +/*int main(int argc, char *argv[]) { // LightBulb #1 string macAddress1 = "D073D5128E300000"; string devIPAddress1 = "192.168.2.126"; IoTDeviceAddress* devAddress1 = new IoTDeviceAddress(devIPAddress1, 12345, 56700, false, false); - unordered_set* myset1 = new unordered_set(); + unordered_set* myset1 = new unordered_set(); myset1->insert(devAddress1); - IoTSet* setDevAddress1 = new IoTSet(*myset1); + IoTSet* setDevAddress1 = new IoTSet(myset1); LifxLightBulb *llb1 = new LifxLightBulb(setDevAddress1, macAddress1); //cout << "Generated LifxLightBulb object!" << endl; @@ -87,20 +127,21 @@ int main(int argc, char *argv[]) string macAddress2 = "D073D50241DA0000"; string devIPAddress2 = "192.168.2.232"; IoTDeviceAddress* devAddress2 = new IoTDeviceAddress(devIPAddress2, 12346, 56700, false, false); - unordered_set* myset2 = new unordered_set(); + unordered_set* myset2 = new unordered_set(); myset2->insert(devAddress2); - IoTSet* setDevAddress2 = new IoTSet(*myset2); + IoTSet* setDevAddress2 = new IoTSet(myset2); LifxLightBulb *llb2 = new LifxLightBulb(setDevAddress2, macAddress2); // Set of lightbulbs - unordered_set* setLb = new unordered_set(); + unordered_set* setLb = new unordered_set(); setLb->insert(llb1); setLb->insert(llb2); - IoTSet* lbSet = new IoTSet(*setLb); + IoTSet* lbSet = new IoTSet(setLb); - void* args[1]; - args[0] = (void*) lbSet; - Lifxtest *lt = new Lifxtest(args); + //void* args[1]; + //args[0] = (void*) lbSet; + //Lifxtest *lt = new Lifxtest(args); + Lifxtest *lt = new Lifxtest(lbSet); lt->init(); //delete llb1; @@ -113,10 +154,8 @@ int main(int argc, char *argv[]) delete setDevAddress2; delete setLb; delete lbSet; - //delete llb1; - //delete llb2; return 0; } - +*/ diff --git a/benchmarks/Cpp/Lifxtest/Lifxtest.hpp b/benchmarks/Cpp/Lifxtest/Lifxtest.hpp index 0cdc745..eb8c15b 100644 --- a/benchmarks/Cpp/Lifxtest/Lifxtest.hpp +++ b/benchmarks/Cpp/Lifxtest/Lifxtest.hpp @@ -1,22 +1,25 @@ #ifndef _LIFXTEST_HPP__ #define _LIFXTEST_HPP__ #include +#include #include "IoTSet.hpp" #include "LightBulb.hpp" #include "LightBulbTest.hpp" + class Lifxtest { private: // IoTSet - //IoTSet lifx_light_bulb; - IoTSet lifx_light_bulb; + IoTSet* lifx_light_bulb; + + ofstream log; public: Lifxtest(); - //Lifxtest(IoTSet _lifx_light_bulb); + Lifxtest(IoTSet* _lifx_light_bulb); Lifxtest(void** args); ~Lifxtest(); void init(); diff --git a/benchmarks/Cpp/Lifxtest/LightBulbTest_Stub.cpp b/benchmarks/Cpp/Lifxtest/LightBulbTest_Stub.cpp index 4a046bc..18fb780 100644 --- a/benchmarks/Cpp/Lifxtest/LightBulbTest_Stub.cpp +++ b/benchmarks/Cpp/Lifxtest/LightBulbTest_Stub.cpp @@ -1,11 +1,46 @@ #include +#include #include "LightBulbTest_Stub.hpp" -// External creator/destroyer -/*extern "C" LightBulbTest_Stub* create(int _port, const char* _skeletonAddress, string _callbackAddress, int _rev, bool* _bResult, vector _ports) { - return new LightBulbTest_Stub(_port, _skeletonAddress, _callbackAddress, _rev, _bResult, _ports); +// External create, destroy, and init functions +extern "C" void* createLightBulbTest_Stub(void** params) { + // Arguments: int _port, const char* _skeletonAddress, string _callbackAddress, int _rev, bool* _bResult, vector _ports + return new LightBulbTest_Stub(*((int*) params[0]), ((string*) params[1])->c_str(), *((string*) params[2]), *((int*) params[3]), (bool*) params[4], *((vector*) params[5])); } -extern "C" void destroy(LightBulbTest_Stub* t) { - delete t; -}*/ + +extern "C" void destroyLightBulbTest_Stub(void* t) { + LightBulbTest_Stub* lbs = (LightBulbTest_Stub*) t; + delete lbs; +} + + +extern "C" void initLightBulbTest_Stub(void* t) { + LightBulbTest_Stub* lbs = (LightBulbTest_Stub*) t; + lbs->init(); +} + + +int main(int argc, char *argv[]) +{ + int stubPort = 55179; + vector ports; + ports.push_back(58551); + const char* skeletonAddress = "localhost"; + string callbackAddress = "localhost"; + int rev = 0; + bool result = false; + LightBulbTest_Stub *lbs = new LightBulbTest_Stub(stubPort, skeletonAddress, callbackAddress, rev, &result, ports); + cout << "Successfully instantiated stub!" << endl; + lbs->init(); + for (int i = 0; i < 100; i++) { + lbs->turnOff(); + cout << "Turning off!" << endl; + this_thread::sleep_for (chrono::milliseconds(1000)); + lbs->turnOn(); + cout << "Turning on!" << endl; + this_thread::sleep_for (chrono::milliseconds(1000)); + } + + return 0; +} diff --git a/benchmarks/Cpp/Lifxtest/LightBulbTest_Stub.hpp b/benchmarks/Cpp/Lifxtest/LightBulbTest_Stub.hpp index 5d0c9b0..3e20d5b 100644 --- a/benchmarks/Cpp/Lifxtest/LightBulbTest_Stub.hpp +++ b/benchmarks/Cpp/Lifxtest/LightBulbTest_Stub.hpp @@ -1,6 +1,7 @@ #ifndef _LIGHTBULBTEST_STUB_HPP__ #define _LIGHTBULBTEST_STUB_HPP__ #include +#include #include "LightBulbTest.hpp" using namespace std; @@ -14,7 +15,8 @@ class LightBulbTest_Stub : public LightBulbTest vector ports; const static int objectId = 0; - + + ofstream log; public: @@ -23,7 +25,17 @@ class LightBulbTest_Stub : public LightBulbTest LightBulbTest_Stub(int _port, const char* _skeletonAddress, string _callbackAddress, int _rev, bool* _bResult, vector _ports) { callbackAddress = _callbackAddress; ports = _ports; + // Logging + log.open("LightBulbTest_Stub_cpp.log"); + log << "Port: " << _port << endl; + log << "Skeleton address: " << _skeletonAddress << endl; + log << "Callback address: " << callbackAddress << endl; + log << "Rev: " << _rev << endl; + log << "bResult: " << *_bResult << endl; + log << "Ports: " << _ports[0] << endl; rmiCall = new IoTRMICall(_port, _skeletonAddress, _rev, _bResult); + log << "Established connection with skeleton!" << endl; + //log.close(); } ~LightBulbTest_Stub() { @@ -99,14 +111,18 @@ class LightBulbTest_Stub : public LightBulbTest return retVal; } - void init() { + void init() { + log << "Calling init() in stub!" << endl; int methodId = 0; string retType = "void"; int numParam = 0; string paramCls[] = { }; void* paramObj[] = { }; void* retObj = NULL; + log << "About to remote call!" << endl; rmiCall->remoteCall(objectId, methodId, retType, paramCls, paramObj, numParam, retObj); + log << "Remote call performed!" << endl; + log.close(); } void setTemperature(int _temperature) { @@ -145,6 +161,4 @@ class LightBulbTest_Stub : public LightBulbTest }; -//typedef LightBulbTest_Stub* create_t(int _port, const char* _skeletonAddress, string _callbackAddress, int _rev, bool* _bResult, vector _ports); -//typedef void destroy_t(LightBulbTest_Stub*); #endif diff --git a/benchmarks/Cpp/Lifxtest/Makefile b/benchmarks/Cpp/Lifxtest/Makefile index 5f82d06..648fcca 100755 --- a/benchmarks/Cpp/Lifxtest/Makefile +++ b/benchmarks/Cpp/Lifxtest/Makefile @@ -16,6 +16,11 @@ so-lifxtest: $(G++) $(ARGS) ./Lifxtest.cpp $(BASE)/iotjava/iotruntime/cpp/socket/Socket.cpp -o $(BIN_DIR)/Lifxtest/Lifxtest.so --std=c++11 -pthread -pg -I$(BASE)/iotjava/iotruntime/cpp/ -I$(BASE)/iotjava/iotruntime/cpp/socket/ -I$(BASE)/iotjava/iotruntime/cpp/setrelation/ -I$(BASE)/iotjava/iotrmi/C++/ -I$(BASE)/benchmarks/virtuals/ -I$(BASE)/benchmarks/drivers/Cpp/LifxLightBulb/ cp ./Lifxtest.config $(BIN_DIR)/Lifxtest +PHONY += lightstub +lightstub: + $(G++) ./LightBulbTest_Stub.cpp $(BASE)/iotjava/iotruntime/cpp/socket/Socket.cpp -o $(BIN_DIR)/Lifxtest/LightBulbTest_Stub.o --std=c++11 -pthread -pg -I$(BASE)/iotjava/iotruntime/cpp/ -I$(BASE)/iotjava/iotruntime/cpp/socket/ -I$(BASE)/iotjava/iotruntime/cpp/setrelation/ -I$(BASE)/iotjava/iotrmi/C++/ -I$(BASE)/benchmarks/virtuals/ + cp ./Lifxtest.config $(BIN_DIR)/Lifxtest + PHONY += so-lightstub so-lightstub: $(G++) $(ARGS) ./LightBulbTest_Stub.cpp $(BASE)/iotjava/iotruntime/cpp/socket/Socket.cpp -o $(BIN_DIR)/Lifxtest/LightBulbTest_Stub.so --std=c++11 -pthread -pg -I$(BASE)/iotjava/iotruntime/cpp/ -I$(BASE)/iotjava/iotruntime/cpp/socket/ -I$(BASE)/iotjava/iotruntime/cpp/setrelation/ -I$(BASE)/iotjava/iotrmi/C++/ -I$(BASE)/benchmarks/virtuals/ diff --git a/benchmarks/drivers/Cpp/LifxLightBulb/LifxLightBulb.cpp b/benchmarks/drivers/Cpp/LifxLightBulb/LifxLightBulb.cpp index af7b892..07076db 100644 --- a/benchmarks/drivers/Cpp/LifxLightBulb/LifxLightBulb.cpp +++ b/benchmarks/drivers/Cpp/LifxLightBulb/LifxLightBulb.cpp @@ -10,14 +10,25 @@ using namespace std; -// External creator/destroyer -/*extern "C" LifxLightBulb* create(IoTSet* _devAddress, string macAddress) { - return new LifxLightBulb(_devAddress, macAddress); + +// External functions to create, destroy and initialize this class object +extern "C" void* createLifxLightBulb(void** params) { + // Arguments: IoTSet* _devAddress, string macAddress + return new LifxLightBulb((IoTSet*) params[0], *((string*) params[1])); +} + + +extern "C" void destroyLifxLightBulb(void* t) { + LifxLightBulb* llb = (LifxLightBulb*) t; + delete llb; +} + + +extern "C" void initLifxLightBulb(void* t) { + LifxLightBulb* llb = (LifxLightBulb*) t; + llb->init(); } -extern "C" void destroy(LifxLightBulb* t) { - delete t; -}*/ // Constructor LifxLightBulb::LifxLightBulb() { @@ -48,7 +59,8 @@ LifxLightBulb::LifxLightBulb() { } -LifxLightBulb::LifxLightBulb(IoTSet* _devAddress, string macAddress) { +// Driver constructor always gets a pointer to device address, trailed by class arguments of generic type +LifxLightBulb::LifxLightBulb(IoTSet* _devAddress, string macAddress) { // Initialize macAddress char tmpMacAddress[16]; @@ -61,12 +73,15 @@ LifxLightBulb::LifxLightBulb(IoTSet* _devAddress, string macA tmpMacByte[1] = tmpMacAddress[i+1]; bulbMacAddress[i/2] = (char) strtol(tmpMacByte, NULL, 16); } - cout << "MAC address is set. Value: "; + //cout << "MAC address is set. Value: "; IoTRMIUtil::printBytes(bulbMacAddress, 8, false); + // Logging + log.open("LifxLightBulb_cpp.log"); + log << "MAC address is " << macAddress << endl; // Initialize device address lb_addresses = _devAddress; - cout << "Device address is set! " << endl; + //cout << "Device address is set! " << endl; } @@ -78,9 +93,10 @@ LifxLightBulb::~LifxLightBulb() { delete communicationSocket; communicationSocket = NULL; } - for(IoTDeviceAddress* dev : *lb_addresses) { - delete dev; - dev = NULL; + for(void* dev : *lb_addresses) { + IoTDeviceAddress* dv = (IoTDeviceAddress*) dev; + delete dv; + dv = NULL; } if (lb_addresses != NULL) { @@ -97,16 +113,20 @@ void LifxLightBulb::init() { if (didAlreadyInit.exchange(true)) return; - unordered_set::const_iterator itr = lb_addresses->begin(); - IoTDeviceAddress* deviceAddress = *itr; - cout << "Address: " << deviceAddress->getAddress() << endl; + unordered_set::const_iterator itr = lb_addresses->begin(); + IoTDeviceAddress* deviceAddress = (IoTDeviceAddress*) *itr; + //cout << "Address: " << deviceAddress->getAddress() << endl; + log << "Address: " << deviceAddress->getAddress() << endl; // Create IoTUDP socket communicationSocket = new IoTUDP(deviceAddress); - cout << "Host address: " << communicationSocket->getHostAddress() << endl; - cout << "Source port: " << communicationSocket->getSourcePort() << endl; - cout << "Destination port: " << communicationSocket->getDestinationPort() << endl << endl; + //cout << "Host address: " << communicationSocket->getHostAddress() << endl; + //cout << "Source port: " << communicationSocket->getSourcePort() << endl; + //cout << "Destination port: " << communicationSocket->getDestinationPort() << endl << endl; + log << "Host address: " << communicationSocket->getHostAddress() << endl; + log << "Source port: " << communicationSocket->getSourcePort() << endl; + log << "Destination port: " << communicationSocket->getDestinationPort() << endl << endl; // Launch the worker function in a separate thread. // NOTE: "this" pointer is passed into the detached thread because it does not belong @@ -115,7 +135,9 @@ void LifxLightBulb::init() { thread th1 (&LifxLightBulb::workerFunction, this, this); th1.detach(); - cout << "Initialized LifxLightBulb!" << endl; + //cout << "Initialized LifxLightBulb!" << endl; + log << "Initialized LifxLightBulb!" << endl; + log.close(); } @@ -317,15 +339,15 @@ void LifxLightBulb::receivedPacket(char* packetData) { } int type = recHeader.getType(); - cout << "Received: " << type << endl; + //cout << "Received: " << type << endl; DeviceStateService* dat = NULL; switch (type) { case 3: dat = parseDeviceStateServiceMessage(payloadBytes); - cout << "Service: " << dat->getService(); - cout << "Port : " << dat->getPort(); + //cout << "Service: " << dat->getService(); + //cout << "Port : " << dat->getPort(); // Avoid memory leak - delete this object delete dat; break; @@ -344,7 +366,8 @@ void LifxLightBulb::receivedPacket(char* packetData) { break; default: - cout << "unknown packet Type" << endl; + break; + //cout << "unknown packet Type" << endl; } // Avoid memory leaks delete payloadBytes; @@ -380,7 +403,7 @@ void LifxLightBulb::workerFunction(LifxLightBulb* llb) { int64_t currentTime = (int64_t) time(NULL); if ((currentTime - lastSentGetBulbVersionRequest) > llb->GET_BULB_VERSION_RESEND_WAIT_SECONDS) { // Get the bulb version so we know what type of bulb this is. - cout << "Sending version packet! " << endl; + //cout << "Sending version packet! " << endl; llb->sendGetVersionPacket(); lastSentGetBulbVersionRequest = currentTime; } @@ -1145,10 +1168,10 @@ void onOff(LifxLightBulb *llb) { for (int i = 0; i < 2; i++) { llb->turnOff(); - cout << "Turning off!" << endl; + //cout << "Turning off!" << endl; this_thread::sleep_for (chrono::milliseconds(1000)); llb->turnOn(); - cout << "Turning on!" << endl; + //cout << "Turning on!" << endl; this_thread::sleep_for (chrono::milliseconds(1000)); } } @@ -1157,33 +1180,33 @@ void onOff(LifxLightBulb *llb) { void adjustTemp(LifxLightBulb *llb) { for (int i = 2500; i < 9000; i += 100) { - cout << "Adjusting Temp: " << i << endl; + //cout << "Adjusting Temp: " << i << endl; llb->setTemperature(i); this_thread::sleep_for (chrono::milliseconds(100)); } - cout << "Adjusted temperature to 9000!" << endl; + //cout << "Adjusted temperature to 9000!" << endl; for (int i = 9000; i > 2500; i -= 100) { - cout << "Adjusting Temp: " << i << endl; + //cout << "Adjusting Temp: " << i << endl; llb->setTemperature(i); this_thread::sleep_for (chrono::milliseconds(100)); } - cout << "Adjusted temperature to 2500!" << endl; + //cout << "Adjusted temperature to 2500!" << endl; } void adjustBright(LifxLightBulb *llb) { for (int i = 100; i > 0; i -= 10) { - cout << "Adjusting Brightness: " << i << endl; + //cout << "Adjusting Brightness: " << i << endl; llb->setColor(llb->getHue(), llb->getSaturation(), i); this_thread::sleep_for (chrono::milliseconds(100)); } - cout << "Adjusted brightness to 0!" << endl; + //cout << "Adjusted brightness to 0!" << endl; for (int i = 0; i < 100; i += 10) { - cout << "Adjusting Brightness: " << i << endl; + //cout << "Adjusting Brightness: " << i << endl; llb->setColor(llb->getHue(), llb->getSaturation(), i); this_thread::sleep_for (chrono::milliseconds(100)); } - cout << "Adjusting brightness to 100!" << endl; + //cout << "Adjusting brightness to 100!" << endl; } diff --git a/benchmarks/drivers/Cpp/LifxLightBulb/LifxLightBulb.hpp b/benchmarks/drivers/Cpp/LifxLightBulb/LifxLightBulb.hpp index 7d23c22..ec1bcdb 100644 --- a/benchmarks/drivers/Cpp/LifxLightBulb/LifxLightBulb.hpp +++ b/benchmarks/drivers/Cpp/LifxLightBulb/LifxLightBulb.hpp @@ -1,6 +1,7 @@ #ifndef _LIFXLIGHTBULB_HPP__ #define _LIFXLIGHTBULB_HPP__ #include +#include #include #include #include @@ -86,13 +87,16 @@ class LifxLightBulb : public LightBulb bool stateDidChange = false; // Device address - IoTSet* lb_addresses; + IoTSet* lb_addresses; // IoTSet* lb_addresses + // Logging + ofstream log; public: // Constructor LifxLightBulb(); - LifxLightBulb(IoTSet* _devAddress, string macAddress); + //LifxLightBulb(IoTSet* _devAddress, string macAddress); + LifxLightBulb(IoTSet* _devAddress, string macAddress); ~LifxLightBulb(); // Initialize the lightbulb void init(); @@ -165,6 +169,4 @@ class LifxLightBulb : public LightBulb void handleLightStateMessageReceived(char* payloadData); }; -//typedef LifxLightBulb* create_t(IoTSet* _devAddress, string macAddress); -//typedef void destroy_t(LifxLightBulb*); #endif diff --git a/benchmarks/drivers/Cpp/LifxLightBulb/LightBulb_Skeleton.cpp b/benchmarks/drivers/Cpp/LifxLightBulb/LightBulb_Skeleton.cpp index aab1ac8..f12e77e 100644 --- a/benchmarks/drivers/Cpp/LifxLightBulb/LightBulb_Skeleton.cpp +++ b/benchmarks/drivers/Cpp/LifxLightBulb/LightBulb_Skeleton.cpp @@ -1,11 +1,48 @@ #include #include "LightBulb_Skeleton.hpp" -// External creator/destroyer -/*extern "C" LightBulb_Skeleton* create(LightBulb *_mainObj, string _callbackAddress, int _port) { - return new LightBulb_Skeleton(_mainObj, _callbackAddress, _port); +#include +#include "IoTSet.hpp" +#include "LifxLightBulb.cpp" +#include "IoTDeviceAddress.hpp" + +// External create, destroy, and init functions +extern "C" void* createLightBulb_Skeleton(void** params) { + // Arguments: LightBulb *_mainObj, string _callbackAddress, int _port + return new LightBulb_Skeleton((LightBulb*) params[0], *((string*) params[1]), *((int*) params[2])); +} + + +extern "C" void destroyLightBulb_Skeleton(void* t) { + LightBulb_Skeleton* lbs = (LightBulb_Skeleton*) t; + delete lbs; } -extern "C" void destroy(LightBulb_Skeleton* t) { - delete t; -}*/ + +extern "C" void initLightBulb_Skeleton(void* t) { + LightBulb_Skeleton* lbs = (LightBulb_Skeleton*) t; + lbs->init(); +} + + +int main(int argc, char *argv[]) +{ + // LightBulb #1 + string macAddress1 = "D073D5128E300000"; + string devIPAddress1 = "192.168.2.126"; + //string macAddress1 = "D073D50241DA0000"; + //string devIPAddress1 = "192.168.2.232"; + IoTDeviceAddress* devAddress1 = new IoTDeviceAddress(devIPAddress1, 43583, 56700, false, false); + unordered_set* myset1 = new unordered_set(); + myset1->insert(devAddress1); + IoTSet* setDevAddress1 = new IoTSet(myset1); + LifxLightBulb *llb1 = new LifxLightBulb(setDevAddress1, macAddress1); + //cout << "Generated LifxLightBulb object!" << endl; + + string callbackAddress = "localhost"; + int stubPort = 55179; + LightBulb_Skeleton *lbs = new LightBulb_Skeleton(llb1, callbackAddress, stubPort); + cout << "Successfully instantiated object and its skeleton!" << endl; + + return 0; +} diff --git a/benchmarks/drivers/Cpp/LifxLightBulb/LightBulb_Skeleton.hpp b/benchmarks/drivers/Cpp/LifxLightBulb/LightBulb_Skeleton.hpp index b70b21d..936ea21 100644 --- a/benchmarks/drivers/Cpp/LifxLightBulb/LightBulb_Skeleton.hpp +++ b/benchmarks/drivers/Cpp/LifxLightBulb/LightBulb_Skeleton.hpp @@ -1,6 +1,7 @@ #ifndef _LIGHTBULB_SKELETON_HPP__ #define _LIGHTBULB_SKELETON_HPP__ #include +#include #include "LightBulb.hpp" #include @@ -22,14 +23,19 @@ class LightBulb_Skeleton : public LightBulb const static int object0Id = 0; //LightBulbSmart static set set0Allowed; - + ofstream log; public: LightBulb_Skeleton(LightBulb *_mainObj, string _callbackAddress, int _port) { bool _bResult = false; mainObj = _mainObj; callbackAddress = _callbackAddress; + // Logging + log.open("LightBulb_Skeleton_cpp.log"); + log << "Callback address: " << callbackAddress << endl; + log << "Port: " << _port << endl; rmiObj = new IoTRMIObject(_port, &_bResult); + log << "Established connection with slave! Wait request invoke now..." << endl; ___waitRequestInvokeMethod(); } @@ -288,9 +294,14 @@ class LightBulb_Skeleton : public LightBulb void ___waitRequestInvokeMethod() { while (true) { + log << "Getting into the while loop" << endl; rmiObj->getMethodBytes(); + log << "Getting method bytes now" << endl; + log << "Method len: " << rmiObj->getMethodBytesLen() << endl; int _objectId = rmiObj->getObjectId(); + log << "Object Id: " << _objectId << endl; int methodId = rmiObj->getMethodId(); + log << "Method Id: " << methodId << endl; if (_objectId == object0Id) { if (set0Allowed.find(methodId) == set0Allowed.end()) { cerr << "Object with object Id: " << _objectId << " is not allowed to access method: " << methodId << endl; @@ -325,11 +336,10 @@ class LightBulb_Skeleton : public LightBulb throw exception(); } } + log.close(); } }; set LightBulb_Skeleton::set0Allowed { 2, 10, 1, 3, 11, 8, 12, 7, 13, 9, 6, 16, 17, 4, 0, 14, 15, 5 }; -//typedef LightBulb_Skeleton* create_t(LightBulb *_mainObj, string _callbackAddress, int _port); -//typedef void destroy_t(LightBulb_Skeleton*); #endif diff --git a/benchmarks/drivers/Cpp/Makefile b/benchmarks/drivers/Cpp/Makefile index 74221bc..24d9caf 100644 --- a/benchmarks/drivers/Cpp/Makefile +++ b/benchmarks/drivers/Cpp/Makefile @@ -18,6 +18,11 @@ so-light: cd LifxLightBulb/; $(G++) $(ARGS) ./LifxLightBulb.cpp $(BASE)/../iotjava/iotruntime/cpp/socket/Socket.cpp -o ../$(BIN_DIR)/iotcode/LifxLightBulb/LifxLightBulb.so --std=c++11 -pthread -pg -I$(BASE)/../iotjava/iotruntime/cpp/ -I$(BASE)/../iotjava/iotruntime/cpp/socket/ -I$(BASE)/../iotjava/iotruntime/cpp/setrelation/ -I$(BASE)/../iotjava/iotrmi/C++/ -I$(BASE)/../benchmarks/virtuals/ cp LifxLightBulb/LifxLightBulb.config $(BIN_DIR)/iotcode/LifxLightBulb +PHONY += lightskel +lightskel: + cd LifxLightBulb/; $(G++) ./LightBulb_Skeleton.cpp $(BASE)/../iotjava/iotruntime/cpp/socket/Socket.cpp -o ../$(BIN_DIR)/iotcode/LifxLightBulb/LightBulb_Skeleton.o --std=c++11 -pthread -pg -I$(BASE)/../iotjava/iotruntime/cpp/ -I$(BASE)/../iotjava/iotruntime/cpp/socket/ -I$(BASE)/../iotjava/iotruntime/cpp/setrelation/ -I$(BASE)/../iotjava/iotrmi/C++/ -I$(BASE)/../benchmarks/virtuals/ + cp LifxLightBulb/LifxLightBulb.config $(BIN_DIR)/iotcode/LifxLightBulb + PHONY += so-lightskel so-lightskel: cd LifxLightBulb/; $(G++) $(ARGS) ./LightBulb_Skeleton.cpp $(BASE)/../iotjava/iotruntime/cpp/socket/Socket.cpp -o ../$(BIN_DIR)/iotcode/LifxLightBulb/LightBulb_Skeleton.so --std=c++11 -pthread -pg -I$(BASE)/../iotjava/iotruntime/cpp/ -I$(BASE)/../iotjava/iotruntime/cpp/socket/ -I$(BASE)/../iotjava/iotruntime/cpp/setrelation/ -I$(BASE)/../iotjava/iotrmi/C++/ -I$(BASE)/../benchmarks/virtuals/ diff --git a/iotjava/Makefile b/iotjava/Makefile index 310b859..473dda1 100644 --- a/iotjava/Makefile +++ b/iotjava/Makefile @@ -111,17 +111,18 @@ compile: cp ./iotrmi/C++/basics/* $(BIN_DIR)/iotpolicy/output_files/Cplus cd $(BIN_DIR)/iotpolicy/output_files; cp *.java ./Java cd $(BIN_DIR)/iotpolicy/output_files; cp *.hpp ./Cplus -# cd $(BIN_DIR)/iotpolicy/output_files/Java; $(JAVAC) -cp .:..:../../../$(BIN_DIR) TestClass_Skeleton.java + cd $(BIN_DIR)/iotpolicy/output_files/Java; $(JAVAC) -cp .:..:../../../$(BIN_DIR) TestClass_Skeleton.java # cd $(BIN_DIR)/iotpolicy/output_files/Java; $(JAVAC) -cp .:..:../../../$(BIN_DIR) TestClassAdvanced_Stub.java -# cd $(BIN_DIR)/iotpolicy/output_files/Java; $(JAVAC) -cp .:..:../../../$(BIN_DIR) TestClassCallbacks_Stub.java -# cd $(BIN_DIR)/iotpolicy/output_files/Java; $(JAVAC) -cp .:..:../../../$(BIN_DIR) TestClassInterface*.java + cd $(BIN_DIR)/iotpolicy/output_files/Java; $(JAVAC) -cp .:..:../../../$(BIN_DIR) TestClassCallbacks_Stub.java + cd $(BIN_DIR)/iotpolicy/output_files/Java; $(JAVAC) -cp .:..:../../../$(BIN_DIR) TestClassInterface*.java + cd $(BIN_DIR)/iotpolicy/output_files/Java; $(JAVAC) -cp .:..:../../../$(BIN_DIR) CallBackInterface*.java # cd $(BIN_DIR)/iotpolicy/output_files/Java; $(JAVAC) -cp .:..:../../../$(BIN_DIR) TestClassComplete*.java # cd $(BIN_DIR)/iotpolicy/output_files/Java; $(JAVAC) -cp .:..:../../../$(BIN_DIR) CallBackInterface_CallbackSkeleton.java # cd $(BIN_DIR)/iotpolicy/output_files/Java; $(JAVAC) -cp .:..:../../../$(BIN_DIR) CallBackInterfaceWithCallBack_CallbackStub.java # cd $(BIN_DIR)/iotpolicy/output_files/Cplus; $(G++) ./TestClass_Skeleton.cpp -o ./TestClass_Skeleton.out --std=c++11 -pthread -pg -I../../../../iotjava/iotrmi/C++/ #cd $(BIN_DIR)/iotpolicy/output_files/Cplus; $(G++) ./TestClassAdvanced_Stub.cpp -o ./TestClassAdvanced_Stub.out --std=c++11 -pthread -pg -I../../../../iotjava/iotrmi/C++/ # cd $(BIN_DIR)/iotpolicy/output_files/Cplus; $(G++) ./TestClassCallbacks_Stub.cpp -o ./TestClassCallbacks_Stub.out --std=c++11 -pthread -pg -I../../../../iotjava/iotrmi/C++/ - cd $(BIN_DIR)/iotpolicy/output_files/Cplus; $(G++) ./TestClass.hpp -o ./TestClass.out --std=c++11 -pthread -pg -I../../../../iotjava/iotrmi/C++/ +# cd $(BIN_DIR)/iotpolicy/output_files/Cplus; $(G++) ./TestClass.hpp -o ./TestClass.out --std=c++11 -pthread -pg -I../../../../iotjava/iotrmi/C++/ #cd $(BIN_DIR)/iotpolicy/output_files/Cplus; $(ARM_G++) ./TestClassInterface_Skeleton.cpp -o ./TestClassInterface_Skeleton.out --std=c++11 -pthread -pg -I../../../../iotjava/iotrmi/C++/ #cd $(BIN_DIR)/iotpolicy/output_files/Cplus; $(ARM_G++) ./TestClassCallbacks_Stub.cpp -o ./TestClassCallbacks_Stub.out --std=c++11 -pthread -pg -I../../../../iotjava/iotrmi/C++/ #cd $(BIN_DIR)/iotpolicy/output_files/Cplus; $(ARM_G++) ./TestClassComplete_Stub.hpp --std=c++11 -pthread -pg -I../../../../iotjava/iotrmi/C++/ diff --git a/iotjava/iotruntime/cpp/iotslave/IoTSlave.cpp b/iotjava/iotruntime/cpp/iotslave/IoTSlave.cpp index d950a03..2761fb1 100644 --- a/iotjava/iotruntime/cpp/iotslave/IoTSlave.cpp +++ b/iotjava/iotruntime/cpp/iotslave/IoTSlave.cpp @@ -3,8 +3,11 @@ #include "IoTSlave.hpp" +#include "Lifxtest.cpp" + IoTSlave::IoTSlave(string _serverAddress, int _serverPort, string _objectName) { + isDriverObject = false; // Default to false serverAddress = _serverAddress; serverPort = _serverPort; objectName = _objectName; @@ -20,6 +23,18 @@ IoTSlave::~IoTSlave() { delete socket; socket = NULL; } + /*if (objMainCls != NULL) { + delete objMainCls; + objMainCls = NULL; + } + if (objSkelCls != NULL) { + delete objSkelCls; + objSkelCls = NULL; + }*/ + for (IoTSet* iotset : vecIoTSet) { + delete iotset; + iotset = NULL; + } closeFile(); } @@ -99,7 +114,7 @@ void IoTSlave::closeFile() { } -void IoTSlave::instantiateObject(string objectClassName) { +void IoTSlave::getObjectHandler(string objectClassName) { // Object handling string strObj = FILEPATH + objectClassName + SOEXT; @@ -111,7 +126,8 @@ void IoTSlave::instantiateObject(string objectClassName) { } writeToFile("Object handled!"); // Create handler - create_object = (create_t*) dlsym(handle, CREATEFUNCTION.c_str()); + string createFunction = CREATEFUNCTION + objectClassName; + create_object = (create_t*) dlsym(handle, createFunction.c_str()); const char* dlsym_error = dlerror(); if (dlsym_error) { cerr << "Cannot load symbol create: " << dlsym_error << '\n'; @@ -120,7 +136,8 @@ void IoTSlave::instantiateObject(string objectClassName) { } writeToFile("Object factory created for " + objectClassName); // Destroy handler - destroy_object = (destroy_t*) dlsym(handle, DESTROYFUNCTION.c_str()); + string destroyFunction = DESTROYFUNCTION + objectClassName; + destroy_object = (destroy_t*) dlsym(handle, destroyFunction.c_str()); dlsym_error = dlerror(); if (dlsym_error) { cerr << "Cannot load symbol destroy: " << dlsym_error << '\n'; @@ -128,6 +145,107 @@ void IoTSlave::instantiateObject(string objectClassName) { exit(1); } writeToFile("Object destroyer created for " + objectClassName); + // Create initializer + string initFunction = INITFUNCTION + objectClassName; + init_object = (init_t*) dlsym(handle, initFunction.c_str()); + dlsym_error = dlerror(); + if (dlsym_error) { + cerr << "Cannot load symbol init: " << dlsym_error << '\n'; + writeToFile("Cannot load symbol init!"); + exit(1); + } + writeToFile("Object initializer created for " + objectClassName); +} + + +// Run init_object function +void IoTSlave::runInitObject(IoTSlave* iotslave) { + + iotslave->init_object(iotslave->objMainCls); +} + + +// Instantiate main object! +// Use handler obtained by getObjectHandler() and instantiate object! +void IoTSlave::instantiateMainObject() { + + // IoTDeviceAddress + other arguments + int paramSize = vecIoTSet.size(); + void* params[paramSize]; + for(int i=0; i Driver object name: " + objectName); objectClassName = recvString(); sendAck(); - writeToFile("Driver object class name: " + objectClassName); + writeToFile("=> Driver object class name: " + objectClassName); objectInterfaceName = recvString(); sendAck(); - writeToFile("Driver object interface name: " + objectInterfaceName); + writeToFile("=> Driver object interface name: " + objectInterfaceName); objectSkelClass = recvString(); sendAck(); - writeToFile("Driver object skeleton class name: " + objectSkelClass); + writeToFile("=> Driver object skeleton class name: " + objectSkelClass); objectRegPort = recvInteger(); sendAck(); - writeToFile("Driver object registry port: " + to_string(objectRegPort)); + writeToFile("=> Driver object registry port: " + to_string(objectRegPort)); objectStubPort = recvInteger(); sendAck(); - writeToFile("Driver object stub port: " + to_string(objectStubPort)); + writeToFile("=> Driver object stub port: " + to_string(objectStubPort)); int numOfArgs = recvInteger(); sendAck(); for (int i = 0; i < numOfArgs; i++) { string arg = recvString(); sendAck(); args.push_back(arg); - writeToFile("Got argument: " + arg); + writeToFile("==> Got argument: " + arg); } for (int i = 0; i < numOfArgs; i++) { string argClass = recvString(); sendAck(); - args.push_back(argClass); - writeToFile("Got argument class: " + argClass); + argClasses.push_back(argClass); + writeToFile("==> Got argument class: " + argClass); } // We are just receiving object information here // Instantiation will be done when IoTDeviceAddress has been sent - //instantiateObject(objectClassName); } +// Create a new IoTSet object to hold objects void IoTSlave::createNewIoTSet() { - + objectFieldName = recvString(); sendAck(); + // Instantiating new IoTSet object + isetObject = new unordered_set(); + writeToFile("Creating new IoTSet for field: " + objectFieldName); } +// Get IoTDeviceAddress object reference and put it inside IoTSet object void IoTSlave::getDeviceIoTSetObject() { + writeToFile("Getting IoTDeviceAddress... "); + // Get the IoTDeviceAddress info + hostAddress = recvString(); sendAck(); + writeToFile("=> Host address: " + hostAddress); + int sourcePort = recvInteger(); sendAck(); + writeToFile("=> Source port: " + to_string(sourcePort)); + int destPort = recvInteger(); sendAck(); + writeToFile("=> Destination port: " + to_string(destPort)); + bool sourcePortWildCard = (bool) recvInteger(); sendAck(); + writeToFile("=> Is source port wild card? " + to_string(sourcePortWildCard)); + bool destPortWildCard = (bool) recvInteger(); sendAck(); + writeToFile("=> Is destination port wild card? " + to_string(destPortWildCard)); + // Create IoTDeviceAddress + IoTDeviceAddress* objDeviceAddress = new IoTDeviceAddress(hostAddress, sourcePort, destPort, + sourcePortWildCard, destPortWildCard); + // Insert it into isetObject! + isetObject->insert(objDeviceAddress); + writeToFile("=> Inserting IoTDeviceAddress into set..."); + writeToFile("==> Now we have " + to_string(isetObject->size()) + " object(s)!"); + // Set flag to true; + isDriverObject = true; +} + + +// Get IoTSet object content reference and put it inside IoTSet object +// This is basically the stub objects +void IoTSlave::getIoTSetObject() { + + writeToFile("Getting IoTSet object... "); + // Get the IoTDeviceAddress info + hostAddress = recvString(); sendAck(); + writeToFile("=> Host address: " + hostAddress); + objectName = recvString(); sendAck(); + writeToFile("=> Driver object name: " + objectName); + objectClassName = recvString(); sendAck(); + writeToFile("=> Driver object class name: " + objectClassName); + objectInterfaceName = recvString(); sendAck(); + writeToFile("=> Driver object interface name: " + objectInterfaceName); + objectStubClass = recvString(); sendAck(); + writeToFile("=> Driver object stub class name: " + objectStubClass); + objectRegPort = recvInteger(); sendAck(); + writeToFile("=> Driver object registry port: " + to_string(objectRegPort)); + objectStubPort = recvInteger(); sendAck(); + writeToFile("=> Driver object stub port: " + to_string(objectStubPort)); + int numOfPorts = recvInteger(); sendAck(); + for (int i = 0; i < numOfPorts; i++) { + int port = recvInteger(); sendAck(); + ports.push_back(port); + writeToFile("==> Got a new port: " + to_string(port)); + } + // Create Stub object + unordered_map::const_iterator itr = mapObjNameStub.find(objectName); + if (itr != mapObjNameStub.end()) { // Stub has been created earlier + writeToFile("=> Stub has been created! Getting back reference..."); + objStubCls = itr->second; + } else { // Instantiate a new stub and map it + writeToFile("=> Stub has not been created! Creating a new stub..."); + getObjectHandler(objectStubClass); + instantiateStubObject(); + mapObjNameStub.insert(make_pair(objectName,objStubCls)); + writeToFile("=> Map has: " + to_string(mapObjNameStub.size()) + " members"); + } + // Insert it into isetObject! + isetObject->insert(objStubCls); + writeToFile("=> Inserting stub object into set..."); + writeToFile("==> Now we have " + to_string(isetObject->size()) + " object(s)!"); +} + + +// Reinitialize IoTSet field! +void IoTSlave::reinitializeIoTSetField() { + + writeToFile("Reinitialize IoTSet field..."); + iotsetObject = new IoTSet(isetObject); + // Collect IoTSet field first in a vector + vecIoTSet.push_back(iotsetObject); + + // Create object if this is for driver object + if (isDriverObject) { + // Instantiate driver object + getObjectHandler(objectClassName); + instantiateDriverObject(); + // Instantiate skeleton object + getObjectHandler(objectSkelClass); + instantiateSkelObject(); + } +} + + +// Invoke init() method in main controller +void IoTSlave::invokeInitMethod() { + + writeToFile("Invoke init() method for: " + mainObjectName); + // Instantiate main controller object + getObjectHandler(mainObjectName); + instantiateMainObject(); } @@ -258,9 +475,10 @@ void IoTSlave::getDeviceIoTSetObject() { // Create a main object, e.g. Lifxtest void IoTSlave::createMainObject() { - writeToFile("Creating main object: " + objectName); - string mainObject = recvString(); - sendAck(); + mainObjectName = recvString(); sendAck(); + writeToFile("Creating main object: " + mainObjectName); + // Just receive the name of the class object here + // We will instantiate the object after we get the set/relation objects } @@ -286,9 +504,9 @@ void IoTSlave::commIoTMaster() { writeToFile("Starting main loop..."); // Main iteration/loop while(true) { - IoTCommCode message = (IoTCommCode) recvInteger(); + IoTCommCode message = (IoTCommCode) recvInteger(); sendAck(); //writeToFile("Message: " + (int) message); - + switch(message) { case CREATE_OBJECT: @@ -312,7 +530,7 @@ void IoTSlave::commIoTMaster() { break; case GET_IOTSET_OBJECT: - //getIoTSetObject(); + getIoTSetObject(); break; case GET_IOTRELATION_FIRST_OBJECT: @@ -324,7 +542,7 @@ void IoTSlave::commIoTMaster() { break; case REINITIALIZE_IOTSET_FIELD: - //reinitializeIoTSetField(); + reinitializeIoTSetField(); break; case REINITIALIZE_IOTRELATION_FIELD: @@ -344,7 +562,7 @@ void IoTSlave::commIoTMaster() { break; case INVOKE_INIT_METHOD: - //invokeInitMethod(); + invokeInitMethod(); break; case END_SESSION: @@ -381,7 +599,6 @@ int main(int argc, char *argv[]) { int serverPort = atoi(servPort); string strObjName = argv[3]; IoTSlave *iotSlave = new IoTSlave(serverAddress, serverPort, strObjName); - //iotSlave->sendInteger(123455); iotSlave->sendAck(); iotSlave->commIoTMaster(); diff --git a/iotjava/iotruntime/cpp/iotslave/IoTSlave.hpp b/iotjava/iotruntime/cpp/iotslave/IoTSlave.hpp index f998a09..343a2c2 100644 --- a/iotjava/iotruntime/cpp/iotslave/IoTSlave.hpp +++ b/iotjava/iotruntime/cpp/iotslave/IoTSlave.hpp @@ -4,13 +4,15 @@ #include #include #include +#include #include // For dlopen, dlsym, etc. -#include "ObjectFactory.hpp" -#include "ISet.hpp" +//#include "ObjectFactory.hpp" +//#include "ISet.hpp" #include "IoTSet.hpp" -#include "IRelation.hpp" +#include "IoTDeviceAddress.hpp" +//#include "IRelation.hpp" #include "IoTRelation.hpp" #include "Socket.cpp" @@ -45,11 +47,21 @@ enum IoTCommCode { }; + +// Defining generic function pointers for +// create, destroy, and init functions of each class object +typedef void* create_t(void**); +typedef void destroy_t(void*); +typedef void init_t(void*); + + class IoTSlave { private: // Constants const static int RCVBUFSIZE = 1024; // Size of receive buffer + const static int SKELPARAMSIZE = 3; // Number of params for skeleton + const static int STUBPARAMSIZE = 6; // Number of params for stub const static string FILEPATH; // File path const static string FILEEXT; // File extension const static string SOEXT; // Shared object (.so) extension @@ -57,25 +69,39 @@ class IoTSlave { const static string INTCLASS; // Int class const static string CREATEFUNCTION; // The create function in class const static string DESTROYFUNCTION; // The destroy function in class + const static string INITFUNCTION; // The init function in class + const static string LOCALHOST; // String "localhost" // Class properties string serverAddress; int serverPort; + string hostAddress; + string mainObjectName; string objectName; string objectClassName; string objectInterfaceName; string objectSkelClass; // Need to send from Java IoTSlave: sMessage.getObjectInterfaceName() + SKEL_CLASS_SUFFIX + string objectStubClass; // Need to send from Java IoTSlave: sMessage.getObjectStubInterfaceName() + STUB_CLASS_SUFFIX int objectRegPort; int objectStubPort; - - void* object; // Handler of object + vector ports; // Now used to contain callback ports + string objectFieldName; // Field name that is going to be initialized with IoTSet or IoTRelation + unordered_set* isetObject; // Set of object + IoTSet* iotsetObject; // IoTSet of object + vector*> vecIoTSet; // IoTSet of object TCPSocket* socket; - ofstream log; // Log the messages - vector args; // Hold the arguments for constructor (in string format) - vector argClasses; // Hold the argument classes + ofstream log; // Log the messages + vector args; // Hold the arguments for constructor (in string format) + vector argClasses; // Hold the argument classes + bool isDriverObject; // Set to true if this is IoTSlave instance for a driver object + void* objMainCls; // Main class handler, i.e. driver or controller object + void* objSkelCls; // Skeleton handler + void* objStubCls; // Stub handler + unordered_map mapObjNameStub; // Mapping between object name and stub // Object handlers create_t* create_object; destroy_t* destroy_object; + init_t* init_object; public: // Constructors @@ -97,6 +123,9 @@ class IoTSlave { void createMainObject(); // Create main object void createNewIoTSet(); void getDeviceIoTSetObject(); + void reinitializeIoTSetField(); + void getIoTSetObject(); + void invokeInitMethod(); private: // Private helper functions @@ -107,7 +136,12 @@ class IoTSlave { void openFile(string fileName); void writeToFile(string logMsg); void closeFile(); - void instantiateObject(string objectClassName); + void getObjectHandler(string objectClassName); + void instantiateMainObject(); + void instantiateDriverObject(); + void instantiateSkelObject(); + void instantiateStubObject(); + void runInitObject(IoTSlave* iotslave); }; // Constant initialization @@ -118,5 +152,7 @@ const string IoTSlave::STRINGCLASS = "string"; const string IoTSlave::INTCLASS = "int"; const string IoTSlave::CREATEFUNCTION = "create"; const string IoTSlave::DESTROYFUNCTION = "destroy"; +const string IoTSlave::INITFUNCTION = "init"; +const string IoTSlave::LOCALHOST = "localhost"; #endif diff --git a/iotjava/iotruntime/cpp/iotslave/IoTSlave.java b/iotjava/iotruntime/cpp/iotslave/IoTSlave.java index 98aa828..61a39bc 100644 --- a/iotjava/iotruntime/cpp/iotslave/IoTSlave.java +++ b/iotjava/iotruntime/cpp/iotslave/IoTSlave.java @@ -12,6 +12,7 @@ public class IoTSlave { private static final String STR_LOCALHOST = "localhost"; private static final String STR_IOTSLAVE_CPP = "./IoTSlave.o"; + private static final String STR_IOTSLAVE_PATH = "~/tmp/iot2/iotjava/iotruntime/cpp/iotslave/"; private static final String STR_ACK = "ACK"; private static final String STR_END = "END"; //private static final String STR_LOG_FILE_PATH = "./"; @@ -120,7 +121,8 @@ public class IoTSlave { */ public static String constructCommand(String serverAddress, int serverPort, String strObjName) { - String strCommand = STR_IOTSLAVE_CPP + " " + serverAddress + " " + serverPort + " " + strObjName; + String strCommand = "ssh rtrimana@localhost cd " + STR_IOTSLAVE_PATH + "; " + + STR_IOTSLAVE_CPP + " " + serverAddress + " " + serverPort + " " + strObjName; return strCommand; } @@ -182,16 +184,36 @@ public class IoTSlave { } + /** + * Send communication code to C++ + */ + public void sendCommCode(IoTCommCode inpCommCode) throws IOException { + + + IoTCommCode commCode = inpCommCode; + int intCode = commCode.ordinal(); + sendInteger(intCode); recvAck(); + } + + + /** + * Create a main controller object for C++ + */ + public void createMainObjectCpp() throws IOException { + + sendCommCode(IoTCommCode.CREATE_MAIN_OBJECT); + String strMainObjName = "Lifxtest"; + sendString(strMainObjName); recvAck(); + System.out.println("IoTSlave: Create a main object: " + strMainObjName); + } + + /** * Create a driver object for C++ */ public void createObjectCpp() throws IOException { - IoTCommCode commCode = null; - int intCode = 0; - commCode = IoTCommCode.CREATE_OBJECT; - intCode = commCode.ordinal(); - sendInteger(intCode); recvAck(); + sendCommCode(IoTCommCode.CREATE_OBJECT); String strDrvObjName = "LifxLightBulbLB2"; String strDrvObjClsName = "LifxLightBulb"; String strDrvObjIntfaceClsName = "LightBulb"; @@ -200,6 +222,7 @@ public class IoTSlave { int iStubPort = 55179; // TODO: On the actual slave we need to do conversion back to string before we send everything to C++ IoTSlave // TODO: Make it as array of string + //String[] arrCppArgs = { "D073D50241DA0000" }; String[] arrCppArgs = { "D073D5128E300000" }; String[] arrCppArgClasses = { "string" }; System.out.println("IoTSlave: Send request to create a driver object... "); @@ -229,20 +252,99 @@ public class IoTSlave { /** - * Send object fields + * Create new IoTSet for C++ + */ + //public void createNewIoTSetCpp() throws IOException { + public void createNewIoTSetCpp(String strObjFieldName) throws IOException { + + sendCommCode(IoTCommCode.CREATE_NEW_IOTSET); + System.out.println("IoTSlave: Creating new IoTSet..."); + //String strObjFieldName = "lb_addresses"; + System.out.println("IoTSlave: Send object field name: " + strObjFieldName); + sendString(strObjFieldName); recvAck(); + } + + + /** + * Get a IoTDeviceAddress object for C++ + */ + public void getDeviceIoTSetObjectCpp() throws IOException { + + sendCommCode(IoTCommCode.GET_DEVICE_IOTSET_OBJECT); + System.out.println("IoTSlave: Getting IoTDeviceAddress..."); + //String strHostAddress = "192.168.2.232"; + String strHostAddress = "192.168.2.126"; + sendString(strHostAddress); recvAck(); + int iSourcePort = 43583; + sendInteger(iSourcePort); recvAck(); + int iDestPort = 56700; + sendInteger(iDestPort); recvAck(); + boolean bSourceWildCard = false; + int iSourceWildCard = (bSourceWildCard ? 1 : 0); + sendInteger(iSourceWildCard); recvAck(); + boolean bDestWildCard = false; + int iDestWildCard = (bDestWildCard ? 1 : 0); + sendInteger(iDestWildCard); recvAck(); + System.out.println("IoTSlave: Send host address: " + strHostAddress); + } + + + /** + * Get a IoTSet content object for C++ + */ + public void getIoTSetObjectCpp() throws IOException { + + sendCommCode(IoTCommCode.GET_IOTSET_OBJECT); + System.out.println("IoTSlave: Getting IoTSet object content..."); + String strHostAddress = "localhost"; + String strDrvObjName = "LifxLightBulbLB2"; + String strDrvObjClsName = "LifxLightBulb"; + String strDrvObjIntfaceClsName = "LightBulb"; + String strDrvObjStubClsName = "LightBulbTest_Stub"; // Send a complete name with "_Stub" + int iRegPort = 30313; + int iStubPort = 55179; + int[] callbackPorts = { 58551 }; + // Send info + System.out.println("IoTSlave: Send host address: " + strHostAddress); + sendString(strHostAddress); recvAck(); + System.out.println("IoTSlave: Driver object name: " + strDrvObjName); + sendString(strDrvObjName); recvAck(); + System.out.println("IoTSlave: Driver object class name: " + strDrvObjClsName); + sendString(strDrvObjClsName); recvAck(); + System.out.println("IoTSlave: Driver object interface name: " + strDrvObjIntfaceClsName); + sendString(strDrvObjIntfaceClsName); recvAck(); + System.out.println("IoTSlave: Driver object skeleton class name: " + strDrvObjStubClsName); + sendString(strDrvObjStubClsName); recvAck(); + System.out.println("IoTSlave: Driver object registry port: " + iRegPort); + sendInteger(iRegPort); recvAck(); + System.out.println("IoTSlave: Driver object stub port: " + iStubPort); + sendInteger(iStubPort); recvAck(); + sendInteger(callbackPorts.length); recvAck(); + for(int i : callbackPorts) { + sendInteger(i); recvAck(); + } + + } + + + /** + * Reinitialize IoTSet field for C++ */ - private void sendFieldsCpp() throws IOException { + private void reinitializeIoTSetFieldCpp() throws IOException { - + System.out.println("IoTSlave: About to Reinitialize IoTSet field!"); + sendCommCode(IoTCommCode.REINITIALIZE_IOTSET_FIELD); + System.out.println("IoTSlave: Reinitialize IoTSet field!"); } /** - * Send object field types + * Invoke init() for C++ */ - private void sendFieldTypesCpp() throws IOException { + private void invokeInitMethodCpp() throws IOException { - + sendCommCode(IoTCommCode.INVOKE_INIT_METHOD); + System.out.println("IoTSlave: Invoke init method!"); } @@ -276,27 +378,51 @@ public class IoTSlave { System.out.println("Sending back string: " + strRecv); iotSlave.sendString(strRecv);*/ + // ========================================= + // Create IoTSlave for controller object! + int iPortMain =12346; + String strAddressMain = "localhost"; + String strObjNameMain = "Lifxtest"; + IoTSlave iotSlaveMain = new IoTSlave(); + iotSlaveMain.setServerSocketCpp(iPortMain); + // Run thread to spawn C++ IoTSlave + String strCmdMain = iotSlaveMain.constructCommand(strAddressMain, iPortMain, strObjNameMain); + iotSlaveMain.createCppThread(strCmdMain); + iotSlaveMain.connectCpp(); + System.out.println("IoTSlave: Connection established with main!"); + // First contact with C++ IoTSlave + System.out.println("IoTSlave: IoTSlave.o main is ready: " + iotSlaveMain.recvAck()); + + // ========================================= + // Create IoTSlave for driver object! int iPort =12345; String strAddress = "localhost"; - String strObjName = "Lifxtest"; - + String strObjName = "LifxLightBulbLB2"; IoTSlave iotSlave = new IoTSlave(); iotSlave.setServerSocketCpp(iPort); - // Run thread to spawn C++ IoTSlave - String strCmd = IoTSlave.constructCommand(strAddress, 12345, strObjName); + String strCmd = IoTSlave.constructCommand(strAddress, iPort, strObjName); IoTSlave.createCppThread(strCmd); iotSlave.connectCpp(); //RuntimeOutput.print("IoTSlave: Connection established!", BOOL_VERBOSE); System.out.println("IoTSlave: Connection established!"); // First contact with C++ IoTSlave System.out.println("IoTSlave: IoTSlave.o is ready: " + iotSlave.recvAck()); - iotSlave.createObjectCpp(); - - - iotSlave.endSessionCpp(); - + //iotSlave.createNewIoTSetCpp(); + iotSlave.createNewIoTSetCpp("lb_addresses"); + iotSlave.getDeviceIoTSetObjectCpp(); + iotSlave.reinitializeIoTSetFieldCpp(); + //iotSlave.endSessionCpp(); + + // ========================================= + // Continue with main object + iotSlaveMain.createMainObjectCpp(); + iotSlaveMain.createNewIoTSetCpp("lifx_light_bulb"); + iotSlaveMain.getIoTSetObjectCpp(); + iotSlaveMain.reinitializeIoTSetFieldCpp(); + iotSlaveMain.invokeInitMethodCpp(); + iotSlaveMain.endSessionCpp(); // Send message to create a main object /*commCode = IoTCommCode.CREATE_MAIN_OBJECT; diff --git a/iotjava/iotruntime/cpp/iotslave/LifxLightBulb.so b/iotjava/iotruntime/cpp/iotslave/LifxLightBulb.so index 8c4657d..209e8c0 100755 Binary files a/iotjava/iotruntime/cpp/iotslave/LifxLightBulb.so and b/iotjava/iotruntime/cpp/iotslave/LifxLightBulb.so differ diff --git a/iotjava/iotruntime/cpp/iotslave/Lifxtest.so b/iotjava/iotruntime/cpp/iotslave/Lifxtest.so index 9bf27d6..8827d7d 100755 Binary files a/iotjava/iotruntime/cpp/iotslave/Lifxtest.so and b/iotjava/iotruntime/cpp/iotslave/Lifxtest.so differ diff --git a/iotjava/iotruntime/cpp/iotslave/LightBulbTest_Stub.so b/iotjava/iotruntime/cpp/iotslave/LightBulbTest_Stub.so index 452ce36..83d8d6d 100755 Binary files a/iotjava/iotruntime/cpp/iotslave/LightBulbTest_Stub.so and b/iotjava/iotruntime/cpp/iotslave/LightBulbTest_Stub.so differ diff --git a/iotjava/iotruntime/cpp/iotslave/LightBulb_Skeleton.so b/iotjava/iotruntime/cpp/iotslave/LightBulb_Skeleton.so index 0f0e895..42ed05f 100755 Binary files a/iotjava/iotruntime/cpp/iotslave/LightBulb_Skeleton.so and b/iotjava/iotruntime/cpp/iotslave/LightBulb_Skeleton.so differ diff --git a/iotjava/iotruntime/cpp/iotslave/Makefile b/iotjava/iotruntime/cpp/iotslave/Makefile index a42bda5..48b8955 100755 --- a/iotjava/iotruntime/cpp/iotslave/Makefile +++ b/iotjava/iotruntime/cpp/iotslave/Makefile @@ -21,6 +21,10 @@ PHONY += run run: java IoTSlave +PHONY += readlog +readlog: + cat *.log + PHONY += clean clean: rm -rf *.class diff --git a/iotjava/iotruntime/cpp/iotslave/ObjectFactory.hpp b/iotjava/iotruntime/cpp/iotslave/ObjectFactory.hpp index ee54758..4ed07a7 100644 --- a/iotjava/iotruntime/cpp/iotslave/ObjectFactory.hpp +++ b/iotjava/iotruntime/cpp/iotslave/ObjectFactory.hpp @@ -1,11 +1,30 @@ #include "LifxLightBulb.cpp" #include "LightBulb_Skeleton.cpp" #include "LightBulbTest_Stub.cpp" +#include "IoTSet.hpp" -typedef void* create_t(string className, void** params); -typedef void destroy_t(void*); +//typedef void* create_t(string className, void** params); +//typedef void destroy_t(void*); + +// Transferring members of IoTSet into IoTSet +IoTSet* createDeviceAddressSet(unordered_set* iotSet) { + + unordered_set* devSet = new unordered_set(); + //for (auto itr = iotSet->begin(); itr != iotSet->end(); ++itr) { + for (unordered_set::const_iterator itr = iotSet->begin(); itr != iotSet->end(); ++itr) { + IoTDeviceAddress* deviceAddress = (IoTDeviceAddress*) *itr; + devSet->insert(deviceAddress); + } + IoTSet* iotDevSet = new IoTSet(devSet); + + delete iotSet; + return iotDevSet; +} + + +/* // External creator/destroyer extern "C" void* create(string className, void** params) { @@ -47,39 +66,5 @@ extern "C" void destroy(string className, void* ob) { } } } - -/*typedef LifxLightBulb* create_t(IoTSet* _devAddress, string macAddress); -typedef void destroy_t(LifxLightBulb*); - -// External creator/destroyer -extern "C" LifxLightBulb* create(IoTSet* _devAddress, string macAddress) { - return new LifxLightBulb(_devAddress, macAddress); -} - -extern "C" void destroy(LifxLightBulb* t) { - delete t; -}*/ - -//typedef LightBulb_Skeleton* create_t(LightBulb *_mainObj, string _callbackAddress, int _port); -//typedef void destroy_t(LightBulb_Skeleton*); - -/*extern "C" LightBulb_Skeleton* create(LightBulb *_mainObj, string _callbackAddress, int _port) { - return new LightBulb_Skeleton(_mainObj, _callbackAddress, _port); -} - -extern "C" void destroy(LightBulb_Skeleton* t) { - delete t; -}*/ - -//typedef LightBulbTest_Stub* create_t(int _port, const char* _skeletonAddress, string _callbackAddress, int _rev, bool* _bResult, vector _ports); -//typedef void destroy_t(LightBulbTest_Stub*); - -/*extern "C" LightBulbTest_Stub* create(int _port, const char* _skeletonAddress, string _callbackAddress, int _rev, bool* _bResult, vector _ports) { - return new LightBulbTest_Stub(_port, _skeletonAddress, _callbackAddress, _rev, _bResult, _ports); -} - -extern "C" void destroy(LightBulbTest_Stub* t) { - delete t; -}*/ - +*/ diff --git a/iotjava/iotruntime/cpp/setrelation/IoTRelation.hpp b/iotjava/iotruntime/cpp/setrelation/IoTRelation.hpp index 77c8ff4..673abc4 100644 --- a/iotjava/iotruntime/cpp/setrelation/IoTRelation.hpp +++ b/iotjava/iotruntime/cpp/setrelation/IoTRelation.hpp @@ -15,10 +15,11 @@ using namespace std; template class IoTRelation { private: - unordered_multimap rel; + const unordered_multimap* rel; public: IoTRelation(); - IoTRelation(unordered_multimap const& s); + //IoTRelation(unordered_multimap const& s); + IoTRelation(const unordered_multimap* s); ~IoTRelation(); public: typename unordered_multimap::const_iterator find(const K& k); // Find the object based on key @@ -46,7 +47,8 @@ IoTRelation::IoTRelation() { * Useful constructor */ template -IoTRelation::IoTRelation(const unordered_multimap& r) { +//IoTRelation::IoTRelation(const unordered_multimap& r) { +IoTRelation::IoTRelation(const unordered_multimap* r) { rel = r; } @@ -67,7 +69,7 @@ IoTRelation::~IoTRelation() { template typename unordered_multimap::const_iterator IoTRelation::find(const K& k) { - return rel.find(k); + return rel->find(k); } @@ -77,7 +79,7 @@ typename unordered_multimap::const_iterator IoTRelation::find(const K& template typename unordered_multimap::const_iterator IoTRelation::begin() { - return rel.begin(); + return rel->begin(); } @@ -87,7 +89,7 @@ typename unordered_multimap::const_iterator IoTRelation::begin() { template typename unordered_multimap::const_iterator IoTRelation::end() { - return rel.end(); + return rel->end(); } @@ -99,7 +101,7 @@ std::pair::const_iterator, typename unordered_multimap::const_iterator> IoTRelation::equal_range(const K& k) { - return rel.equal_range(k); + return rel->equal_range(k); } @@ -109,7 +111,7 @@ std::pair::const_iterator, template int IoTRelation::size() { - return rel.size(); + return rel->size(); } @@ -119,7 +121,7 @@ int IoTRelation::size() { template unordered_multimap IoTRelation::values() { - return new unordered_multimap(rel); + return new unordered_multimap(*rel); } #endif diff --git a/iotjava/iotruntime/cpp/setrelation/IoTSet.hpp b/iotjava/iotruntime/cpp/setrelation/IoTSet.hpp index 78d8c63..60f34f7 100644 --- a/iotjava/iotruntime/cpp/setrelation/IoTSet.hpp +++ b/iotjava/iotruntime/cpp/setrelation/IoTSet.hpp @@ -15,10 +15,10 @@ using namespace std; template class IoTSet { private: - unordered_set set; + const unordered_set* set; public: IoTSet(); - IoTSet(unordered_set const& s); + IoTSet(const unordered_set* s); ~IoTSet(); public: typename unordered_set::const_iterator find(const T& k); // Find the object @@ -43,7 +43,7 @@ IoTSet::IoTSet() { * Useful constructor */ template -IoTSet::IoTSet(const unordered_set& s) { +IoTSet::IoTSet(const unordered_set* s) { set = s; } @@ -64,7 +64,7 @@ IoTSet::~IoTSet() { template typename unordered_set::const_iterator IoTSet::find(const T& k) { - return set.find(k); + return set->find(k); } @@ -74,7 +74,7 @@ typename unordered_set::const_iterator IoTSet::find(const T& k) { template typename unordered_set::const_iterator IoTSet::begin() { - return set.begin(); + return set->begin(); } @@ -84,7 +84,7 @@ typename unordered_set::const_iterator IoTSet::begin() { template typename unordered_set::const_iterator IoTSet::end() { - return set.end(); + return set->end(); } @@ -94,7 +94,7 @@ typename unordered_set::const_iterator IoTSet::end() { template int IoTSet::size() { - return set.size(); + return set->size(); } @@ -104,7 +104,7 @@ int IoTSet::size() { template unordered_set* IoTSet::values() { - return new unordered_set(set); + return new unordered_set(*set); } #endif