Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

212 lines
4.7 KiB

  1. /*++
  2. Copyright(c) 2001 Microsoft Corporation
  3. Module Name:
  4. NLB Manager
  5. File Name:
  6. nlbhost.cpp
  7. Abstract:
  8. Implementation of class NLBHost
  9. NLBHost is responsible for connecting to an NLB host and getting/setting
  10. its NLB-related configuration.
  11. History:
  12. 03/31/01 JosephJ Created
  13. --*/
  14. #include "stdafx.h"
  15. #include "private.h"
  16. #ifdef _DEBUG
  17. #define new DEBUG_NEW
  18. #undef THIS_FILE
  19. static char THIS_FILE[] = __FILE__;
  20. #endif
  21. /////////////////////////////////////////////////////////////////////////////
  22. // The one and only application object
  23. CWinApp theApp;
  24. void test(int argc, WCHAR* argv[]);
  25. int __cdecl wmain(int argc, WCHAR* argv[], WCHAR* envp[])
  26. {
  27. int nRetCode = 0;
  28. // initialize MFC and print and error on failure
  29. if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
  30. {
  31. // TODO: change error code to suit your needs
  32. cerr << L"Fatal Error: MFC initialization failed" << endl;
  33. nRetCode = 1;
  34. }
  35. else
  36. {
  37. test(argc, argv);
  38. }
  39. return nRetCode;
  40. }
  41. VOID
  42. MyLogger(
  43. PVOID Context,
  44. const WCHAR * Text
  45. )
  46. {
  47. wprintf(L"LOG: %s", Text);
  48. wprintf(L"\n");
  49. }
  50. void test(int argc, WCHAR* argv[])
  51. {
  52. WCHAR wszBuf[1024];
  53. NLBHost * pHost = NULL;
  54. UINT Status = 0;
  55. NLBHost::HostInformation *pHostInfo = NULL;
  56. pHost = new NLBHost(
  57. L"JOSEPHJ1C",
  58. L"JosephJ's Dev Machine",
  59. MyLogger,
  60. NULL // Logger context
  61. );
  62. if (pHost == NULL)
  63. {
  64. MyLogger(NULL, L"Could not create an instance of NLBHost.");
  65. goto end;
  66. }
  67. Status = pHost->Ping();
  68. if (!NLBH_SUCCESS(Status)) goto end;
  69. Status = pHost->GetHostInformation(
  70. &pHostInfo
  71. );
  72. if (!NLBH_SUCCESS(Status))
  73. {
  74. pHostInfo = NULL;
  75. goto end;
  76. }
  77. int NumNics = pHostInfo->nicInformation.size();
  78. wsprintf(
  79. wszBuf,
  80. L"Processing host with MachineName %s(NumNics=%d)",
  81. (LPCWSTR)pHostInfo->MachineName,
  82. NumNics
  83. );
  84. MyLogger(NULL, wszBuf);
  85. for( int i = 0; i < NumNics; i++)
  86. {
  87. NLBHost::NicInformation *pNicInfo;
  88. MGR_RAW_CLUSTER_CONFIGURATION ClusterConfig;
  89. UINT GenerationId;
  90. UINT RequestId;
  91. pNicInfo = &pHostInfo->nicInformation[i];
  92. wsprintf(
  93. wszBuf,
  94. L"Processing NIC %s (%s)\n",
  95. (LPCWSTR) pNicInfo->adapterGuid,
  96. (LPCWSTR) pNicInfo->friendlyName
  97. );
  98. MyLogger(NULL, wszBuf);
  99. Status = pHost->GetClusterConfiguration(
  100. pNicInfo->adapterGuid,
  101. &ClusterConfig,
  102. &GenerationId
  103. );
  104. if (!NLBH_SUCCESS(Status))
  105. {
  106. // Failure...
  107. continue;
  108. }
  109. Status = pHost->SetClusterConfiguration(
  110. pNicInfo->adapterGuid,
  111. &ClusterConfig,
  112. GenerationId,
  113. &RequestId
  114. );
  115. if (NLBH_PENDING(Status))
  116. {
  117. INT Delay = 1000; // 1 sec
  118. // Give some time for the remote host's connectivity to be lost...
  119. //
  120. MyLogger(NULL, L"Sleeping for 5 seconds.");
  121. Sleep(5000);
  122. // Now wait until we can ping the host, then query for the async
  123. // result of the SetClusterConfiguration operation.
  124. // We keep polling for the result, sleeping increasing amounts
  125. // in between.
  126. //
  127. MyLogger(NULL, L"Pinging the host ....");
  128. while (NLBH_SUCCESS(pHost->Ping()))
  129. {
  130. UINT ResultCode;
  131. _bstr_t ResultText;
  132. MyLogger(NULL, L"Checking for asynchronous completion...");
  133. Status = pHost->GetAsyncResult(
  134. RequestId,
  135. &GenerationId,
  136. &ResultCode,
  137. &ResultText
  138. );
  139. if (!NLBH_PENDING(Status))
  140. {
  141. break;
  142. }
  143. MyLogger(NULL, L"Operation still pending...");
  144. MyLogger(NULL, L"Waiting to try again ....");
  145. Sleep(Delay);
  146. MyLogger(NULL, L"Pinging the host ....");
  147. Delay *=2;
  148. }
  149. }
  150. }
  151. end:
  152. if (pHostInfo != NULL)
  153. {
  154. delete pHostInfo;
  155. pHostInfo = NULL;
  156. }
  157. if (pHost != NULL)
  158. {
  159. delete pHost;
  160. pHost = NULL;
  161. }
  162. return;
  163. }