11 #define LISTEN_PORT 2156
12 #define BACKLOG 10 //max pending connections
13 #define RECIEVE_BUFFER_SIZE 2048
15 extern int classsize[];
17 objstr_t *mainobjstore;
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 mainobjstore = objstrCreate(DEFAULT_OBJ_STORE_SIZE);
25 if (mhashCreate(HASH_SIZE, LOADFACTOR))
28 if (lhashCreate(HASH_SIZE, LOADFACTOR))
31 //pthread_t threadListen;
32 //pthread_create(&threadListen, NULL, dstmListen, NULL);
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;
46 listenfd = socket(AF_INET, SOCK_STREAM, 0);
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);
58 if (bind(listenfd, (struct sockaddr *)&my_addr, addrlength) == -1)
64 if (listen(listenfd, BACKLOG) == -1)
70 printf("Listening on port %d, fd = %d\n", LISTEN_PORT, listenfd);
73 acceptfd = accept(listenfd, (struct sockaddr *)&client_addr, &addrlength);
74 pthread_create(&thread_dstm_accept, NULL, dstmAccept, (void *)acceptfd);
79 void *dstmAccept(void *acceptfd)
81 int numbytes,i,choice, oid;
82 char buffer[RECIEVE_BUFFER_SIZE];
86 int fd_flags = fcntl((int)acceptfd, F_GETFD), size;
88 printf("Recieved connection: fd = %d\n", (int)acceptfd);
91 numbytes = recv((int)acceptfd, (void *)buffer, sizeof(buffer), 0);
92 buffer[numbytes] = '\0';
100 sscanf(buffer, "%s %d\n", opcode, &oid);
102 if (strcmp(opcode, "TRANS_RD") == 0) {
103 printf("DEBUG -> Requesting: %s %d\n", opcode, oid);
104 srcObj = mhashSearch(oid);
105 h = (objheader_t *) srcObj;
106 printf("DEBUG -> Sending oid = %d, type %d\n", h->oid, h->type);
107 size = sizeof(objheader_t) + sizeof(classsize[h->type]);
108 if(send((int)acceptfd, srcObj, size, 0) < 0) {
111 //printf("DEBUG -> sent ...%d\n", write(acceptfd, srcObj, size));
113 else if (strcmp(opcode, "TRANS_COMMIT") == 0)
114 printf(" This is a Commit\n");
115 else if (strcmp(opcode,"TRANS_ABORT") == 0)
116 printf(" This is a Abort\n");
118 printf(" This is a Broadcastt\n");
120 //printf("Read %d bytes from %d\n", numbytes, (int)acceptfd);
121 //printf("%s", buffer);
124 //} while (numbytes != 0);
126 if (close((int)acceptfd) == -1)
131 printf("Closed connection: fd = %d\n", (int)acceptfd);