1#include "locktests.h" 2 3#include <netdb.h> 4#include <string.h> 5#define PORT 12346 6#define MAX_CONNECTION 16 7 8int maxClients; 9int *fdClient; 10char *server_name; 11int fdServer; 12extern char message[M_SIZE]; 13 14int serverReceiveClient(int c) 15{ 16 char tmp[M_SIZE]; 17 int r, s; 18 /* Ensure we read _exactly_ M_SIZE characters in the message */ 19 memset(message, 0, M_SIZE); 20 memset(tmp, 0, M_SIZE); 21 r = 0; 22 s = 0; 23 24 while (s < M_SIZE) { 25 r = read(fdClient[c], tmp, M_SIZE - s); 26 /* Loop until we have a complete message */ 27 strncpy(message + s, tmp, r); 28 s += r; 29 } 30 return s; 31} 32 33int serverSendClient(int n) 34{ 35 return write(fdClient[n], message, M_SIZE); 36} 37 38int clientReceiveNet(void) 39{ 40 readFromServer(message); 41 return 0; 42} 43 44int setupConnectionServer(void) 45{ 46 struct sockaddr_in local; 47 int c; 48 socklen_t size; 49 int sock; 50 struct sockaddr_in remote; 51 52 if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) { 53 perror("socket"); 54 exit(1); 55 } 56 memset(&local, 0x00, sizeof(local)); 57 local.sin_family = AF_INET; 58 local.sin_port = htons(PORT); 59 local.sin_addr.s_addr = INADDR_ANY; 60 memset(&(local.sin_zero), 0x00, 8); 61 62 if (bind(sock, (struct sockaddr *)&local, sizeof(struct sockaddr)) == 63 -1) { 64 perror("bind"); 65 exit(1); 66 } 67 68 if (listen(sock, MAX_CONNECTION) == -1) { 69 perror("listen"); 70 return 1; 71 } 72 size = sizeof(struct sockaddr_in); 73 for (c = 0; c < maxClients; c++) { 74 if ((fdClient[c] = 75 accept(sock, (struct sockaddr *)&remote, &size)) == -1) { 76 perror("accept"); 77 return 1; 78 } 79 80 } 81 return 0; 82} 83 84int writeToClient(int c, char *message) 85{ 86 return write(fdClient[c], message, 512); 87} 88 89int serverCloseConnection(void) 90{ 91 int c; 92 for (c = 0; c < maxClients; c++) 93 close(fdClient[c]); 94 return 0; 95 96} 97 98int writeToAllClients(char *foo) 99{ 100 int c; 101 for (c = 0; c < maxClients; c++) 102 writeToClient(c, foo); 103 return 0; 104} 105 106int setupClients(int type, char *fname, int nThread) 107{ 108 /* 109 * Send parameters to all slaves : 110 * 111 * We must send : 112 * - the position of the test file 113 * - the number of slaves for each client 114 * - The kind of slaves : process or thread 115 */ 116 char message[512]; 117 sprintf(message, "%d:%s:%d::", type, fname, nThread); 118 writeToAllClients(message); 119 return 0; 120} 121 122int configureServer(int max) 123{ 124 maxClients = max; 125 fdClient = malloc(sizeof(int) * max); 126 127 setupConnectionServer(); 128 129 return 0; 130} 131 132int setupConnectionClient(void) 133{ 134 135 struct hostent *server; 136 struct sockaddr_in serv_addr; 137 138 if (!(server = gethostbyname(server_name))) { 139 printf("erreur DNS\n"); 140 return 1; 141 } 142 143 fdServer = socket(AF_INET, SOCK_STREAM, 0); 144 if (fdServer < 0) { 145 perror("socket"); 146 return 1; 147 } 148 149 serv_addr.sin_addr = *(struct in_addr *)server->h_addr; 150 serv_addr.sin_port = htons(PORT); 151 serv_addr.sin_family = AF_INET; 152 if (connect(fdServer, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) 153 < 0) { 154 perror("connect"); 155 return 1; 156 } 157 return 0; 158} 159 160int readFromServer(char *message) 161{ 162 char tmp[M_SIZE]; 163 int r, s; 164 /* Ensure we read exactly M_SIZE characters */ 165 memset(message, 0, M_SIZE); 166 memset(tmp, 0, M_SIZE); 167 r = 0; 168 s = 0; 169 while (s < M_SIZE) { 170 r = read(fdServer, tmp, M_SIZE - s); 171 /* Loop until we have a complete message */ 172 strncpy(message + s, tmp, r); 173 s += r; 174 } 175 return s; 176} 177 178int getConfiguration(int *type, char *fname, int *nThread) 179{ 180 char conf[M_SIZE]; 181 char *p; 182 int i; 183 readFromServer(conf); 184 p = strtok(conf, ":"); 185 printf("%s\n", p); 186 *type = atoi(p); 187 p = strtok(NULL, ":"); 188 i = strlen(p); 189 strncpy(fname, p, i); 190 p = strtok(NULL, ":"); 191 *nThread = atoi(p); 192 193 return 0; 194} 195 196int configureClient(char *s) 197{ 198 server_name = s; 199 setupConnectionClient(); 200 return 0; 201} 202