starputop_connection.c 4.0 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. #include <starpu_config.h>
  18. #ifdef STARPU_HAVE_WINDOWS
  19. # include <w32api.h>
  20. # define WINVER WindowsXP
  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/starputop_connection.h>
  28. #include <top/starputop_message_queue.h>
  29. #include <starpu_top.h>
  30. #include <pthread.h>
  31. #include <stdio.h>
  32. #include <string.h>
  33. #include <sys/types.h>
  34. #include <stdlib.h>
  35. #include <unistd.h>
  36. const char *STARPUTOP_PORT = "2011";
  37. const int STARPUTOP_BUFFER_SIZE=1024;
  38. extern starputop_message_queue_t* starputop_mt;
  39. //client socket after fopen
  40. FILE* starputop_socket_fd_read;
  41. FILE* starputop_socket_fd_write;
  42. //client socket (file descriptor)
  43. int starputop_socket_fd;
  44. void * message_from_ui(void * p)
  45. {
  46. (void) p;
  47. char str[STARPUTOP_BUFFER_SIZE];
  48. while(1)
  49. {
  50. char * check=fgets (str, STARPUTOP_BUFFER_SIZE, starputop_socket_fd_read);
  51. printf("Message from UI : %s",str);
  52. if (check)
  53. {
  54. starputop_process_input_message(str);
  55. }
  56. else
  57. {
  58. fprintf(stderr,"Connection dropped\n");
  59. //unlocking StarPU.
  60. starputop_process_input_message("GO\n");
  61. starputop_process_input_message("DEBUG;OFF\n");
  62. starputop_process_input_message("STEP\n");
  63. return NULL;
  64. }
  65. }
  66. return NULL;
  67. }
  68. void * message_to_ui(void * p)
  69. {
  70. (void) p;
  71. while(1)
  72. {
  73. char* message = starputop_message_remove(starputop_mt);
  74. int len=strlen(message);
  75. int check=fwrite(message, sizeof(char), len, starputop_socket_fd_write);
  76. int check2=fflush(starputop_socket_fd_write);
  77. free(message);
  78. if (check!=len || check2==EOF )
  79. {
  80. fprintf(stderr,"Connection dropped : message no longer send\n");
  81. while(1)
  82. {
  83. message=starputop_message_remove(starputop_mt);
  84. free(message);
  85. }
  86. }
  87. }
  88. return NULL;
  89. }
  90. void starputop_communications_threads_launcher()
  91. {
  92. pthread_t from_ui;
  93. pthread_t to_ui;
  94. pthread_attr_t threads_attr;
  95. //Connection to UI & Socket Initilization
  96. printf("%s:%d Connection to UI initilization\n",__FILE__, __LINE__);
  97. struct sockaddr_storage from;
  98. struct addrinfo req, *ans;
  99. int code;
  100. req.ai_flags = AI_PASSIVE;
  101. req.ai_family = PF_UNSPEC;
  102. req.ai_socktype = SOCK_STREAM;
  103. req.ai_protocol = 0;
  104. if ((code = getaddrinfo(NULL, STARPUTOP_PORT, &req, &ans)) != 0)
  105. {
  106. fprintf(stderr, " getaddrinfo failed %d\n", code);
  107. exit(EXIT_FAILURE);
  108. }
  109. int sock=socket(ans->ai_family, ans->ai_socktype, ans->ai_protocol);
  110. int optval = 1;
  111. setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval));
  112. if (bind(sock, ans->ai_addr, ans->ai_addrlen) < 0)
  113. {
  114. perror("bind");
  115. exit(EXIT_FAILURE);
  116. }
  117. listen(sock, 2);
  118. socklen_t len = sizeof(from);
  119. if ((starputop_socket_fd=accept(sock, (struct sockaddr *) &from, &len)) ==-1)
  120. {
  121. fprintf(stderr, "accept error\n");
  122. perror("accept");
  123. exit(EXIT_FAILURE);
  124. }
  125. if ( (starputop_socket_fd_read=fdopen(starputop_socket_fd, "r")) == NULL)
  126. {
  127. perror("fdopen");
  128. exit(EXIT_FAILURE);
  129. }
  130. starputop_socket_fd=dup(starputop_socket_fd);
  131. if ((starputop_socket_fd_write=fdopen(starputop_socket_fd, "w")) == NULL)
  132. {
  133. perror("fdopen");
  134. exit(EXIT_FAILURE);
  135. }
  136. close(sock);
  137. //Threads creation
  138. fprintf(stderr,"Threads Creation\n");
  139. pthread_attr_init(&threads_attr);
  140. pthread_attr_setdetachstate(&threads_attr, PTHREAD_CREATE_DETACHED);
  141. pthread_create(&from_ui, &threads_attr, message_from_ui, NULL);
  142. pthread_create(&to_ui, &threads_attr, message_to_ui, NULL);
  143. }