Advertisement
Guest User

Untitled

a guest
Dec 4th, 2014
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.10 KB | None | 0 0
  1. module marscamera_c_interface;
  2. /+
  3. + Minimised code for reproducing PyD segfaults from threading.
  4. +
  5. + Author: Michael Walsh
  6. +/
  7. import core.exception: AssertError;
  8.  
  9. import std.concurrency: spawn, Tid, thisTid, send, receive, receiveTimeout, OwnerTerminated, register, locate;
  10. import std.datetime: dur;
  11.  
  12. shared string last_error = "";
  13.  
  14. bool connected = false;
  15.  
  16.  
  17. int _acquire(int exposure_time_ms);
  18. int _get_image(uint* matrix); // 65536 length
  19. int _camera_close();
  20.  
  21.  
  22. //acquire
  23. void message_handler1(Tid thr, string cmd, float var1) {
  24. if (cmd == "acquire") {
  25. int ret = _acquire(var1);
  26. send!(string, int)(thr, cmd, ret);
  27. }
  28. }
  29.  
  30.  
  31. //get_image, close
  32. void message_handler5(Tid thr, string cmd) {
  33. if (cmd == "get_image") {
  34. uint[65536] image;
  35. uint* tmp = cast(uint*)image;
  36. int ret = _get_image(tmp);
  37. send!(string, int, uint[65536])(thr, cmd, ret, image);
  38. } else if (cmd == "camera_close") {
  39. int ret = _camera_close();
  40. if (ret) {
  41. connected = false;
  42. }
  43. send!(string, int)(thr, cmd, ret);
  44. }
  45. }
  46.  
  47.  
  48. void listener(Tid t) {
  49. send(t, "find", 1);
  50. while (connected) {
  51. receive(&message_handler1, // (Tid, string, float)
  52. &message_handler5,
  53. (OwnerTerminated s) {connected = false;}); // (Tid, string)
  54. }
  55. }
  56.  
  57. void _camera_find(Tid t, string ip_address) {
  58. try {
  59. connected = true;
  60. listener(t);
  61. } catch (Exception e) {
  62. last_error = e.msg.idup;
  63. } catch (AssertError e) {
  64. last_error = "Assertion error" ~ e.msg.idup;
  65. }
  66.  
  67. }
  68.  
  69. int camera_find(string ip) {
  70. scope(failure) {last_error = "Exception in camera_find"; return 0;}
  71. if (locate("listener") == Tid.init) {
  72. Tid listener_thread = spawn(&_camera_find, thisTid(), ip);
  73. register("listener", listener_thread);
  74. int ret = -1;
  75. receiveTimeout(dur!"msecs"(5000),
  76. (string s, int _ret) {
  77. assert(s == "find");
  78. ret = _ret;
  79. });
  80. if (ret == -1) {
  81. last_error = "Timeout on camera find";
  82. return 0;
  83. } else {
  84. return ret;
  85. }
  86. } else {
  87. last_error = "camera_find exception: camera already connected";
  88. return 0;
  89. }
  90. }
  91.  
  92.  
  93. int _acquire(float exposure_time_ms) {
  94. //The real version of this code acquires on the camera, which has approximately 500 ms of delay.
  95. return 1;
  96. }
  97.  
  98. int acquire(float exposure_time_ms) {
  99. try {
  100. auto listener_thread = locate("listener");
  101. if (listener_thread != Tid.init) {
  102. send!(Tid, string, float)(listener_thread, thisTid(), "acquire", exposure_time_ms);
  103. int ret = -1;
  104. receiveTimeout(dur!"msecs"(3000),
  105. (string s, int _ret) {
  106. assert(s == "acquire");
  107. ret = _ret;
  108. });
  109. if (ret == -1) {
  110. last_error = "Timeout on receive response";
  111. return 0;
  112. } else {
  113. return ret;
  114. }
  115. } else {
  116. last_error = "Camera not initialised with 'find'";
  117. return 0;
  118. }
  119. } catch (Exception e) {
  120. last_error = e.msg.idup;
  121. return 0;
  122. } catch (AssertError e) {
  123. last_error = "Assertion error" ~ e.msg.idup;
  124. return 0;
  125. }
  126. }
  127.  
  128. int _get_image(uint *matrix) {
  129. // The real version of this code overwrites *matrix with a 65536-long array of data.
  130. return 1;
  131. }
  132.  
  133. int get_image(uint *matrix) {
  134. auto listener_thread = locate("listener");
  135. if (listener_thread != Tid.init) {
  136. send!(Tid, string)(listener_thread, thisTid(), "get_image");
  137. int ret = -1;
  138. receiveTimeout(dur!"msecs"(1000),
  139. (string s, int _ret, uint[65536] _matrix) {
  140. assert(s == "get_image");
  141. foreach (i; 0..65536) {
  142. matrix[i] = _matrix[i];
  143. }
  144. ret = _ret;
  145. });
  146. if (ret == -1) {
  147. last_error = "get_image: Timeout on receive response";
  148. return 0;
  149. } else {
  150. return ret;
  151. }
  152. } else {
  153. last_error = "Camera not initialised with 'find'";
  154. return 0;
  155. }
  156. }
  157.  
  158. int _camera_close() {
  159. // The real version of this code closes the socket connection.
  160. return 1;
  161. }
  162.  
  163. int camera_close() {
  164. auto listener_thread = locate("listener");
  165. if (listener_thread != Tid.init) {
  166. send!(Tid, string)(listener_thread, thisTid(), "camera_close");
  167. int ret = -1;
  168. receiveTimeout(dur!"msecs"(1000),
  169. (string s, int _ret) {
  170. assert(s == "camera_close");
  171. ret = _ret;
  172. });
  173. if (ret == -1) {
  174. last_error = "camera_close: Timeout on receive response";
  175. return 0;
  176. } else {
  177. return ret;
  178. }
  179. } else {
  180. last_error = "Camera not initialised with 'find'";
  181. return 0;
  182. }
  183. }
  184.  
  185. void __throw_last_error() {
  186. throw new Exception(last_error);
  187. }
  188.  
  189. /+
  190. + Code used for the PyD interface.
  191. +/
  192.  
  193. void __camera_find(string ip_address) {
  194. scope(failure) __throw_last_error();
  195. if (!camera_find(ip_address)) {
  196. __throw_last_error();
  197. }
  198. }
  199.  
  200. void __camera_close() {
  201. scope(failure) __throw_last_error();
  202. if (!camera_close()) {
  203. __throw_last_error();
  204. }
  205. }
  206.  
  207. void __acquire(float exposure_time_ms) {
  208. scope(failure) __throw_last_error();
  209. if (!acquire(exposure_time_ms)) {
  210. __throw_last_error();
  211. }
  212. }
  213.  
  214. uint[65536] __get_image() {
  215. scope(failure) __throw_last_error();
  216. uint[65536] matrix;
  217. if (!get_image(cast(uint*)matrix)) {
  218. __throw_last_error();
  219. }
  220. return matrix;
  221. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement