Added trans.c for transaction funtions. Implemented transStart() and transCreateObj...
[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 int dstmInit(void)
17 {
18         //todo:initialize main object store
19         //do we want this to be a global variable, or provide
20         //separate access funtions and hide the structure?
21         
22         if (mhashCreate(HASH_SIZE, LOADFACTOR))
23                 return 1; //failure
24         
25         if (lhashCreate(HASH_SIZE, LOADFACTOR))
26                 return 1; //failure
27         
28         pthread_t threadListen;
29         pthread_create(&threadListen, NULL, dstmListen, NULL);
30         
31         return 0;
32 }
33
34 void *dstmListen()
35 {
36         int listenfd, acceptfd;
37         struct sockaddr_in my_addr;
38         struct sockaddr_in client_addr;
39         socklen_t addrlength = sizeof(struct sockaddr);
40         pthread_t thread_dstm_accept;
41         int i;
42
43         listenfd = socket(PF_INET, SOCK_STREAM, 0);
44         if (listenfd == -1)
45         {
46                 perror("socket");
47                 exit(1);
48         }
49
50         my_addr.sin_family = AF_INET;
51         my_addr.sin_port = htons(LISTEN_PORT);
52         my_addr.sin_addr.s_addr = INADDR_ANY;
53         memset(&(my_addr.sin_zero), '\0', 8);
54
55         if (bind(listenfd, (struct sockaddr *)&my_addr, addrlength) == -1)
56         {
57                 perror("bind");
58                 exit(1);
59         }
60         
61         if (listen(listenfd, BACKLOG) == -1)
62         {
63                 perror("listen");
64                 exit(1);
65         }
66
67         printf("Listening on port %d, fd = %d\n", LISTEN_PORT, listenfd);
68         while(1)
69         {
70                 acceptfd = accept(listenfd, (struct sockaddr *)&client_addr, &addrlength);
71                 pthread_create(&thread_dstm_accept, NULL, dstmAccept, (void *)acceptfd);
72         }
73         pthread_exit(NULL);
74 }
75
76 void *dstmAccept(void *acceptfd)
77 {
78         int numbytes;
79         char buffer[RECIEVE_BUFFER_SIZE];
80         int fd_flags = fcntl((int)acceptfd, F_GETFD);
81         printf("Recieved connection: fd = %d\n", (int)acceptfd);
82         do
83         {
84                 numbytes = recv((int)acceptfd, (void *)buffer, sizeof(buffer), 0);
85                 buffer[numbytes] = '\0';
86                 if (numbytes == -1)
87                 {
88                         perror("recv");
89                         pthread_exit(NULL);
90                 }
91                 else
92                 {
93                         printf("Read %d bytes from %d\n", numbytes, (int)acceptfd);
94                         printf("%s", buffer);
95                 }
96                 
97         } while (numbytes != 0);
98         if (close((int)acceptfd) == -1)
99         {
100                 perror("close");
101                 pthread_exit(NULL);
102         }
103         else
104                 printf("Closed connection: fd = %d\n", (int)acceptfd);
105         pthread_exit(NULL);
106 }
107
108