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