int conn_insert(int sd) { conn_t **v = vector; size_t i; for (i = 0; i < MAX_CONNECTIONS; i++) if (v[i] == NULL) { if ((v[i] = mkconn(sd)) == NULL) { close(sd); return -1; } return 0; } close(sd); return -1; }
void runcb(int fd, short what, void *arg) { Run *run; Request *r; Call *c; Header *h; struct evhttp_connection *conn; struct evhttp_request *req; enum evhttp_cmd_type cmd; int i; run = (Run*)arg; r = &run->rs[rand() % run->rsiz]; if(run->cachedconn!=nil){ conn = run->cachedconn; run->cachedconn = nil; }else conn = mkconn(run->host, run->port); c = mal(sizeof(*c)); c->run = run; c->conn = conn; req = evhttp_request_new(&donecb, c); if(strcasecmp(r->action, "get")==0) cmd = EVHTTP_REQ_GET; else if(strcasecmp(r->action, "post")==0) cmd = EVHTTP_REQ_POST; else panic("invalid action \"%s\"", r->action); for(i=0;i<r->nheader;i++){ h = &r->headers[i]; evhttp_add_header( req->output_headers, h->key, h->value); } evhttp_make_request(conn, req, cmd, r->uri); event_add(&run->ev, &run->tv); }
// calcul f1() int main(int argc, char *argv[]) { char buf[BUF_SIZE]; char *shost, *sport; int sfd, nrpar; double x, y, z, f1; ssize_t nread; // stăpân: nume sau adresă IP if (argc < 2) shost = HOST; else shost = argv[1]; // stăpân: număr port TCP if (argc < 3) sport = PORT; else sport = argv[2]; #ifndef NDEBUG printf("I mapsu_cf1: utilizează server «%s:%s»\n", shost, sport); #endif sfd = mkconn(shost, sport); if (-1 == sfd) { fprintf(stderr, "mapsu_cf1: conectarea la «%s:%s» a eșuat!\n", shost, sport); exit(EXIT_FAILURE); } nrpar = 0; while ((nread = recv(sfd, buf, BUF_SIZE-1, 0)) > 0) { if (buf[nread-1] == '\n') buf[nread-1] = '\0'; else buf[nread] = '\0'; if ('\0' == buf[0]) continue; #ifndef NDEBUG //printf("I mapsu_cf1: recv %s\n", buf); #endif nread = sscanf(buf, "%lg %lg %lg", &x, &y, &z); if (3 == nread) { f1 = log10(y/z) + 1; #ifndef NDEBUG if (1 == nrpar % 2) printf("I mapsu_cf1: send f1=%.26lf\n", f1); usleep(200000); #endif sprintf(buf, "f1: %.26lf\n", f1); if (send(sfd, buf, strlen(buf), 0) == -1) { perror("send: "); exit(EXIT_FAILURE); } ++nrpar; #ifndef NDEBUG } else { printf("I mapsu_cf1: recv %s\n", buf); printf("W mapsu_cf1: nu am citit 3 coeficienți.. ignorat\n"); #endif } } return 0; }