1 #include <sys/socket.h>
2 #include <netinet/in.h>
6 #include <netinet/tcp.h>
7 #include "addUdpEnhance.h"
10 /************************
12 ***********************/
15 int createUdpSocket() {
17 struct sockaddr_in clientaddr;
20 if((sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
21 perror("socket creation failed");
24 if((setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on))) < 0) {
25 perror("setsockopt - SOL_SOCKET");
34 struct sockaddr_in servaddr;
36 //Create Global Udp Socket
37 if((udpSockFd = createUdpSocket()) < 0) {
38 printf("Error in socket\n");
41 sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
47 if(setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &setsockflag, sizeof(setsockflag)) < 0) {
53 if(setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &setsockflag, sizeof(setsockflag)) < 0) {
59 bzero(&servaddr, sizeof(servaddr));
60 servaddr.sin_family = AF_INET;
61 servaddr.sin_port = htons(UDP_PORT);
62 servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
64 if(bind(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) {
72 /* Function that listens for udp broadcast messages */
73 void *udpListenBroadcast(void *sockfd) {
74 pthread_t thread_udpBroadcast;
75 struct sockaddr_in servaddr;
76 socklen_t socklen = sizeof(struct sockaddr);
77 char readBuffer[MAX_SIZE];
80 printf("Listening on port %d, fd = %d\n", UDP_PORT, (int)sockfd);
82 memset(readBuffer, 0, MAX_SIZE);
84 int bytesRcvd = recvfrom((int)sockfd, readBuffer, sizeof(readBuffer), 0, (struct sockaddr *)&servaddr, &socklen);
86 printf("DEBUG-> Recv Error! \n");
89 short status = *((short *) &readBuffer[0]);
92 if((retval = invalidateFromPrefetchCache(readBuffer))!= 0) {
93 printf("Error: In invalidateFromPrefetchCache() at %s, %d\n", __FILE__, __LINE__);
98 printf("Error: Cannot regcognize the status in file %s, at line %d\n", __FILE__, __LINE__);
102 /* Close connection */
103 if(close((int)sockfd) == -1)
108 /* Function that invalidate objects that
109 * have been currently modified
110 * returns -1 on error and 0 on success */
111 int invalidateObj(thread_data_array_t *tdata) {
112 struct sockaddr_in clientaddr;
115 bzero(&clientaddr, sizeof(clientaddr));
116 clientaddr.sin_family = AF_INET;
117 clientaddr.sin_port = htons(UDP_PORT);
118 clientaddr.sin_addr.s_addr = INADDR_BROADCAST;
119 int maxObjsPerMsg = (MAX_SIZE - sizeof(unsigned int))/sizeof(unsigned int);
120 if(tdata->buffer->f.nummod < maxObjsPerMsg) {
121 /* send single udp msg */
123 if((retval = sendUdpMsg(tdata, &clientaddr, iteration)) < 0) {
124 printf("%s() error in sending udp message at %s, %d\n", __func__, __FILE__, __LINE__);
128 /* Split into several udp msgs */
129 int maxUdpMsg = tdata->buffer->f.nummod/maxObjsPerMsg;
130 if (tdata->buffer->f.nummod%maxObjsPerMsg) maxUdpMsg++;
132 for(i = 1; i <= maxUdpMsg; i++) {
133 if((retval = sendUdpMsg(tdata, &clientaddr, i)) < 0) {
134 printf("%s() error in sending udp message at %s, %d\n", __func__, __FILE__, __LINE__);
142 /* Function sends a udp broadcast, also distinguishes
143 * msg size to be sent based on the iteration flag
144 * returns -1 on error and 0 on success */
145 int sendUdpMsg(thread_data_array_t *tdata, struct sockaddr_in *clientaddr, int iteration) {
146 char writeBuffer[MAX_SIZE];
147 int maxObjsPerMsg = (MAX_SIZE - sizeof(unsigned int))/sizeof(unsigned int);
149 *((short *)&writeBuffer[0]) = INVALIDATE_OBJS; //control msg
150 offset += sizeof(short);
151 if(iteration == 0) { // iteration flag == zero, send single udp msg
152 *((short *) (writeBuffer+offset)) = (short) (sizeof(unsigned int) * (tdata->buffer->f.nummod)); //sizeof msg
153 offset += sizeof(short);
155 for(i = 0; i < tdata->buffer->f.nummod; i++) {
156 *((unsigned int *) (writeBuffer+offset)) = tdata->buffer->oidmod[i]; //copy objects
157 offset += sizeof(unsigned int);
159 } else { // iteration flag > zero, send multiple udp msg
161 if((tdata->buffer->f.nummod - (iteration * maxObjsPerMsg)) > 0)
162 numObj = maxObjsPerMsg;
164 numObj = tdata->buffer->f.nummod - ((iteration - 1)*maxObjsPerMsg);
165 *((short *) (writeBuffer+offset)) = (short) (sizeof(unsigned int) * numObj);
166 offset += sizeof(short);
167 int index = (iteration - 1) * maxObjsPerMsg;
169 for(i = 0; i < numObj; i++) {
170 *((unsigned int *) (writeBuffer+offset)) = tdata->buffer->oidmod[index+i];
171 offset += sizeof(unsigned int);
175 if((n = sendto(udpSockFd, (const void *) writeBuffer, sizeof(writeBuffer), 0, (const struct sockaddr *)clientaddr, sizeof(struct sockaddr_in))) < 0) {
176 perror("sendto error- ");
177 printf("DEBUG-> sendto error: errorno %d\n", errno);
183 /* Function searches given oid in prefetch cache and invalidates obj from cache
184 * returns -1 on error and 0 on success */
185 int invalidateFromPrefetchCache(char *buffer) {
186 int offset = sizeof(short);
187 /* Read objects sent */
188 int numObjsRecv = *((short *)(buffer+offset)) / sizeof(unsigned int);
190 for(i = 0; i < numObjsRecv; i++) {
192 oid = *((unsigned int *)(buffer+offset));
194 /* Lookup Objects in prefetch cache and remove them */
195 if((header = prehashSearch(oid)) != NULL) {
198 offset += sizeof(unsigned int);