dalistop.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems®.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #include "platform.h"
  14. #include "jlib.hpp"
  15. #include "jfile.hpp"
  16. #include "jmisc.hpp"
  17. #include "mpbase.hpp"
  18. #include "mpcomm.hpp"
  19. #include "daclient.hpp"
  20. int main(int argc, const char* argv[])
  21. {
  22. InitModuleObjects();
  23. int exitCode = 1;
  24. try
  25. {
  26. unsigned port = 0;
  27. const char *server = nullptr;
  28. if (argc<2)
  29. {
  30. // with no args, use port from daliconfig if present (used by init scripts)
  31. Owned<IFile> daliConfigFile = createIFile("daliconf.xml");
  32. if (daliConfigFile->exists())
  33. {
  34. Owned<IPropertyTree> daliConfig = createPTree(*daliConfigFile, ipt_caseInsensitive);
  35. port = daliConfig->getPropInt("@port", DALI_SERVER_PORT);
  36. server = ".";
  37. }
  38. else
  39. {
  40. printf("usage: dalistop <server_ip:port> [/nowait]\n");
  41. printf("eg: dalistop . -- stop dali server running locally\n");
  42. printf(" dalistop eq0001016 -- stop dali server running remotely\n");
  43. }
  44. }
  45. else
  46. {
  47. server = argv[1];
  48. port = DALI_SERVER_PORT;
  49. }
  50. if (server)
  51. {
  52. SocketEndpoint ep;
  53. ep.set(server, port);
  54. bool nowait = false;
  55. if (argc>=3)
  56. nowait = stricmp(argv[2],"/nowait")==0;
  57. printf("Stopping Dali Server on %s, port=%u\n", server, port);
  58. Owned<IGroup> group = createIGroup(1,&ep);
  59. initClientProcess(group, DCR_DaliStop);
  60. Owned<ICommunicator> comm = createCommunicator(group);
  61. CMessageBuffer mb;
  62. int fn=-1;
  63. mb.append(fn);
  64. if (comm->verifyConnection(0,2000))
  65. {
  66. comm->send(mb,0,MPTAG_DALI_COVEN_REQUEST,MP_ASYNC_SEND);
  67. if (nowait)
  68. {
  69. Sleep(1000);
  70. exitCode = 0;
  71. }
  72. else
  73. {
  74. // verifyConnection() has a min conn timeout of 10s
  75. // use recv() instead to check for socket closed ...
  76. try
  77. {
  78. while (!comm->recv(mb,0,MPTAG_DALI_COVEN_REQUEST,nullptr,5000))
  79. {
  80. printf("Waiting for Dali Server to stop....\n");
  81. }
  82. exitCode = 0;
  83. }
  84. catch (IMP_Exception *e)
  85. {
  86. if (e->errorCode() == MPERR_link_closed)
  87. exitCode = 0;
  88. e->Release();
  89. }
  90. }
  91. }
  92. else
  93. fprintf(stderr, "Dali not responding\n");
  94. stopMPServer();
  95. }
  96. }
  97. catch (IException *e)
  98. {
  99. pexception("Exception",e);
  100. stopMPServer();
  101. }
  102. releaseAtoms();
  103. return exitCode;
  104. }