giuliomoro@135: #include "ClockSync.h" giuliomoro@135: giuliomoro@135: void ClockSync::setVirtualClock(VirtualClock &aVirtualClock){ giuliomoro@135: virtualClock=&aVirtualClock; giuliomoro@135: } giuliomoro@135: void ClockSync::init(bool thisIsSlave, int aPort, VirtualClock &aVirtualClock){ giuliomoro@135: setVirtualClock(aVirtualClock); giuliomoro@135: slave=thisIsSlave; giuliomoro@135: setPort(aPort); giuliomoro@135: // isSlave() ? client.setServer("127.0.0.1") : client.setServer("127.0.0.1"); giuliomoro@135: isSlave() ? client.setServer("192.168.7.1") : client.setServer("192.168.7.2"); giuliomoro@135: bufferLength=kSyncMessageLength; giuliomoro@135: T1=-1; giuliomoro@135: T1p=-1; giuliomoro@135: T2=-1; giuliomoro@135: T2p=-1; giuliomoro@135: receiveLoopSleepUs=10; giuliomoro@135: receiveLoopTimeout=1e5; giuliomoro@135: movingAverage.setLength(201); giuliomoro@135: expectedClockSyncType=isSlave() ? kSync : kNone; giuliomoro@135: } giuliomoro@135: ClockSync::ClockSync(bool thisIsSlave, int aPort, VirtualClock &aVirtualClock){ giuliomoro@135: init(thisIsSlave, aPort, aVirtualClock); giuliomoro@135: } giuliomoro@135: void* ClockSync::getBuffer(){ giuliomoro@135: return buffer; giuliomoro@135: } giuliomoro@135: bool ClockSync::isSlave(){ giuliomoro@135: return slave; giuliomoro@135: } giuliomoro@135: bool ClockSync::isMaster(){ giuliomoro@135: return !slave; giuliomoro@135: } giuliomoro@135: int ClockSync::getType(){ giuliomoro@135: return ((int*)buffer)[0]; giuliomoro@135: } giuliomoro@135: myClock_t ClockSync::getTimestamp(){ giuliomoro@135: return *((myClock_t*)((char*)buffer+sizeof(int))); giuliomoro@135: } giuliomoro@135: void ClockSync::setType(int clockSyncType){ giuliomoro@135: ((int*)buffer)[0]=clockSyncType; giuliomoro@135: } giuliomoro@135: void ClockSync::setTimestamp(myClock_t timestamp){ giuliomoro@135: ((myClock_t*)((char*)buffer+sizeof(int)))[0]=timestamp; giuliomoro@135: } giuliomoro@135: void ClockSync::print(){ giuliomoro@135: //printf("type: %d, timestamp: %lld\n",*((int*)buffer),*((myClock_t*)(((int*)buffer)+1))); giuliomoro@135: } giuliomoro@135: void ClockSync::setPort(int aPort){ giuliomoro@135: if(aPort>0){ giuliomoro@135: int inPort=isSlave() ? aPort : aPort+1; giuliomoro@135: int outPort=isSlave() ? aPort+1: aPort; giuliomoro@135: server.bindToPort(inPort); giuliomoro@135: client.setPort(outPort); giuliomoro@135: //printf("Receiving on port %d\n", inPort); giuliomoro@135: //printf("Sending to port %d\n", outPort); giuliomoro@135: } giuliomoro@135: } giuliomoro@135: /** giuliomoro@135: * sends a clockSync without blocking, checks results and returns the timestamp giuliomoro@135: * immediately after the clockSync has been sent or -1 if there was an error or timeout expired. giuliomoro@135: */ giuliomoro@135: myClock_t ClockSync::send(){ giuliomoro@135: // print(); giuliomoro@135: int ret; giuliomoro@135: ret=client.waitUntilReady(false, 0); giuliomoro@135: if(ret<=0){ //don't retry giuliomoro@135: return -1; giuliomoro@135: } giuliomoro@135: ret=client.send(buffer, bufferLength); giuliomoro@135: myClock_t timestamp=(myClock_t)virtualClock->getNow(); giuliomoro@135: if(ret<0){ giuliomoro@135: //if there was an error while sending, don't retry giuliomoro@135: return -1; giuliomoro@135: } giuliomoro@135: return timestamp; //get the accurate time *after* you sent the sync clockSync giuliomoro@135: } giuliomoro@135: /** giuliomoro@135: * receives a clockSync without blocking, checks results and returns the timestamp giuliomoro@135: * immediately after the clockSync has been received, or -1 if there was an error giuliomoro@135: * or 0 if timeout expired. giuliomoro@135: */ giuliomoro@135: myClock_t ClockSync::receive(){ giuliomoro@135: int ret; giuliomoro@135: ret=server.waitUntilReady(true, 0); giuliomoro@135: if(ret<=0){ //don't retry giuliomoro@135: return 0; giuliomoro@135: } giuliomoro@135: ret=server.read(buffer, bufferLength, false); giuliomoro@135: myClock_t timestamp=(myClock_t)virtualClock->getNow(); giuliomoro@135: if(timestamp==0){ giuliomoro@135: //printf("The virtualClock period is <=0\n"); giuliomoro@135: return -3; giuliomoro@135: } giuliomoro@135: if(ret==-1){ giuliomoro@135: //if there was an error while receiving, don't retry giuliomoro@135: return -1; giuliomoro@135: } giuliomoro@135: if(ret!=bufferLength){ giuliomoro@135: //printf("Received a clockSync of the wrong size: %d\n", ret); giuliomoro@135: return -2; giuliomoro@135: } giuliomoro@135: // print(); giuliomoro@135: return timestamp; //get the accurate time *after* you sent the sync clockSync giuliomoro@135: } giuliomoro@135: giuliomoro@135: int ClockSync::masterSendSync(){ giuliomoro@135: //let's send a sync clockSync! giuliomoro@135: //printf("Sending a sync clockSync\n"); giuliomoro@135: setType(kSync); giuliomoro@135: setTimestamp(-1);//do not care about sending the timestamp, a more accurate one will be sent in the follow up giuliomoro@135: localTimestamp=send(); giuliomoro@135: if(localTimestamp<0){ giuliomoro@135: //printf("Could not send sync clockSync\n"); giuliomoro@135: return -1; //error, don't retry, start over giuliomoro@135: } giuliomoro@135: //let's send a followUp giuliomoro@135: //printf("sent a sync clockSync\n"); giuliomoro@135: setType(kFollowUp); giuliomoro@135: setTimestamp(localTimestamp); giuliomoro@135: if(localTimestamp<0){ giuliomoro@135: //printf("Could not send followup clockSync\n"); giuliomoro@135: return -2; //error, don't retry, start over giuliomoro@135: } giuliomoro@135: int ret=send(); giuliomoro@135: if(ret<0){ giuliomoro@135: //printf("Error while sending followup\n"); giuliomoro@135: return -3; giuliomoro@135: } giuliomoro@135: //printf("sent a followUp clockSync\n"); giuliomoro@135: expectedClockSyncType=kDelayReq; giuliomoro@135: return 1; giuliomoro@135: } giuliomoro@135: #ifdef USE_JUCE giuliomoro@135: #define NOTGSHOULDSTOP 1 giuliomoro@135: #else giuliomoro@135: extern bool gShouldStop; giuliomoro@135: #define NOTGSHOULDSTOP (!gShouldStop) giuliomoro@135: #endif /* USE_JUCE */ giuliomoro@135: int ClockSync::receiveLoop(){ giuliomoro@135: int receiveLoopElapsed=0; giuliomoro@135: while( NOTGSHOULDSTOP && (isSlave() || (receiveLoopElapsed