Guest User

Untitled

a guest
Apr 24th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.96 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <signal.h>
  5. #include <unistd.h>
  6. #include <sys/types.h>
  7. #include <sys/ipc.h>
  8. #include <sys/msg.h>
  9. #include <pthread.h>
  10. #include "shared_data.h"
  11.  
  12. #define MAX 999
  13.  
  14. /* removed some structs */
  15.  
  16. typedef struct {
  17.     pthread_t thread;
  18.     int status;
  19. } thread;
  20.  
  21. typedef struct {
  22.     int key;
  23.     thread * t;
  24. } threadInfo;
  25.  
  26. student db[MAX];
  27. int size;
  28.  
  29. int id; // main message queue
  30.  
  31. int currentConnections;
  32. int maxConnections;
  33. int flushTimer;
  34. unsigned int lastKey;
  35. char * databaseFile;
  36.  
  37. /* There were more methods here, but I removed them for the sake of saving space */
  38.  
  39. void saveData() {
  40.     FILE * fp;
  41.     fp = fopen(databaseFile, "wb");
  42.     fwrite(db, sizeof(student), size, fp);
  43.     fclose(fp);
  44. }
  45.  
  46. void alarmHandler(int signum){
  47.     saveData();
  48.     alarm(5);
  49. }
  50.  
  51. void ctrlcHandler(int signum) {
  52.     char c = 'a';
  53.     printf("\nExit? (y/n): ");
  54.     while(c != 'y' && c != 'n')
  55.         scanf("%c",&c);
  56.     if (c == 'y') {
  57.         saveData();
  58.         msgctl(id, IPC_RMID, NULL);
  59.         exit(1);
  60.     }
  61. }
  62.  
  63. void * client(void * t) {
  64.     threadInfo * t_info = (threadInfo *)t;
  65.     printf("i am the thread with msgq %d\n", t_info->key);
  66.     int msg = msgget(t_info->key, IPC_CREAT|0666);
  67.     CommandRequest req;
  68.     msgrcv(msg, &req, sizeof(CommandRequest), CLIENT_TAG, 0);
  69.     switch(req.c) {
  70.         case DISCONNECT:
  71.             msgctl(msg, IPC_RMID, NULL);
  72.             printf("client disconnected\n");
  73.             t_info->t->status = 0;
  74.             currentConnections--;
  75.         break;
  76.     }
  77.     return NULL;
  78. }
  79.  
  80. void run(){
  81.     thread threads[maxConnections];
  82.     int k = 0;
  83.     for(; k < maxConnections ; k++) threads[k].status = 1;
  84.     if (signal(SIGALRM, alarmHandler) == SIG_ERR) {
  85.         perror("Error installing SIGALRM signal handler");
  86.     }
  87.     if (signal(SIGINT, ctrlcHandler) == SIG_ERR) {
  88.         perror("Error installing SIGINT signal handler");
  89.     }
  90.     alarm(flushTimer);
  91.     id = msgget(PUBLIC_KEY, IPC_CREAT|0666);
  92.     if (id == -1) {
  93.         perror("msgget");
  94.         exit(1);
  95.     }
  96.    
  97.     while(1) {
  98.         CommandRequest req;
  99.         msgrcv(id, &req, sizeof(CommandRequest), CLIENT_TAG, 0); //receive connect request
  100.  
  101.        
  102.         printf("\"%c\"\n", req.c);
  103.         if (req.c == CONNECT) {
  104.             ConnectResponse resp;
  105.             if (currentConnections < maxConnections) {
  106.                 printf("client has connected\n");
  107.                 int i = 0;
  108.                 resp.c = OK;
  109.                 resp.key = lastKey++;
  110.                 int clientmsgq = msgget(resp.key, IPC_CREAT|0666);
  111.                 currentConnections++;
  112.                 for (;i < maxConnections && !threads[i].status;i++);
  113.                 if (i != maxConnections) {
  114.                     threadInfo t_info;
  115.                     t_info.t = &threads[i];
  116.                     t_info.key = resp.key;
  117.                     pthread_create(&(threads[i].thread), NULL, &client, &t_info);
  118.                     printf("thread created\n");
  119.                 }
  120.             }
  121.             else {
  122.                 resp.c = ERROR;
  123.                 resp.key = -1; 
  124.             }
  125.             resp.mtype = SERVER_TAG;
  126.             msgsnd(id, &resp, sizeof(ConnectResponse), 0);                     
  127.         }
  128.     }
  129. }
  130.  
  131. int main(int argc, char ** argv){
  132.     maxConnections = atoi(argv[1]);
  133.     flushTimer = atoi(argv[2]);
  134.     databaseFile = argv[3];
  135.     lastKey = PUBLIC_KEY + 1;
  136.     run();
  137.     return 0;
  138. }
Add Comment
Please, Sign In to add comment