diff --git a/db/iotdb.txt b/db/iotdb.txt index 00a0fe0..da41e17 100644 --- a/db/iotdb.txt +++ b/db/iotdb.txt @@ -1,265 +1,5 @@ sensors: temp motion door keychain devices: bulb outlet -states: 5 Detected Closed Authenticated ON OFF +states: 5 Detected Closed Authenticated OFF OFF history -timestamp sensor/device value comment -1553474164971 BULB OFF -1553474164972 OUTLET OFF -1553474164973 BULB ON -1553474164974 OUTLET ON -1553474166079 MOTION Detected -1553474168079 MOTION Detected -1553474168078 BULB OFF -1553474168080 BULB ON -1553474170079 MOTION Detected -1553474171079 MOTION Detected -1553474171080 DOOR Open User left the house. -1553474172082 BULB OFF -1553474172084 DOOR Closed -1553474174981 OUTLET OFF -1553474188085 KEY Authenticated -1553474188087 DOOR Open -1553474189088 BULB ON -1553474189090 DOOR Closed -1553474191089 BULB OFF -1553474191091 MOTION Detected User entered the house. -1553474191090 BULB ON -1553474192091 MOTION Detected -1553474193091 MOTION Detected -1553474194985 OUTLET ON -1553474195090 MOTION Detected -1553474196090 MOTION Detected -1553474197090 MOTION Detected -1553474197090 DOOR Open User left the house. -1553474198090 MOTION Detected User entered the house. -1553474198092 BULB OFF -1553474198093 DOOR Closed -1553474199987 OUTLET OFF -1553474214095 DOOR Open -1553474215097 DOOR Closed -1553474216099 MOTION Detected User entered the house. -1553474218099 MOTION Detected -1553474223099 MOTION Detected -1553474225099 MOTION Detected -1553474226099 MOTION Detected -1553474230099 MOTION Detected -1553474233099 MOTION Detected -1553474234099 MOTION Detected -1553474236098 MOTION Detected -1553474239098 MOTION Detected -1553474239098 DOOR Open User left the house. -1553474240098 MOTION Detected User entered the house. -1553474240099 DOOR Closed -1553474241101 MOTION Detected User entered the house. -1553474242101 MOTION Detected -1553474243101 MOTION Detected -1553474244101 MOTION Detected -1553474245102 MOTION Detected -1553474246102 MOTION Detected -1553474247102 MOTION Detected -1553474248102 KEY Authenticated -1553474248102 MOTION Detected -1553474248103 DOOR Open User left the house. -1553474249102 MOTION Detected User entered the house. -1553474249104 BULB ON -1553474249106 DOOR Closed -1553474250108 MOTION Detected User entered the house. -1553474252108 MOTION Detected -1553474254107 BULB OFF -1553474256107 MOTION Detected -1553474256107 BULB ON -1553474257107 DOOR Open User left the house. -1553474257107 MOTION Detected User entered the house. -1553474258110 BULB OFF -1553474258111 DOOR Closed -1553474266112 KEY Authenticated -1553474266114 DOOR Open -1553474267115 BULB ON -1553474267117 DOOR Closed -1553474268118 MOTION Detected User entered the house. -1553474269999 OUTLET ON -1553474270118 BULB OFF -1553474271118 MOTION Detected -1553474271118 BULB ON -1553474272118 MOTION Detected -1553474273118 MOTION Detected -1553474274118 MOTION Detected -1553474275119 DOOR Open User left the house. -1553474276119 MOTION Detected User entered the house. -1553474276121 BULB OFF -1553474276122 DOOR Closed -1553474280004 OUTLET OFF -1553474284124 KEY Authenticated -1553474284125 DOOR Open -1553474285125 BULB ON -1553474285127 DOOR Closed -1553474286128 MOTION Detected User entered the house. -1553474287128 MOTION Detected -1553474289128 BULB OFF -1553474290005 OUTLET ON -1553474290128 MOTION Detected -1553474290128 BULB ON -1553474292128 MOTION Detected -1553474293128 MOTION Detected -1553474293128 DOOR Open User left the house. -1553474294130 BULB OFF -1553474294132 DOOR Closed -1553474295007 OUTLET OFF -1553474302135 DOOR Open -1553474303136 DOOR Closed -1553474304138 MOTION Detected User entered the house. -1553474305138 MOTION Detected -1553474310138 MOTION Detected -1553474311138 DOOR Open User left the house. -1553474312138 MOTION Detected User entered the house. -1553474312140 DOOR Closed -1553474314141 MOTION Detected User entered the house. -1553474315140 MOTION Detected -1553474316140 MOTION Detected -1553474317140 MOTION Detected -1553474319140 MOTION Detected -1553474320140 KEY Authenticated -1553474320142 DOOR Open User left the house. -1553474321144 BULB ON -1553474321145 DOOR Closed -1553474322146 MOTION Detected User entered the house. -1553474323147 MOTION Detected -1553474324147 MOTION Detected -1553474326148 MOTION Detected -1553474326147 BULB OFF -1553474326148 BULB ON -1553474328148 MOTION Detected -1553474329148 MOTION Detected -1553474330015 OUTLET ON -1553474331148 MOTION Detected -1553474333148 MOTION Detected -1553474334148 MOTION Detected -1553474336147 BULB OFF -1553474337147 MOTION Detected -1553474337147 BULB ON -1553474338147 MOTION Detected -1553474340147 MOTION Detected -1553474342147 BULB OFF -1553474345019 OUTLET OFF -1553474345148 DOOR Open User left the house. -1553474346148 MOTION Detected User entered the house. -1553474346147 BULB ON -1553474346149 BULB OFF -1553474346151 DOOR Closed -1553474354153 DOOR Open -1553474355154 DOOR Closed -1553474356156 MOTION Detected User entered the house. -1553474357156 MOTION Detected -1553474359156 MOTION Detected -1553474361156 MOTION Detected -1553474362156 MOTION Detected -1553474363156 DOOR Open User left the house. -1553474363156 MOTION Detected User entered the house. -1553474364156 MOTION Detected -1553474364158 DOOR Closed -1553474365161 MOTION Detected User entered the house. -1553474366161 MOTION Detected -1553474369161 MOTION Detected -1553474370161 MOTION Detected -1553474371161 MOTION Detected -1553474372160 DOOR Open User left the house. -1553474373161 MOTION Detected User entered the house. -1553474373162 DOOR Closed -1553474374164 MOTION Detected User entered the house. -1553474376163 MOTION Detected -1553474377163 MOTION Detected -1553474378163 MOTION Detected -1553474380163 MOTION Detected -1553474381163 KEY Authenticated -1553474381164 DOOR Open User left the house. -1553474382163 MOTION Detected User entered the house. -1553474382166 BULB ON -1553474382168 DOOR Closed -1553474384168 BULB OFF -1553474384169 MOTION Detected User entered the house. -1553474384170 BULB ON -1553474385027 OUTLET ON -1553474385169 MOTION Detected -1553474386169 MOTION Detected -1553474388169 BULB OFF -1553474389169 MOTION Detected -1553474389169 BULB ON -1553474390169 DOOR Open User left the house. -1553474391170 BULB OFF -1553474391171 BULB OFF -1553474391173 DOOR Closed -1553474395029 OUTLET OFF -1553474399176 KEY Authenticated -1553474399177 DOOR Open -1553474400179 BULB ON -1553474400180 DOOR Closed -1553474402181 BULB OFF -1553474402182 MOTION Detected User entered the house. -1553474402182 BULB ON -1553474403182 MOTION Detected -1553474405034 OUTLET ON -1553474405182 MOTION Detected -1553474406182 MOTION Detected -1553474408182 DOOR Open User left the house. -1553474408181 BULB OFF -1553474409182 MOTION Detected User entered the house. -1553474409181 BULB ON -1553474409183 BULB OFF -1553474409186 DOOR Closed -1553474410035 OUTLET OFF -1553474417186 DOOR Open -1553474418188 DOOR Closed -1553474421190 MOTION Detected User entered the house. -1553474422190 MOTION Detected -1553474423190 MOTION Detected -1553474426191 KEY Authenticated -1553474426191 MOTION Detected -1553474426192 DOOR Open User left the house. -1553474427193 BULB ON -1553474427196 DOOR Closed -1553474428197 MOTION Detected User entered the house. -1553474429197 MOTION Detected -1553474431197 MOTION Detected -1553474433197 BULB OFF -1553474434197 MOTION Detected -1553474434197 BULB ON -1553474435197 MOTION Detected -1553474437196 MOTION Detected -1553474438197 MOTION Detected -1553474440197 BULB OFF -1553474441197 MOTION Detected -1553474441197 BULB ON -1553474442196 MOTION Detected -1553474443197 DOOR Open User left the house. -1553474444197 MOTION Detected User entered the house. -1553474444199 BULB OFF -1553474444200 DOOR Closed -1553474452202 DOOR Open -1553474453204 DOOR Closed -1553474454205 MOTION Detected User entered the house. -1553474455206 MOTION Detected -1553474456207 MOTION Detected -1553474457207 MOTION Detected -1553474458206 MOTION Detected -1553474459206 MOTION Detected -1553474462206 MOTION Detected -1553474463207 MOTION Detected -1553474465206 MOTION Detected -1553474467206 MOTION Detected -1553474468206 MOTION Detected -1553474469206 DOOR Open User left the house. -1553474469207 MOTION Detected User entered the house. -1553474470206 MOTION Detected -1553474470209 DOOR Closed -1553474471211 MOTION Detected User entered the house. -1553474474211 MOTION Detected -1553474475210 MOTION Detected -1553474482210 MOTION Detected -1553474483210 MOTION Detected -1553474484210 MOTION Detected -1553474486210 MOTION Detected -1553474488210 MOTION Detected -1553474489210 MOTION Detected -1553474490210 MOTION Detected -1553474491210 MOTION Detected -1553474496270 BULB ON +BerkleyStamp LamportStamp sensor/device value comment diff --git a/devices/devices.cpp b/devices/devices.cpp index efcaf54..506c687 100644 --- a/devices/devices.cpp +++ b/devices/devices.cpp @@ -44,6 +44,9 @@ int outletid = ERR_SENSOR_NOT_REGISTERED; /* Berkeley Offset */ int offset = 0; +/* Lamport Time (Atomic Counter) */ +atomic lamport; + /* Mutex locks for synchronization */ LOCK bulb_lock; LOCK outlet_lock; @@ -99,10 +102,14 @@ string getIPAddress() * @params: device id * @returns: device_id and state */ -long long query_state(int device_id) +long long query_state(int device_id, int r_lamport) { int res = -1; + /* Adjust Lamport's Logical Clock */ + if (lamport++ <= r_lamport) + lamport = r_lamport + 1; + /* Check the device id */ if (device_id == bulbid) { @@ -135,8 +142,12 @@ long long query_state(int device_id) * @params: device id * @returns: device_id and timestamp */ -long long request_timestamp(int device_id) -{ +long long request_timestamp(int device_id, int r_lamport) +{ + /* Adjust Lamport's Logical Clock */ + if (lamport++ <= r_lamport) + lamport = r_lamport + 1; + long long res = 0; if (device_id == bulbid || device_id == outletid) { @@ -152,8 +163,12 @@ long long request_timestamp(int device_id) * @params: device id * @returns: device_id and offset */ -long long set_offset(int device_id, int l_offset) +long long set_offset(int device_id, int l_offset, int r_lamport) { + /* Adjust Lamport's Logical Clock */ + if (lamport++ <= r_lamport) + lamport = r_lamport + 1; + if (device_id == bulbid || device_id == outletid) { printf("Offset received %d\n", l_offset); @@ -166,6 +181,17 @@ long long set_offset(int device_id, int l_offset) return ret_offset; } +/* FUNCTION: request_lamport + * DESCRIPTION: Returns lamport time variable to remote requests. + * + * @params: none + * @returns: int local time + */ + int request_lamport() + { + return lamport; + } + /* FUNCTION: getLocalTimeStamp * DESCRIPTION: Get local time im ms adjusted by the berkeley offset * @@ -188,9 +214,13 @@ long long getLocalTimeStamp() * @params: none * @returns: none */ -void powerdown() +void powerdown(int r_lamport) { - /* Unlock the powerdown mutex */ + /* Adjust Lamport's Logical Clock */ + if (lamport++ <= r_lamport) + lamport = r_lamport + 1; + + /* Unlock the powerdown mutex */ mode_lock.unlock(); return; } @@ -202,11 +232,15 @@ void powerdown() * @params: device id and updated state * @returns: acknowledgement */ -STATUS change_state(int device_id, int new_state) +STATUS change_state(int device_id, int new_state, int r_lamport) { STATUS status = SUCCESS; - /* Check the device ID */ + /* Adjust Lamport's Logical Clock */ + if (lamport++ <= r_lamport) + lamport = r_lamport + 1; + + /* Check the device ID */ if (device_id == bulbid) { cout << "Changing State for Smart Bulb to: " << new_state << endl; @@ -236,8 +270,12 @@ STATUS change_state(int device_id, int new_state) * @params: none * @returns: id of leader */ -void leader(int lid) +void leader(int lid, int r_lamport) { + /* Adjust Lamport's Logical Clock */ + if (lamport++ <= r_lamport) + lamport = r_lamport + 1; + lead = lid; } @@ -247,8 +285,12 @@ void leader(int lid) * @params: none * @returns: status of leader election */ -int election(int id) +int election(int id, int r_lamport) { + /* Adjust Lamport's Logical Clock */ + if (lamport++ <= r_lamport) + lamport = r_lamport + 1; + cout << "Leader Election in Progress.." << endl; int myid = bulbid; @@ -292,6 +334,7 @@ void *ServerEntry(void *arg) srv.bind("leader", &leader); srv.bind("request_timestamp", &request_timestamp); srv.bind("set_offset", &set_offset); + srv.bind("request_lamport", &request_lamport); /* Run the server loop with 2 threads */ srv.async_run(2); @@ -367,7 +410,7 @@ STATUS main(int argc, char **argv) cout << "Registering Smart Bulb...\n"; - bulbid = client.call("registerf", "device", "bulb", devices_ip, devices_port).as(); + bulbid = client.call("registerf", "device", "bulb", devices_ip, devices_port, lamport++).as(); if (bulbid == ERR_SENSOR_NOT_REGISTERED) { @@ -381,7 +424,7 @@ STATUS main(int argc, char **argv) cout << "SUCCESS \n"; - outletid = client.call("registerf", "device", "outlet", devices_ip, devices_port).as(); + outletid = client.call("registerf", "device", "outlet", devices_ip, devices_port, lamport++).as(); if (outletid == ERR_SENSOR_NOT_REGISTERED) { diff --git a/gateway/dbmanager.cpp b/gateway/dbmanager.cpp index 8290ea3..dc34012 100644 --- a/gateway/dbmanager.cpp +++ b/gateway/dbmanager.cpp @@ -213,7 +213,7 @@ string* getHistory(int count) return history; } -void logEntry(long long timeStamp, int device_id, int value, string comment) +void logEntry(long long timeStamp, int lamport, int device_id, int value, string comment) { if (device_id > 5 || device_id < 0) return; @@ -279,6 +279,7 @@ void logEntry(long long timeStamp, int device_id, int value, string comment) vector temp_history; temp_history.push_back(to_string(timeStamp)); + temp_history.push_back(to_string(lamport)); temp_history.push_back(localsendev); temp_history.push_back(localvalue); temp_history.push_back(comment); diff --git a/gateway/gateway.cpp b/gateway/gateway.cpp index 9ba4eb7..496a381 100644 --- a/gateway/gateway.cpp +++ b/gateway/gateway.cpp @@ -48,6 +48,9 @@ int states[2] = {OFF, OFF}; /* Berkeley Offset */ int offset = 0; +/* Lamport Time (Atomic Counter) */ +atomic lamport; + /* Structures for DB */ vector dbstates; vector history; @@ -131,7 +134,7 @@ long long query_state(int device_id) { rpc::client cln(ips[device_id], ports[device_id]); auto start = chrono::system_clock::now(); - value = cln.call("query_state", device_id).as(); + value = cln.call("query_state", device_id, lamport++).as(); auto end = chrono::system_clock::now(); #ifdef LATENCY chrono::duration elapsed_seconds = end - start; @@ -195,12 +198,15 @@ int change_state(int device_id, int state) if (isRegistered[device_id] == 1 && ips[device_id] != "") { rpc::client cln(ips[device_id], ports[device_id]); - ack = cln.call("change_state", device_id, state).as(); + ack = cln.call("change_state", device_id, state, lamport++).as(); /* Log the event into database */ - long long timeStamp = cln.call("request_timestamp", device_id).as(); + long long timeStamp = cln.call("request_timestamp", device_id, lamport++).as(); + int l_lamport = cln.call("request_lamport").as(); + if (lamport++ <= l_lamport) + lamport = l_lamport + 1; setState(device_id, state); - logEntry(timeStamp, device_id, state, " "); + logEntry(timeStamp, lamport, device_id, state, " "); if (ack == SUCCESS) states[device_id - 4] = state; @@ -446,7 +452,7 @@ void *berkeleyTime(void *arg) { rpc::client cln(ips[device_id], ports[device_id]); //FixMe: return from remote call has device id in the upper 32 bits - timestamp[device_id] = cln.call("request_timestamp", device_id).as(); + timestamp[device_id] = cln.call("request_timestamp", device_id, lamport++).as(); avg += timestamp[device_id]; } @@ -458,7 +464,7 @@ void *berkeleyTime(void *arg) int remote_offset = (int)(avg - timestamp[device_id]); rpc::client cln(ips[device_id], ports[device_id]); //FixMe: return is long long. - response = (int)(cln.call("set_offset", device_id, remote_offset).as()); + response = (int)(cln.call("set_offset", device_id, remote_offset, lamport++).as()); if (response != remote_offset) printf("Error: berkeleyTime - device id: %d not set properly.\n", device_id); } @@ -497,7 +503,7 @@ void change_mode(int lmode) { mode = lmode; rpc::client cln(ips[MOTION], ports[MOTION]); - cln.async_call("change_mode", lmode); // Change mode on sensors + cln.async_call("change_mode", lmode, lamport++); // Change mode on sensors if (lmode == HOME) { @@ -512,7 +518,7 @@ void change_mode(int lmode) else if (lmode == EXIT) { rpc::client cln2(ips[OUTLET], ports[OUTLET]); - cln2.call("powerdown"); // Powerdown + cln2.call("powerdown", lamport++); // Powerdown } } @@ -614,7 +620,7 @@ STATUS lelection() if (isRegistered[i] == 1 && ips[i] != "") { rpc::client cln(ips[i], ports[i]); - ack = cln.call("election", myid).as(); + ack = cln.call("election", myid, lamport++).as(); if (ack == 0) { break; @@ -636,7 +642,7 @@ STATUS lelection() for (int j = 0; j < 6; j++) { rpc::client cln(ips[j], ports[j]); - (void) cln.call("leader", lid); + (void) cln.call("leader", lid, lamport++); } return SUCCESS; @@ -718,11 +724,15 @@ STATUS main() srv.bind("leader", &leader); /* Bind the Register Function */ - srv.bind("registerf", []( string ltype, string lname, string IP, int port) + srv.bind("registerf", []( string ltype, string lname, string IP, int port, int r_lamport) { #ifdef DEBUG cout << "registerf called " <(); + long long timeStamp = cln.call("request_timestamp", device_id, lamport++).as(); + int l_lamport = cln.call("request_lamport").as(); + if (lamport++ <= l_lamport) + lamport = l_lamport + 1; setState(device_id, (int)state); - logEntry(timeStamp, device_id, (int)state, " "); + logEntry(timeStamp, lamport, device_id, (int)state, " "); }); /* Run the server loop */ diff --git a/include/dbmanager.h b/include/dbmanager.h index 0d42304..2d97e7a 100644 --- a/include/dbmanager.h +++ b/include/dbmanager.h @@ -50,7 +50,7 @@ void setState(int device_id, int value); string* getHistory(int count); -void logEntry(long long timeStamp, int device_id, int value, string comment); +void logEntry(long long timeStamp, int lamport, int device_id, int value, string comment); void log(vector history); #endif /* DBMANAGER_H_ */ \ No newline at end of file diff --git a/include/devices.h b/include/devices.h index d06295f..8ff6328 100644 --- a/include/devices.h +++ b/include/devices.h @@ -69,10 +69,11 @@ typedef std::mutex LOCK; /* Function Prototypes */ std::string getIPAddress(); -long long query_state(int device_id); -STATUS change_state(int device_id, int new_state); -long long request_timestamp(int device_id); -long long set_offset(int device_id, int l_offset); +long long query_state(int device_id, int r_lamport); +STATUS change_state(int device_id, int new_state, int r_lamport); +long long request_timestamp(int device_id, int r_lamport); +long long set_offset(int device_id, int l_offset, int r_lamport); +int request_lamport(); long long getLocalTimeStamp(); #endif /* SENSORS_H_ */ diff --git a/include/sensors.h b/include/sensors.h index f78882a..8172e3c 100644 --- a/include/sensors.h +++ b/include/sensors.h @@ -74,11 +74,12 @@ typedef std::mutex LOCK; STATUS SetTimer(int msecs); void *report_state(void *arg); int Random_Number(); -void change_mode(int inmode); +void change_mode(int inmode, int r_lamport); std::string getIPAddress(); -long long query_state(int device_id); -long long request_timestamp(int device_id); -long long set_offset(int device_id, int l_offset); +long long query_state(int device_id, int r_lamport); +long long request_timestamp(int device_id, int r_lamport); +long long set_offset(int device_id, int l_offset, int r_lamport); +int request_lamport(); long long getLocalTimeStamp(); void Update(); void *Server_Entry(void *arg); diff --git a/sensors/sensors.cpp b/sensors/sensors.cpp index 4a124e2..633a713 100644 --- a/sensors/sensors.cpp +++ b/sensors/sensors.cpp @@ -50,6 +50,9 @@ TIMER motion_timer; /* Berkeley Offset */ int offset = 0; +/* Lamport Time (Atomic Counter) */ +atomic lamport; + /* Sensor IDs assigned by Gateway */ int msensor = ERR_SENSOR_NOT_REGISTERED; int tsensor = ERR_SENSOR_NOT_REGISTERED; @@ -109,8 +112,12 @@ string getIPAddress() * @params: new mode * @returns: none */ -void change_mode(int inmode) +void change_mode(int inmode, int r_lamport) { + /* Adjust Lamport's Logical Clock */ + if (lamport++ <= r_lamport) + lamport = r_lamport + 1; + /* Acquire mode update lock */ offset_lock.lock(); @@ -210,10 +217,10 @@ void *report_state(void *arg) rpc::client client(gateway_ip, gateway_port); cout << "Registering Temperature Sensor...\n"; - tsensor = client.call("registerf", "sensor", "temp", sensor_ip, sensor_port).as(); + tsensor = client.call("registerf", "sensor", "temp", sensor_ip, sensor_port, lamport++).as(); cout << "Registering Motion Sensor...\n"; - msensor = client.call("registerf", "sensor", "motion", sensor_ip, sensor_port).as(); + msensor = client.call("registerf", "sensor", "motion", sensor_ip, sensor_port, lamport++).as(); if (msensor == ERR_SENSOR_NOT_REGISTERED) { @@ -236,7 +243,7 @@ void *report_state(void *arg) cout << "Motion Detected " << endl; /* Push motion sense data */ - client.call("report_state", msensor, val); + client.call("report_state", msensor, val, lamport++); } } @@ -259,10 +266,10 @@ void *doorf(void *arg) rpc::client client(gateway_ip, gateway_port); cout << "Registering Door Sensor...\n"; - dsensor = client.call("registerf", "sensor", "door", sensor_ip, sensor_port).as(); + dsensor = client.call("registerf", "sensor", "door", sensor_ip, sensor_port, lamport++).as(); cout << "Registering Keyhain...\n"; - psensor = client.call("registerf", "sensor", "keychain", sensor_ip, sensor_port).as(); + psensor = client.call("registerf", "sensor", "keychain", sensor_ip, sensor_port, lamport++).as(); if (dsensor == ERR_SENSOR_NOT_REGISTERED) { @@ -312,7 +319,7 @@ void *doorf(void *arg) { cout << "Keychain Reported " << endl; - client.call("report_state", psensor, val2); + client.call("report_state", psensor, val2, lamport++); } } @@ -321,12 +328,12 @@ void *doorf(void *arg) cout << "Door Opened " << endl; /* Open the door */ - client.call("report_state", dsensor, val); + client.call("report_state", dsensor, val, lamport++); sleep(1); /* Close the door */ - client.call("report_state", dsensor, false); + client.call("report_state", dsensor, false, lamport++); cout << "Door Closed " << endl; @@ -346,8 +353,12 @@ void *doorf(void *arg) * @params: device id * @returns: device_id and state */ -long long query_state(int device_id) +long long query_state(int device_id, int r_lamport) { + /* Adjust Lamport's Logical Clock */ + if (lamport++ <= r_lamport) + lamport = r_lamport + 1; + /* Random number for temperature of motion */ float res = (float) Random_Number(); @@ -409,8 +420,12 @@ long long query_state(int device_id) * @params: device id * @returns: device_id and timestamp */ -long long request_timestamp(int device_id) +long long request_timestamp(int device_id, int r_lamport) { + /* Adjust Lamport's Logical Clock */ + if (lamport++ <= r_lamport) + lamport = r_lamport + 1; + long long res = 0; if (device_id == msensor || device_id == tsensor || device_id == dsensor || device_id == psensor) { @@ -426,8 +441,12 @@ long long request_timestamp(int device_id) * @params: device id * @returns: device_id and offset */ -long long set_offset(int device_id, int l_offset) +long long set_offset(int device_id, int l_offset, int r_lamport) { + /* Adjust Lamport's Logical Clock */ + if (lamport++ <= r_lamport) + lamport = r_lamport + 1; + if (device_id == msensor || device_id == tsensor || device_id == dsensor || device_id == psensor) { printf("Offset received %d\n", l_offset); @@ -440,6 +459,17 @@ long long set_offset(int device_id, int l_offset) return ret_offset; } +/* FUNCTION: request_lamport + * DESCRIPTION: Returns lamport time variable to remote requests. + * + * @params: none + * @returns: int local time + */ + int request_lamport() + { + return lamport; + } + /* FUNCTION: getLocalTimeStamp * DESCRIPTION: Get local time im ms adjusted by the berkeley offset * @@ -461,13 +491,21 @@ void Update() motion_lock.unlock(); } -void leader(int lid) +void leader(int lid, int r_lamport) { + /* Adjust Lamport's Logical Clock */ + if (lamport++ <= r_lamport) + lamport = r_lamport + 1; + lead = lid; } -int election(int id) +int election(int id, int r_lamport) { + /* Adjust Lamport's Logical Clock */ + if (lamport++ <= r_lamport) + lamport = r_lamport + 1; + cout << "Leader Election in Progress.." << endl; int myid = tsensor; @@ -511,6 +549,7 @@ void *Server_Entry(void *arg) srv.bind("leader", &leader); srv.bind("request_timestamp", &request_timestamp); srv.bind("set_offset", &set_offset); + srv.bind("request_lamport", &request_lamport); /* Run the server loop. */