static struct chring *chring_tx_to_chring(struct chring_tx *ct) { struct chring *ring; int err = 0, i; ring = ring_alloc(ct->nr, ct->group); if (IS_ERR(ring)) { hvfs_err(xnet, "ring_alloc failed w/ %ld\n", PTR_ERR(ring)); return ring; } /* ok, let's copy the array to chring */ for (i = 0; i < ct->nr; i++) { err = ring_add_point_nosort(&ct->array[i], ring); if (err) { hvfs_err(xnet, "ring add point failed w/ %d\n", err); goto out; } } /* sort it */ ring_resort_nolock(ring); /* calculate the checksum of the CH ring */ lib_md5_print(ring->array, ring->alloc * sizeof(struct chp), "CHRING"); return ring; out: ring_free(ring); return ERR_PTR(err); }
void db_worker_start(char *path) { if (!db_enabled) { return; } ring_alloc(&ring, 1024); mtx_init(&mtx, mtx_plain); cnd_init(&cnd); thrd_create(&thrd, db_worker_run, path); }
void ring_grow(Ring *ring) { Ring new_ring; RingEntry entry; ring_alloc(&new_ring, ring->capacity * 2); while (ring_get(ring, &entry)) { ring_put(&new_ring, &entry); } free(ring->data); ring->capacity = new_ring.capacity; ring->start = new_ring.start; ring->end = new_ring.end; ring->data = new_ring.data; }
/* ring_add() add one site to the CH ring */ static int ring_add(struct chring **r, u64 site) { struct chp *p; char buf[256]; int vid_max, i, err; vid_max = hmo.conf.ring_vid_max ? hmo.conf.ring_vid_max : HVFS_RING_VID_MAX; if (!*r) { *r = ring_alloc(vid_max << 1, 0); if (!*r) { hvfs_err(xnet, "ring_alloc() failed.\n"); return -ENOMEM; } } p = (struct chp *)xzalloc(vid_max * sizeof(struct chp)); if (!p) { hvfs_err(xnet, "xzalloc() chp failed\n"); return -ENOMEM; } for (i = 0; i < vid_max; i++) { snprintf(buf, 256, "%ld.%d", site, i); (p + i)->point = hvfs_hash(site, (u64)buf, strlen(buf), HASH_SEL_VSITE); (p + i)->vid = i; (p + i)->type = CHP_AUTO; (p + i)->site_id = site; err = ring_add_point_nosort(p + i, *r); if (err) { hvfs_err(xnet, "ring_add_point() failed.\n"); return err; } } return 0; }
int main (int argc, char *argv[]) { /* * The protocol is double connection: * Control connection: TCP based connection for establishing calls * Data connection: UDP based connection for audio data transfer * One UDP socket to send audio data * Other UDP socket to receive audio data */ /* TODO: Add a menu in the main function * Based on the number input by user, jump to different state * The code for establishing control socket must be in a separate * function which is called based on the user input */ struct sockaddr_in own, other; int addrlen; int sockfd, newsockfd; struct connection_data conn; int rc; /* Threads for sending, receiving and playback */ pthread_t call; dbg("Creating RX ring buffer"); /* allocate enough space to store 512 periods or 2.56s of audio data * as each period is of 5ms size */ if (ring_alloc(&rbuff, SAMPLES_PER_PERIOD *4 * 512)) { fprintf(stderr, "Unable to allocate RX ring: %s \n", strerror(errno)); return EXIT_FAILURE; } ring_init(rbuff); printf("Created RX ring of size %d \n", ring_size(rbuff)); dbg("Creating TX ring buffer"); if (ring_alloc(&sbuff, SAMPLES_PER_PERIOD *4 * 512)) { fprintf(stderr, "Unable to allocated TX ring: %s \n", strerror(errno)); return EXIT_FAILURE; } ring_init(sbuff); printf("Created TX ring of size %d \n", ring_size(sbuff)); dbg("Creating control socket"); sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) { fprintf(stderr, "Unable to create socket\n"); return EXIT_FAILURE; } pthread_mutex_init(&rx_lock, NULL); pthread_mutex_init(&tx_lock, NULL); dbg("Control socket created"); /* populate sockaddr_in structure for server */ own.sin_family = AF_INET; own.sin_addr.s_addr = INADDR_ANY; /* convert from host to network byte order */ /* htons : host to network short */ own.sin_port = htons(PORT); dbg("Binding the control socket"); if (bind(sockfd, (struct sockaddr *)&own, sizeof(own))) { fprintf(stderr, "Unable to bind control socket \n"); return EXIT_FAILURE; } dbg("Control socket binded"); /* Start listening for incoming connections */ dbg("Listening for incoming connections"); listen(sockfd, 1); /* Allow only single connection */ addrlen = sizeof(other); printf("Waiting for connection \n"); while ((newsockfd = accept(sockfd, (struct sockaddr *)&other, &addrlen))) { /* Instantiate a connection_handler thread to * handle call */ conn.own = own; conn.other = other; conn.control_sock = newsockfd; pthread_create(&call, NULL, connection_handler, &conn); /* wait for call to finish before accepting another call */ printf("Call in progress\n"); pthread_join(call, NULL); /* TODO: add duration of call */ printf("Call ended \n"); printf("Waiting for connection \n"); } if (newsockfd < 0) { fprintf(stderr, "Accept failed \n"); exit(1); } return EXIT_SUCCESS; }
int main (int argc, char *argv[]) { /* * The protocol is double connection: * Control connection: TCP based connection for establishing calls * Data connection: UDP based connection for audio data transfer * One UDP socket to send audio data * Other UDP socket to receive audio data */ /* TODO: Add a menu in the main function * Based on the number input by user, jump to different state * The code for establishing control socket must be in a separate * function which is called based on the user input */ struct sockaddr_in server; int sockfd; char *server_ip; char buffer[BUFLEN]; struct connection_data conn; /* Threads for sending, receiving and playback */ pthread_t call; int rc; if (argc < 2) { fprintf(stderr, "Invalid arguments \n"); printf("Usage: control server_ip\n"); } server_ip = argv[1]; dbg("Creating RX ring buffer"); if (ring_alloc(&rbuff, SAMPLES_PER_PERIOD * 4 * 512)) { fprintf(stderr, "Unable to allocate RX ring: %s \n", strerror(errno)); return EXIT_FAILURE; } ring_init(rbuff); printf("Created RX ring of size %d \n", ring_size(rbuff)); dbg("Creating TX ring buffer"); if (ring_alloc(&sbuff, SAMPLES_PER_PERIOD * 4 * 512)) { fprintf(stderr, "Unable to allocated TX ring: %s \n", strerror(errno)); return EXIT_FAILURE; } ring_init(sbuff); printf("Created TX ring of size %d \n", ring_size(sbuff)); dbg("Creating control socket"); sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) { fprintf(stderr, "Unable to create socket\n"); return EXIT_FAILURE; } pthread_mutex_init(&rx_lock, NULL); pthread_mutex_init(&tx_lock, NULL); dbg("Control socket created"); /* zeroize sockaddr_in structure for server*/ memset((char *)&server, 0, sizeof server); /* populate sockaddr_in structure for server */ server.sin_family = AF_INET; /* convert from host to network byte order */ /* htons : host to network short */ server.sin_port = htons(OTHER_PORT); /* set server ip address */ if (inet_aton(server_ip, &server.sin_addr) == 0) { fprintf(stderr, "inet_aton() failed \n"); close(sockfd); return EXIT_FAILURE; } conn.other = server; dbg("Trying to connect to server"); printf("Trying to connect %s \n", server_ip); if (connect(sockfd, (struct sockaddr *)&server, sizeof(server)) < 0) { fprintf(stderr, "Unable to connect to server: %s \n", strerror(errno)); close(sockfd); return EXIT_FAILURE; } printf("Connection established with server %s\n", server_ip); conn.control_sock = sockfd; dbg("Trying to make call"); memset(buffer, 0, sizeof buffer); sprintf(buffer, "CALL"); rc = write(sockfd, buffer, strlen(buffer)); if (rc < 0) { fprintf(stderr, "Unable to write to socket \n"); close(sockfd); return EXIT_FAILURE; } memset(buffer, 0, sizeof buffer); rc = read(sockfd, buffer, sizeof buffer); if (rc < 0) { fprintf(stderr, "Error reading from socket \n"); close(sockfd); return EXIT_FAILURE; } printf("The return code: %s \n", buffer); if (!strncmp("OK", buffer, strlen(buffer))) { /* Other side returned OK */ dbg("Call established"); /* Instantiate a connection_handler thread to * handle call */ pthread_create(&call, NULL, connection_handler, &conn); } printf("Call in progress\n"); pthread_join(call, NULL); /* TODO: add duration of call */ printf("Call ended \n"); return EXIT_SUCCESS; }