*** empty log message ***
[IRC.git] / Robust / src / Runtime / DSTM / interface / dstmserver.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <pthread.h>
5 #include <netdb.h>
6 #include <fcntl.h>
7 #include "dstm.h"
8 #include "mlookup.h"
9 #include "llookup.h"
10
11 #define LISTEN_PORT 2153
12 #define BACKLOG 10 //max pending connections
13 #define RECIEVE_BUFFER_SIZE 1500
14
15
16 objstr_t *mainobjstore;
17 mainobjstore = objstrCreate(DEFAULT_OBJ_STORE_SIZE);    
18
19 int dstmInit(void)
20 {
21         //todo:initialize main object store
22         //do we want this to be a global variable, or provide
23         //separate access funtions and hide the structure?
24
25         if (mhashCreate(HASH_SIZE, LOADFACTOR))
26                 return 1; //failure
27         
28         if (lhashCreate(HASH_SIZE, LOADFACTOR))
29                 return 1; //failure
30         
31         pthread_t threadListen;
32         pthread_create(&threadListen, NULL, dstmListen, NULL);
33         
34         return 0;
35 }
36
37 void *dstmListen()
38 {
39         int listenfd, acceptfd;
40         struct sockaddr_in my_addr;
41         struct sockaddr_in client_addr;
42         socklen_t addrlength = sizeof(struct sockaddr);
43         pthread_t thread_dstm_accept;
44         int i;
45
46         listenfd = socket(PF_INET, SOCK_STREAM, 0);
47         if (listenfd == -1)
48         {
49                 perror("socket");
50                 exit(1);
51         }
52
53         my_addr.sin_family = AF_INET;
54         my_addr.sin_port = htons(LISTEN_PORT);
55         my_addr.sin_addr.s_addr = INADDR_ANY;
56         memset(&(my_addr.sin_zero), '\0', 8);
57
58         if (bind(listenfd, (struct sockaddr *)&my_addr, addrlength) == -1)
59         {
60                 perror("bind");
61                 exit(1);
62         }
63         
64         if (listen(listenfd, BACKLOG) == -1)
65         {
66                 perror("listen");
67                 exit(1);
68         }
69
70         printf("Listening on port %d, fd = %d\n", LISTEN_PORT, listenfd);
71         while(1)
72         {
73                 acceptfd = accept(listenfd, (struct sockaddr *)&client_addr, &addrlength);
74                 pthread_create(&thread_dstm_accept, NULL, dstmAccept, (void *)acceptfd);
75         }
76         pthread_exit(NULL);
77 }
78
79 void *dstmAccept(void *acceptfd)
80 {
81         int numbytes;
82         char buffer[RECIEVE_BUFFER_SIZE];
83         int fd_flags = fcntl((int)acceptfd, F_GETFD);
84         printf("Recieved connection: fd = %d\n", (int)acceptfd);
85         do
86         {
87                 numbytes = recv((int)acceptfd, (void *)buffer, sizeof(buffer), 0);
88                 buffer[numbytes] = '\0';
89                 if (numbytes == -1)
90                 {
91                         perror("recv");
92                         pthread_exit(NULL);
93                 }
94                 else
95                 {
96                         printf("Read %d bytes from %d\n", numbytes, (int)acceptfd);
97                         printf("%s", buffer);
98                 }
99                 
100         } while (numbytes != 0);
101         if (close((int)acceptfd) == -1)
102         {
103                 perror("close");
104                 pthread_exit(NULL);
105         }
106         else
107                 printf("Closed connection: fd = %d\n", (int)acceptfd);
108         pthread_exit(NULL);
109 }
110
111