hodisp_base.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 "hrpc.hpp"
  14. #include "hodisp_base.hpp"
  15. #include "jsocket.hpp"
  16. #include "jkeyboard.hpp"
  17. bool dispatch_abort=false;
  18. void m_dispatch::dispatch(c_dispatch * disp)
  19. { while (1)
  20. {
  21. unsigned i=0;
  22. for (i=0; i<max_disp; i++)
  23. { if (d[i]&&d[i]->is_done())
  24. { if (d[i]->ex1)
  25. { char * t=last_error;
  26. last_error=alloccat(d[i]->ex1,"\n",t,0);
  27. //traceft("m_dispatch error added %s\n",d[i]->ex1);
  28. if (t) free(t);
  29. }
  30. if (!d[i]->join(2000))
  31. traceft("m_dispatch join failed");
  32. d[i]->Release();
  33. d[i]=0;
  34. }
  35. }
  36. for (i=0; i<max_disp; i++)
  37. { if (!d[i])
  38. { d[i]=disp;
  39. d[i]->start();
  40. return;
  41. }
  42. }
  43. MilliSleep(1000);
  44. }
  45. }
  46. bool m_dispatch::all_done(bool no_abort)
  47. { try
  48. { bool all_d=false;
  49. traceft("all_done start\n");
  50. while (!all_d)
  51. { all_d=true;
  52. if (!no_abort&&dispatch_abort) return false;
  53. for (unsigned i=0; i<max_disp; i++)
  54. { if (d[i])
  55. { if (d[i]->is_done())
  56. { if (d[i]->ex1)
  57. { char * t=last_error;
  58. last_error=alloccat(d[i]->ex1,"\n",t,0);
  59. if (t) free(t);
  60. }
  61. if (!d[i]->join(2000))
  62. traceft("all_done join failed\n");
  63. d[i]->Release();
  64. d[i]=0;
  65. } else
  66. { all_d=false;
  67. //traceft("nd %i ",i);
  68. }
  69. }
  70. }
  71. //traceft("Sleep %i\n",all_d);
  72. MilliSleep(1000);
  73. }
  74. traceft("all_done end\n");
  75. } catch (...)
  76. { traceft("unknown exception in all_done\n");
  77. }
  78. return true;
  79. }
  80. bool user_continue() {
  81. keyboard kb;
  82. bool res=false;
  83. printf("\n ignore exception y/n\n");
  84. for (int i=10; i>0; i--) {
  85. printf(" %i \r",i);
  86. sleep(1);
  87. if(kb.kbhit()) {
  88. char t = kb.getch();
  89. if (t=='y'||t=='Y') {
  90. res=true;
  91. break;
  92. }
  93. else if (t=='n'||t=='N') {
  94. res=false;
  95. break;
  96. }
  97. }
  98. }
  99. printf("\n");
  100. return res;
  101. }
  102. bool m_dispatch::all_done_ex(bool no_abort)
  103. { bool res=all_done(no_abort);
  104. if (last_error&&!user_continue()) throw MakeStringException(1, last_error);
  105. return res;
  106. }
  107. void m_dispatch::stop(){};
  108. m_dispatch::m_dispatch(unsigned max_d)
  109. { max_disp=max_d;
  110. unsigned si=sizeof(c_dispatch *)*max_d;
  111. d=(c_dispatch**)malloc(si);
  112. memset(d,0,si);
  113. last_error=0;
  114. }
  115. m_dispatch::~m_dispatch()
  116. { free(d);
  117. if (last_error) free(last_error);
  118. }
  119. void m_dispatch::clear_error()
  120. { if (last_error) free(last_error);
  121. last_error=0;
  122. }
  123. bool c_dispatch::is_done()
  124. { bool res;
  125. cs.enter();
  126. res=done;
  127. cs.leave();
  128. return res;
  129. };
  130. c_dispatch::~c_dispatch()
  131. { if (ex1) free(ex1);
  132. };
  133. int c_dispatch::run()
  134. { try
  135. { action();
  136. } catch (char *s )
  137. { ex1=strdup(s);
  138. } catch(IHRPC_Exception *e)
  139. { StringBuffer s;
  140. ex1=alloccat(e->errorMessage(s).toCharArray()," ",id," ",0);
  141. e->Release();
  142. } catch(IJSOCK_Exception *e)
  143. { StringBuffer s;
  144. ex1=alloccat(e->errorMessage(s).toCharArray()," ",id," ",0);
  145. e->Release();
  146. }
  147. catch (IException *e)
  148. { StringBuffer s;
  149. ex1=alloccat(e->errorMessage(s).toCharArray()," ",id," ",0);
  150. e->Release();
  151. }
  152. catch (...)
  153. { traceft("unknown exception in c_dispatch::run\n");
  154. }
  155. if (ex1) traceft("%s exception %s\n",id?id:"",ex1);
  156. //traceft("c_dispatch run done\n");
  157. cs.enter();
  158. done=true;
  159. cs.leave();
  160. return 0;
  161. }