/**************************************************************** * NAME: startKelsa * * DESCRIPTION: This is the pthread function * * PARAMETERS: * (void *) threadNum - thread counter * * RETURN: * (int) ZERO if success * ERROR otherwise * ****************************************************************/ void * startKelsa(void *threadNum) { funcEntry(logF, ipAddress, "startKelsa"); int rc = SUCCESS, // Return code i_rc, // Temp RC *counter; // Thread counter //counter = (int *) malloc(sizeof(int)); counter = (int *) threadNum; pthread_t tid = pthread_self(); // Thread ID sprintf(logMsg, "This is thread with counter: %d and thread ID: %lu", *counter, tid); printToLog(logF, ipAddress, logMsg); switch(*counter) { case 0: // First thread calls receiver function that does: // i) Approve join requests if LEADER // ii) Receive heartbeats strcat(logMsg, "\texecuting receiverFunc"); printToLog(logF, ipAddress, logMsg); i_rc = receiverFunc(); break; case 1: // Second thread calls sender function that does: // i) Sends heartbeats strcat(logMsg, "\texecuting sendFunc"); printToLog(logF, ipAddress, logMsg); i_rc = sendFunc(); break; case 2: // Third thread calls heartbeat checker function that: // i) checks heartbeat table strcat(logMsg, "\texecuting heartBeatCheckerFunc"); printToLog(logF, ipAddress, logMsg); i_rc = heartBeatCheckerFunc(); break; default: // Can't get here if we do then exit printToLog(logF, ipAddress, "default case. An error"); rc = ERROR; goto rtn; break; } // End of switch rtn: funcExit(logF, ipAddress, "startKelsa", rc); }
/************************************************* Description: 设置状态回复定时器,以信宿和帧类型区分 Calls: TCPServer:: Input: destination: 信宿 conn:TCP连接 totalLength:帧总长度 tpye:帧类型 message:帧消息字 Output: 无 Return: 无 *************************************************/ void Dispatcher::setTimerForDT(ADDRESS destination, TcpConnectionPtr& conn, uint16_t totalLength, MessageType type, u_char * message, function<void ()> retryExceedHandler) { //帧计数加1,并取消相关定时器 uint32_t count = getAndSetFrameCount(destination, 1); shared_ptr<int> retryCount(new int); *retryCount = 0; weak_ptr<TcpConnection> weakTcpPtr(conn); function<void()> sendFunc = bind(&Dispatcher::sendForTimer, this, weakTcpPtr, totalLength, type, message, destination, count, retryCount, retryExceedHandler); TimerId timerId = loop_->runEvery(timeoutSec_, sendFunc); cancelTimerForDT(destination, type); { MutexLockGuard lock(statusMutex_); statusTimer_[destination][type] = timerId; } sendFunc(); }