aboutsummaryrefslogtreecommitdiff
path: root/snowcast_server_concurrent.c
diff options
context:
space:
mode:
authorsotech117 <michael_foiani@brown.edu>2023-09-18 10:07:28 -0400
committersotech117 <michael_foiani@brown.edu>2023-09-18 10:07:28 -0400
commit5236560176cfe8e4d06be4812719037937b7f4dc (patch)
tree721b5d9759b67b7bb7cc3f81e1622f90c318bbcf /snowcast_server_concurrent.c
parentc77bc555ba1cc3b43f19b6a76b563d67eee2c347 (diff)
rever back to pthread_cancel
Diffstat (limited to 'snowcast_server_concurrent.c')
-rw-r--r--snowcast_server_concurrent.c110
1 files changed, 19 insertions, 91 deletions
diff --git a/snowcast_server_concurrent.c b/snowcast_server_concurrent.c
index 0586635..903b3fd 100644
--- a/snowcast_server_concurrent.c
+++ b/snowcast_server_concurrent.c
@@ -55,7 +55,7 @@ void *init_user(int sockfd);
void *update_user_udpPort(int sockfd, int udpPort);
void *update_user_station(int sockfd, int stationNum);
void *print_user_data(int sockfd);
-void *destroy_user(int sockfd);
+void destroy_user(int sockfd);
// void *load_file(void* arg);
@@ -107,68 +107,36 @@ main(int argc, char *argv[])
/* Make the manager routine */
void *send_udp_packet_routine(void *arg) {
- // pthread_mutex_lock(&mutex);
- // while(startThreads) {
- // pthread_cond_wait(&cond, &mutex);
- // }
- // pthread_mutex_unlock(&mutex);
- int did_send_data = 0;
- int did_load_data = 0;
-
pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
+ int did_work = 1;
+
int * i = (int *) arg;
while (1)
{
+ // wait for
pthread_mutex_lock(&m);
- if (!start_threads && did_send_data && did_load_data) {
- did_load_data = 0;
- did_send_data = 0;
- }
- while(!start_threads) {
+ did_work = 0;
+ while (!start_threads)
+ {
pthread_cond_wait(&cond, &m);
}
- pthread_mutex_unlock(&m);
- if(!did_send_data) {
+
+ if (!did_work) {
printf("send data: thread %d \n", i);
- did_send_data = 1;
- }
- if(!did_load_data) {
printf("load data: thread %d \n", i);
- did_load_data = 1;
+ did_work = 1;
}
+ pthread_mutex_unlock(&m);
+ usleep(500000);
+ // pthread_mutex_lock(&mutex);
+ // start_threads = 0;
+ // pthread_mutex_unlock(&mutex);
}
return NULL;
}
-// /* Make the manager routine */
-// void *load_file(void *arg) {
-// // read first data off the files
-// pthread_mutex_lock(&mutex);
-// while(startThreads) {
-// pthread_cond_wait(&cond, &mutex);
-// }
-// pthread_mutex_unlock(&mutex);
-
-// int * i = (int *) arg;
-// while (1)
-// {
-// pthread_mutex_lock(&mutex);
-// while(startThreads) {
-// pthread_cond_wait(&cond, &mutex);
-// }
-// pthread_mutex_unlock(&mutex);
-// /* Do some work. */
-// printf("Thread %d \n", i);
-// // send data to port
-// // read data coming in off the file
-// // sleep for a secong
-// sleep(1);
-// }
-// return NULL;
-// }
-
void *synchronization_thread(void *arg) {
int c = 0;
while (1)
@@ -176,10 +144,9 @@ void *synchronization_thread(void *arg) {
start_threads = 1;
printf("\nbroadcast %d\n", c++);
pthread_cond_broadcast(&cond);
- usleep(1000);
+ usleep(2000);
start_threads = 0;
- sleep(1);
-
+ usleep(1000000-2000);
}
}
@@ -319,46 +286,6 @@ void *select_thread(void *arg) {
printf("udpPort (from Hello) for new connection is %d.\n", ntohs(command.number));
// update udp port of user
update_user_udpPort(i, ntohs(command.number));
-
-
-
- // // TALKER: get us a udp socket and bind it
- // struct addrinfo hintsUdp, *servinfoUdp, *pUdp;
- // int rvUdp, sockfdUdp, numbytesUdp;
- // memset(&hintsUdp, 0, sizeof hintsUdp);
- // hintsUdp.ai_family = AF_INET; // IPv4
- // hintsUdp.ai_socktype = SOCK_DGRAM; // UDP
- // if ((rvUdp = getaddrinfo(argv[1], command.number, &hints, &servinfoUdp)) != 0) {
- // fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rvUdp));
- // return 1;
- // }
-
- // // loop through all the results and make a socket
- // for(p = servinfoUdp; p != NULL; p = p->ai_next) {
- // if ((sockfdUdp = socket(p->ai_family, p->ai_socktype,
- // p->ai_protocol)) == -1) {
- // perror("talker: socket");
- // continue;
- // }
- // break;
- // }
-
- // if (p == NULL) {
- // fprintf(stderr, "talker: failed to create socket\n");
- // return 2;
- // }
-
-
- // if ((numbytesUdp = sendto(sockfdUdp, "test", strlen("test"), 0,
- // p->ai_addr, p->ai_addrlen)) == -1) {
- // perror("talker: sendto");
- // exit(1);
- // }
-
- // freeaddrinfo(servinfoUdp);
-
- // printf("talker: sent %d bytes to %d\n", numbytesUdp, sockfdUdp);
- // close(sockfdUdp);
}
else if (command.commandType == 1) {
// setStation command for the user
@@ -429,11 +356,12 @@ void *print_user_data(int index) {
printf("udpPort: %d, stationNum: %d, sockfd: %d, threadId:%d\n",
user_data[index].udpPort, user_data[index].stationNum, user_data[index].sockfd, user_data[index].streamThread);
}
-void *destroy_user(int sockfd) {
+void destroy_user(int sockfd) {
pthread_mutex_lock(&mutex_user_data);
// stop the thread streaming to the user
pthread_cancel(user_data[sockfd_to_user[sockfd]].streamThread);
+ // pthread_kill(user_data[sockfd_to_user[sockfd]].streamThread, SIGINT);
// "remove" the user from the list of user data
user_data[sockfd_to_user[sockfd]] = (user_t) {-1, -1, -1, -1};
// map sockfd to -1