CNdasDeviceHeartbeatListener* GetListenerInstance() { CNdasDeviceHeartbeatListener* pListener = SharedData.listener; if (NULL != pListener) { ::InterlockedIncrement((LPLONG)&SharedData.ref); return pListener; } pListener = new CNdasDeviceHeartbeatListener(); if (NULL == pListener) { ::SetLastError(ERROR_OUTOFMEMORY); return NULL; } BOOL fSuccess = pListener->Initialize(); if (!fSuccess) { DBGPRT_ERR(_T("Failed to init listener: ")); return NULL; } if (!pListener->Run()) { DBGPRT_ERR(_T("Failed to run listener: ")); return NULL; } // okay, we've got pListener running SharedData.listener = pListener; ::InterlockedIncrement((LPLONG)&SharedData.ref); return pListener; }
int __cdecl wmain() { WSADATA wsaData; if (0 != ::WSAStartup(MAKEWORD(2,2), &wsaData)) { _tprintf(_T("Failed to initialize socket: %d\n"), ::GetLastError()); } CNdasDeviceHeartbeatListener listener; BOOL fSuccess = listener.Initialize(); if (!fSuccess) { _tprintf(_T("Failed to init listener : %d\n"), ::GetLastError()); return 1; } if (!listener.Run()) { _tprintf(_T("Failed to run listener: %d\n"), ::GetLastError()); return 1; } CNdasDeviceHeartbeatHandler* subscriber = new CMyClass(&listener); ::Sleep(60000); listener.Stop(TRUE); delete subscriber; return 0; }
DWORD CNdasAutoRegister::OnTaskStart() { _ASSERTE(NULL != m_hSemQueue && "Don't forget to call initialize()."); // Queue Semaphore, Terminating Thread, Pipe Instances(MAX...) HANDLE hWaitHandles[2]; hWaitHandles[0] = m_hTaskTerminateEvent; hWaitHandles[1] = m_hSemQueue; CNdasDeviceHeartbeatListener* pListener = pGetNdasDeviceHeartbeatListner(); pListener->Attach(this); do { DWORD dwWaitResult = ::WaitForMultipleObjects( 2, hWaitHandles, FALSE, INFINITE); if (WAIT_OBJECT_0 == dwWaitResult) { break; } else if (WAIT_OBJECT_0 + 1 == dwWaitResult) { while (TRUE) { m_queueLock.Lock(); if (m_queue.empty()) { m_queueLock.Unlock(); break; } QUEUE_ENTRY entry = m_queue.front(); m_queue.pop(); m_queueLock.Unlock(); (VOID) ProcessRegister(entry.deviceID, entry.access); } } else { _ASSERTE(FALSE); // ERROR } } while (TRUE); pListener->Detach(this); return 0; }
VOID CNdasAutoRegister::Update(ximeta::CSubject* pChangedSubject) { CNdasDeviceHeartbeatListener* pListener = pGetNdasDeviceHeartbeatListner(); // // Ignore other than subscribed heartbeat listener // if (pListener == pChangedSubject) { NDAS_DEVICE_HEARTBEAT_DATA hbData; pListener->GetHeartbeatData(&hbData); NDAS_DEVICE_ID deviceId = {0}; ::CopyMemory(&deviceId, hbData.remoteAddr.Node, sizeof(deviceId)); ACCESS_MASK autoRegAccess = m_data.GetAutoRegAccess(deviceId); if (!autoRegAccess) { return; } // // If already registered, do nothing // CRefObjPtr<CNdasDevice> pDevice = pGetNdasDevice(deviceId); if (NULL != pDevice.p) { return; } ::NdasLogEventInformation( EVT_NDASSVC_INFO_AUTOREG_NDAS_DEVICE_FOUND, NULL, 0, sizeof(deviceId), NULL, &deviceId); (VOID) AddToQueue(deviceId, autoRegAccess); } }
void CNdasDevice::Update(ximeta::CSubject* pChangedSubject) { ximeta::CAutoLock autolock(this); CNdasDeviceHeartbeatListener* pListener = pGetNdasDeviceHeartbeatListner(); // // Ignore other than subscribed heartbeat listener // if (pListener == pChangedSubject) { NDAS_DEVICE_HEARTBEAT_DATA hbData; pListener->GetHeartbeatData(&hbData); // // matching device id (address) only // // LPX_ADDRESS and NDAS_DEVICE_ID are different type // so we cannot merely use CompareLpxAddress function here // if (0 == ::memcmp( hbData.remoteAddr.Node, m_deviceId.Node, sizeof(m_deviceId.Node[0]) * 6)) { OnDiscovered( hbData.localAddr, hbData.remoteAddr, hbData.ucType, hbData.ucVersion); } } }