#include "LifxLightBulb.cpp"
#include "Iterator.hpp"
-Lifxtest::Lifxtest() {
+// External create, destroy, and init functions
+extern "C" void* createLifxtest(void** params) {
+ // Arguments: IoTSet<void*>* lifx_light_bulb
+ return new Lifxtest((IoTSet<void*>*) 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<LightBulbTest*> _lifx_light_bulb) {
+Lifxtest::Lifxtest() {
+ log.open("Lifxtest_object_cpp.log");
+ log << "lifx_light_bulb initialized!" << endl;
+}
+Lifxtest::Lifxtest(IoTSet<void*>* _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<LightBulbTest*>*) args[0];
- lifx_light_bulb = *(IoTSet<LightBulb*>*) args[0];
+ log.open("Lifxtest_object_cpp.log");
+ lifx_light_bulb = (IoTSet<void*>*) args[0];
+ log << "lifx_light_bulb initialized!" << endl;
}
+
Lifxtest::~Lifxtest() {
}
void Lifxtest::init() {
- //unordered_set<LightBulbTest*>* bulbSet = lifx_light_bulb.values();
- unordered_set<LightBulb*>* bulbSet = lifx_light_bulb.values();
- //for(LightBulbTest* lifx : *bulbSet) {
- for(LightBulb* lifx : *bulbSet) {
-
+ unordered_set<void*>* bulbSet = lifx_light_bulb->values();
+ //for (unordered_set<void*>::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<IoTDeviceAddress*>* myset1 = new unordered_set<IoTDeviceAddress*>();
+ unordered_set<void*>* myset1 = new unordered_set<void*>();
myset1->insert(devAddress1);
- IoTSet<IoTDeviceAddress*>* setDevAddress1 = new IoTSet<IoTDeviceAddress*>(*myset1);
+ IoTSet<void*>* setDevAddress1 = new IoTSet<void*>(myset1);
LifxLightBulb *llb1 = new LifxLightBulb(setDevAddress1, macAddress1);
//cout << "Generated LifxLightBulb object!" << endl;
string macAddress2 = "D073D50241DA0000";
string devIPAddress2 = "192.168.2.232";
IoTDeviceAddress* devAddress2 = new IoTDeviceAddress(devIPAddress2, 12346, 56700, false, false);
- unordered_set<IoTDeviceAddress*>* myset2 = new unordered_set<IoTDeviceAddress*>();
+ unordered_set<void*>* myset2 = new unordered_set<void*>();
myset2->insert(devAddress2);
- IoTSet<IoTDeviceAddress*>* setDevAddress2 = new IoTSet<IoTDeviceAddress*>(*myset2);
+ IoTSet<void*>* setDevAddress2 = new IoTSet<void*>(myset2);
LifxLightBulb *llb2 = new LifxLightBulb(setDevAddress2, macAddress2);
// Set of lightbulbs
- unordered_set<LightBulb*>* setLb = new unordered_set<LightBulb*>();
+ unordered_set<void*>* setLb = new unordered_set<void*>();
setLb->insert(llb1);
setLb->insert(llb2);
- IoTSet<LightBulb*>* lbSet = new IoTSet<LightBulb*>(*setLb);
+ IoTSet<void*>* lbSet = new IoTSet<void*>(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;
delete setDevAddress2;
delete setLb;
delete lbSet;
- //delete llb1;
- //delete llb2;
return 0;
}
-
+*/
#ifndef _LIFXTEST_HPP__
#define _LIFXTEST_HPP__
#include <iostream>
+#include <fstream>
#include "IoTSet.hpp"
#include "LightBulb.hpp"
#include "LightBulbTest.hpp"
+
class Lifxtest {
private:
// IoTSet
- //IoTSet<LightBulbTest*> lifx_light_bulb;
- IoTSet<LightBulb*> lifx_light_bulb;
+ IoTSet<void*>* lifx_light_bulb;
+
+ ofstream log;
public:
Lifxtest();
- //Lifxtest(IoTSet<LightBulbTest*> _lifx_light_bulb);
+ Lifxtest(IoTSet<void*>* _lifx_light_bulb);
Lifxtest(void** args);
~Lifxtest();
void init();
#include <iostream>
+#include <thread>
#include "LightBulbTest_Stub.hpp"
-// External creator/destroyer
-/*extern "C" LightBulbTest_Stub* create(int _port, const char* _skeletonAddress, string _callbackAddress, int _rev, bool* _bResult, vector<int> _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<int> _ports
+ return new LightBulbTest_Stub(*((int*) params[0]), ((string*) params[1])->c_str(), *((string*) params[2]), *((int*) params[3]), (bool*) params[4], *((vector<int>*) 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<int> 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;
+}
#ifndef _LIGHTBULBTEST_STUB_HPP__
#define _LIGHTBULBTEST_STUB_HPP__
#include <iostream>
+#include <fstream>
#include "LightBulbTest.hpp"
using namespace std;
vector<int> ports;
const static int objectId = 0;
-
+
+ ofstream log;
public:
LightBulbTest_Stub(int _port, const char* _skeletonAddress, string _callbackAddress, int _rev, bool* _bResult, vector<int> _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() {
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) {
};
-//typedef LightBulbTest_Stub* create_t(int _port, const char* _skeletonAddress, string _callbackAddress, int _rev, bool* _bResult, vector<int> _ports);
-//typedef void destroy_t(LightBulbTest_Stub*);
#endif
$(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/
using namespace std;
-// External creator/destroyer
-/*extern "C" LifxLightBulb* create(IoTSet<IoTDeviceAddress*>* _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<IoTDeviceAddress*>* _devAddress, string macAddress
+ return new LifxLightBulb((IoTSet<void*>*) 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() {
}
-LifxLightBulb::LifxLightBulb(IoTSet<IoTDeviceAddress*>* _devAddress, string macAddress) {
+// Driver constructor always gets a pointer to device address, trailed by class arguments of generic type
+LifxLightBulb::LifxLightBulb(IoTSet<void*>* _devAddress, string macAddress) {
// Initialize macAddress
char tmpMacAddress[16];
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;
}
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) {
if (didAlreadyInit.exchange(true))
return;
- unordered_set<IoTDeviceAddress*>::const_iterator itr = lb_addresses->begin();
- IoTDeviceAddress* deviceAddress = *itr;
- cout << "Address: " << deviceAddress->getAddress() << endl;
+ unordered_set<void*>::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
thread th1 (&LifxLightBulb::workerFunction, this, this);
th1.detach();
- cout << "Initialized LifxLightBulb!" << endl;
+ //cout << "Initialized LifxLightBulb!" << endl;
+ log << "Initialized LifxLightBulb!" << endl;
+ log.close();
}
}
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;
break;
default:
- cout << "unknown packet Type" << endl;
+ break;
+ //cout << "unknown packet Type" << endl;
}
// Avoid memory leaks
delete payloadBytes;
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;
}
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));
}
}
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;
}
#ifndef _LIFXLIGHTBULB_HPP__
#define _LIFXLIGHTBULB_HPP__
#include <iostream>
+#include <fstream>
#include <atomic>
#include <mutex>
#include <thread>
bool stateDidChange = false;
// Device address
- IoTSet<IoTDeviceAddress*>* lb_addresses;
+ IoTSet<void*>* lb_addresses; // IoTSet<IoTDeviceAddress*>* lb_addresses
+ // Logging
+ ofstream log;
public:
// Constructor
LifxLightBulb();
- LifxLightBulb(IoTSet<IoTDeviceAddress*>* _devAddress, string macAddress);
+ //LifxLightBulb(IoTSet<IoTDeviceAddress*>* _devAddress, string macAddress);
+ LifxLightBulb(IoTSet<void*>* _devAddress, string macAddress);
~LifxLightBulb();
// Initialize the lightbulb
void init();
void handleLightStateMessageReceived(char* payloadData);
};
-//typedef LifxLightBulb* create_t(IoTSet<IoTDeviceAddress*>* _devAddress, string macAddress);
-//typedef void destroy_t(LifxLightBulb*);
#endif
#include <iostream>
#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 <unordered_set>
+#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<void*>* myset1 = new unordered_set<void*>();
+ myset1->insert(devAddress1);
+ IoTSet<void*>* setDevAddress1 = new IoTSet<void*>(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;
+}
#ifndef _LIGHTBULB_SKELETON_HPP__
#define _LIGHTBULB_SKELETON_HPP__
#include <iostream>
+#include <fstream>
#include "LightBulb.hpp"
#include <vector>
const static int object0Id = 0; //LightBulbSmart
static set<int> 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();
}
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;
throw exception();
}
}
+ log.close();
}
};
set<int> 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
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/
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++/
#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;
delete socket;
socket = NULL;
}
+ /*if (objMainCls != NULL) {
+ delete objMainCls;
+ objMainCls = NULL;
+ }
+ if (objSkelCls != NULL) {
+ delete objSkelCls;
+ objSkelCls = NULL;
+ }*/
+ for (IoTSet<void*>* iotset : vecIoTSet) {
+ delete iotset;
+ iotset = NULL;
+ }
closeFile();
}
}
-void IoTSlave::instantiateObject(string objectClassName) {
+void IoTSlave::getObjectHandler(string objectClassName) {
// Object handling
string strObj = FILEPATH + objectClassName + SOEXT;
}
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';
}
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';
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<vecIoTSet.size(); i++) {
+ params[i] = vecIoTSet[i]; // Just the first object is taken in this case
+ }
+ writeToFile("Vector IoTSet size: " + to_string(vecIoTSet.size()));
+ objMainCls = create_object(params);
+ writeToFile("Object created for " + mainObjectName);
+ init_object(objMainCls);
+ //thread th1 (&IoTSlave::runInitObject, this, this);
+ //th1.detach();
+ //thread th1 (&IoTSlave::runInitObject, this, this);
+ //th1.join();
+ writeToFile("Initialized object " + mainObjectName);
+}
+
+
+// Instantiate driver object!
+// Use handler obtained by getObjectHandler() and instantiate object!
+void IoTSlave::instantiateDriverObject() {
+
+ // IoTDeviceAddress + other arguments
+ int paramSize = vecIoTSet.size() + args.size();
+ void* params[paramSize];
+ for(int i=0; i<vecIoTSet.size(); i++) {
+ params[i] = vecIoTSet[i]; // Just the first object is taken in this case
+ }
+ writeToFile("Vector IoTSet size: " + to_string(vecIoTSet.size()));
+ writeToFile("Arg size: " + to_string(args.size()));
+ int countArg = vecIoTSet.size(); // Start from after the address set
+ // Iterate over arguments
+ for(int i=0; i<args.size(); i++) {
+ params[countArg] = getObjectConverted(params[countArg], args[i], argClasses[i]);
+ countArg++;
+ }
+ objMainCls = create_object(params);
+ // Delete unused object after conversion and instantiation
+ for(int i=1; i<paramSize; i++) {
+ if (argClasses[i-1].compare(STRINGCLASS) == 0) {
+ delete (string*) params[i];
+ } else if (argClasses[i-1].compare(INTCLASS) == 0)
+ delete (int*) params[i];
+ }
+ writeToFile("Object created for " + objectClassName);
+}
+
+
+// Use handler obtained by getObjectHandler() and instantiate skeleton object!
+void IoTSlave::instantiateSkelObject() {
+
+ void* params[SKELPARAMSIZE];
+ params[0] = objMainCls;
+ string callbackAddress = LOCALHOST;
+ params[1] = &callbackAddress;
+ params[2] = &objectStubPort;
+ writeToFile("Skeleton Object " + objectSkelClass + " created for " + objectClassName);
+ // After this, this slave needs to be killed using "pkill IoTSlave" because it's waiting in an infinite while-loop
+ objSkelCls = create_object(params);
+}
+
+
+// Use handler obtained by getObjectHandler() and instantiate stub object!
+void IoTSlave::instantiateStubObject() {
+
+ void* params[STUBPARAMSIZE];
+ params[0] = &objectStubPort;
+ params[1] = &hostAddress;
+ string callbackAddress = LOCALHOST;
+ params[2] = &callbackAddress;
+ int rev = 0;
+ params[3] = &rev;
+ bool result = false;
+ params[4] = &result;
+ params[5] = &ports;
+ writeToFile("Stub Object " + objectStubClass + " created for " + objectClassName);
+ objStubCls = create_object(params);
}
void IoTSlave::createObject() {
writeToFile("Creating a driver object now...");
- sendAck();
// Receiving object info
objectName = recvString(); sendAck();
- writeToFile("Driver object name: " + objectName);
+ writeToFile("=> 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<void*>();
+ 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<string,void*>::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<void*>(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();
}
// 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
}
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:
break;
case GET_IOTSET_OBJECT:
- //getIoTSetObject();
+ getIoTSetObject();
break;
case GET_IOTRELATION_FIRST_OBJECT:
break;
case REINITIALIZE_IOTSET_FIELD:
- //reinitializeIoTSetField();
+ reinitializeIoTSetField();
break;
case REINITIALIZE_IOTRELATION_FIELD:
break;
case INVOKE_INIT_METHOD:
- //invokeInitMethod();
+ invokeInitMethod();
break;
case END_SESSION:
int serverPort = atoi(servPort);
string strObjName = argv[3];
IoTSlave *iotSlave = new IoTSlave(serverAddress, serverPort, strObjName);
- //iotSlave->sendInteger(123455);
iotSlave->sendAck();
iotSlave->commIoTMaster();
#include <iostream>
#include <fstream>
#include <vector>
+#include <thread>
#include <dlfcn.h> // 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"
};
+
+// 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
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<int> ports; // Now used to contain callback ports
+ string objectFieldName; // Field name that is going to be initialized with IoTSet or IoTRelation
+ unordered_set<void*>* isetObject; // Set of object
+ IoTSet<void*>* iotsetObject; // IoTSet of object
+ vector<IoTSet<void*>*> vecIoTSet; // IoTSet of object
TCPSocket* socket;
- ofstream log; // Log the messages
- vector<string> args; // Hold the arguments for constructor (in string format)
- vector<string> argClasses; // Hold the argument classes
+ ofstream log; // Log the messages
+ vector<string> args; // Hold the arguments for constructor (in string format)
+ vector<string> 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<string, void*> mapObjNameStub; // Mapping between object name and stub
// Object handlers
create_t* create_object;
destroy_t* destroy_object;
+ init_t* init_object;
public:
// Constructors
void createMainObject(); // Create main object
void createNewIoTSet();
void getDeviceIoTSetObject();
+ void reinitializeIoTSetField();
+ void getIoTSetObject();
+ void invokeInitMethod();
private:
// Private helper functions
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
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
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 = "./";
*/
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;
}
}
+ /**
+ * 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";
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... ");
/**
- * 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!");
}
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;
run:
java IoTSlave
+PHONY += readlog
+readlog:
+ cat *.log
+
PHONY += clean
clean:
rm -rf *.class
#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<void*> into IoTSet<IoTDeviceAddress*>
+IoTSet<IoTDeviceAddress*>* createDeviceAddressSet(unordered_set<void*>* iotSet) {
+
+ unordered_set<IoTDeviceAddress*>* devSet = new unordered_set<IoTDeviceAddress*>();
+ //for (auto itr = iotSet->begin(); itr != iotSet->end(); ++itr) {
+ for (unordered_set<void*>::const_iterator itr = iotSet->begin(); itr != iotSet->end(); ++itr) {
+ IoTDeviceAddress* deviceAddress = (IoTDeviceAddress*) *itr;
+ devSet->insert(deviceAddress);
+ }
+ IoTSet<IoTDeviceAddress*>* iotDevSet = new IoTSet<IoTDeviceAddress*>(devSet);
+
+ delete iotSet;
+ return iotDevSet;
+}
+
+
+/*
// External creator/destroyer
extern "C" void* create(string className, void** params) {
}
}
}
-
-/*typedef LifxLightBulb* create_t(IoTSet<IoTDeviceAddress*>* _devAddress, string macAddress);
-typedef void destroy_t(LifxLightBulb*);
-
-// External creator/destroyer
-extern "C" LifxLightBulb* create(IoTSet<IoTDeviceAddress*>* _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<int> _ports);
-//typedef void destroy_t(LightBulbTest_Stub*);
-
-/*extern "C" LightBulbTest_Stub* create(int _port, const char* _skeletonAddress, string _callbackAddress, int _rev, bool* _bResult, vector<int> _ports) {
- return new LightBulbTest_Stub(_port, _skeletonAddress, _callbackAddress, _rev, _bResult, _ports);
-}
-
-extern "C" void destroy(LightBulbTest_Stub* t) {
- delete t;
-}*/
-
+*/
template <class K,class V>
class IoTRelation {
private:
- unordered_multimap<K,V> rel;
+ const unordered_multimap<K,V>* rel;
public:
IoTRelation();
- IoTRelation(unordered_multimap<K,V> const& s);
+ //IoTRelation(unordered_multimap<K,V> const& s);
+ IoTRelation(const unordered_multimap<K,V>* s);
~IoTRelation();
public:
typename unordered_multimap<K,V>::const_iterator find(const K& k); // Find the object based on key
* Useful constructor
*/
template <class K,class V>
-IoTRelation<K,V>::IoTRelation(const unordered_multimap<K,V>& r) {
+//IoTRelation<K,V>::IoTRelation(const unordered_multimap<K,V>& r) {
+IoTRelation<K,V>::IoTRelation(const unordered_multimap<K,V>* r) {
rel = r;
}
template <class K,class V>
typename unordered_multimap<K,V>::const_iterator IoTRelation<K,V>::find(const K& k) {
- return rel.find(k);
+ return rel->find(k);
}
template <class K,class V>
typename unordered_multimap<K,V>::const_iterator IoTRelation<K,V>::begin() {
- return rel.begin();
+ return rel->begin();
}
template <class K,class V>
typename unordered_multimap<K,V>::const_iterator IoTRelation<K,V>::end() {
- return rel.end();
+ return rel->end();
}
typename unordered_multimap<K,V>::const_iterator>
IoTRelation<K,V>::equal_range(const K& k) {
- return rel.equal_range(k);
+ return rel->equal_range(k);
}
template <class K,class V>
int IoTRelation<K,V>::size() {
- return rel.size();
+ return rel->size();
}
template <class K,class V>
unordered_multimap<K,V> IoTRelation<K,V>::values() {
- return new unordered_multimap<K,V>(rel);
+ return new unordered_multimap<K,V>(*rel);
}
#endif
template <class T>
class IoTSet {
private:
- unordered_set<T> set;
+ const unordered_set<T>* set;
public:
IoTSet();
- IoTSet(unordered_set<T> const& s);
+ IoTSet(const unordered_set<T>* s);
~IoTSet();
public:
typename unordered_set<T>::const_iterator find(const T& k); // Find the object
* Useful constructor
*/
template <class T>
-IoTSet<T>::IoTSet(const unordered_set<T>& s) {
+IoTSet<T>::IoTSet(const unordered_set<T>* s) {
set = s;
}
template <class T>
typename unordered_set<T>::const_iterator IoTSet<T>::find(const T& k) {
- return set.find(k);
+ return set->find(k);
}
template <class T>
typename unordered_set<T>::const_iterator IoTSet<T>::begin() {
- return set.begin();
+ return set->begin();
}
template <class T>
typename unordered_set<T>::const_iterator IoTSet<T>::end() {
- return set.end();
+ return set->end();
}
template <class T>
int IoTSet<T>::size() {
- return set.size();
+ return set->size();
}
template <class T>
unordered_set<T>* IoTSet<T>::values() {
- return new unordered_set<T>(set);
+ return new unordered_set<T>(*set);
}
#endif