starpu_top_connection.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011 William Braik, Yann Courtois, Jean-Marie Couteyen, Anthony
  4. * Roy
  5. *
  6. * StarPU is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2.1 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * StarPU is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. *
  15. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. */
  17. #define WINVER WindowsXP
  18. #include <starpu_config.h>
  19. #ifdef STARPU_HAVE_WINDOWS
  20. # include <w32api.h>
  21. # include <ws2tcpip.h>
  22. #else
  23. # include <sys/socket.h>
  24. # include <netinet/in.h>
  25. # include <netdb.h>
  26. #endif
  27. #include <top/starpu_top_core.h>
  28. #include <top/starpu_top_connection.h>
  29. #include <top/starpu_top_message_queue.h>
  30. #include <starpu_top.h>
  31. #include <common/utils.h>
  32. #include <stdio.h>
  33. #include <string.h>
  34. #include <sys/types.h>
  35. #include <stdlib.h>
  36. #include <unistd.h>
  37. const char *STARPU_TOP_PORT = "2011";
  38. const int STARPU_TOP_BUFFER_SIZE=1024;
  39. //client socket after fopen
  40. FILE* starpu_top_socket_fd_read;
  41. FILE* starpu_top_socket_fd_write;
  42. //client socket (file descriptor)
  43. int starpu_top_socket_fd;
  44. static
  45. void * message_from_ui(void * p)
  46. {
  47. (void) p;
  48. char str[STARPU_TOP_BUFFER_SIZE];
  49. while(1)
  50. {
  51. char * check=fgets (str, STARPU_TOP_BUFFER_SIZE, starpu_top_socket_fd_read);
  52. printf("Message from UI : %s",str);
  53. if (check)
  54. {
  55. _starpu_top_process_input_message(str);
  56. }
  57. else
  58. {
  59. fprintf(stderr,"Connection dropped\n");
  60. //unlocking StarPU.
  61. _starpu_top_process_input_message("GO\n");
  62. _starpu_top_process_input_message("DEBUG;OFF\n");
  63. _starpu_top_process_input_message("STEP\n");
  64. return NULL;
  65. }
  66. }
  67. }
  68. static
  69. void * message_to_ui(void * p)
  70. {
  71. (void) p;
  72. while(1)
  73. {
  74. char* message = _starpu_top_message_remove(_starpu_top_mt);
  75. int len=strlen(message);
  76. int check=fwrite(message, sizeof(char), len, starpu_top_socket_fd_write);
  77. int check2=fflush(starpu_top_socket_fd_write);
  78. free(message);
  79. if (check!=len || check2==EOF )
  80. {
  81. fprintf(stderr,"Connection dropped : message no longer send\n");
  82. while(1)
  83. {
  84. message=_starpu_top_message_remove(_starpu_top_mt);
  85. free(message);
  86. }
  87. }
  88. }
  89. return NULL;
  90. }
  91. void _starpu_top_communications_threads_launcher(void)
  92. {
  93. starpu_pthread_t from_ui;
  94. starpu_pthread_t to_ui;
  95. starpu_pthread_attr_t threads_attr;
  96. //Connection to UI & Socket Initilization
  97. printf("%s:%d Connection to UI initilization\n",__FILE__, __LINE__);
  98. struct sockaddr_storage from;
  99. struct addrinfo req, *ans;
  100. int code;
  101. req.ai_flags = AI_PASSIVE;
  102. req.ai_family = PF_UNSPEC;
  103. req.ai_socktype = SOCK_STREAM;
  104. req.ai_protocol = 0;
  105. if ((code = getaddrinfo(NULL, STARPU_TOP_PORT, &req, &ans)) != 0)
  106. {
  107. fprintf(stderr, " getaddrinfo failed %d\n", code);
  108. exit(EXIT_FAILURE);
  109. }
  110. int sock=socket(ans->ai_family, ans->ai_socktype, ans->ai_protocol);
  111. int optval = 1;
  112. setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval));
  113. if (bind(sock, ans->ai_addr, ans->ai_addrlen) < 0)
  114. {
  115. perror("bind");
  116. exit(EXIT_FAILURE);
  117. }
  118. listen(sock, 2);
  119. socklen_t len = sizeof(from);
  120. if ((starpu_top_socket_fd=accept(sock, (struct sockaddr *) &from, &len)) ==-1)
  121. {
  122. fprintf(stderr, "accept error\n");
  123. perror("accept");
  124. exit(EXIT_FAILURE);
  125. }
  126. if ( (starpu_top_socket_fd_read=fdopen(starpu_top_socket_fd, "r")) == NULL)
  127. {
  128. perror("fdopen");
  129. exit(EXIT_FAILURE);
  130. }
  131. starpu_top_socket_fd=dup(starpu_top_socket_fd);
  132. if ((starpu_top_socket_fd_write=fdopen(starpu_top_socket_fd, "w")) == NULL)
  133. {
  134. perror("fdopen");
  135. exit(EXIT_FAILURE);
  136. }
  137. close(sock);
  138. //Threads creation
  139. fprintf(stderr,"Threads Creation\n");
  140. starpu_pthread_attr_init(&threads_attr);
  141. starpu_pthread_attr_setdetachstate(&threads_attr, PTHREAD_CREATE_DETACHED);
  142. STARPU_PTHREAD_CREATE(&from_ui, &threads_attr, message_from_ui, NULL);
  143. STARPU_PTHREAD_CREATE(&to_ui, &threads_attr, message_to_ui, NULL);
  144. }