Commit 817896ef authored by SHAILESH KUMAR's avatar SHAILESH KUMAR

job queue

parent efc835b7
No preview for this file type
...@@ -59,6 +59,7 @@ void sendXML(int sockfd,char *msg){ ...@@ -59,6 +59,7 @@ void sendXML(int sockfd,char *msg){
} }
toXML(encodedXML,key,value,msgtype,""); toXML(encodedXML,key,value,msgtype,"");
write(sockfd,strcat(encodedXML,delim),strlen(encodedXML)); write(sockfd,strcat(encodedXML,delim),strlen(encodedXML));
char buf[256]; char buf[256];
read(sockfd,buf,256); read(sockfd,buf,256);
...@@ -114,8 +115,10 @@ int main(int argc, char *argv[]) ...@@ -114,8 +115,10 @@ int main(int argc, char *argv[])
sendXML(sockfd,line); sendXML(sockfd,line);
} }
while(1);
fclose(fp); fclose(fp);
while(1);
//exit(0); //exit(0);
return 0; return 0;
......
...@@ -59,6 +59,7 @@ void sendXML(int sockfd,char *msg){ ...@@ -59,6 +59,7 @@ void sendXML(int sockfd,char *msg){
} }
toXML(encodedXML,key,value,msgtype,""); toXML(encodedXML,key,value,msgtype,"");
write(sockfd,strcat(encodedXML,delim),strlen(encodedXML)); write(sockfd,strcat(encodedXML,delim),strlen(encodedXML));
char buf[256]; char buf[256];
read(sockfd,buf,256); read(sockfd,buf,256);
...@@ -114,8 +115,10 @@ int main(int argc, char *argv[]) ...@@ -114,8 +115,10 @@ int main(int argc, char *argv[])
sendXML(sockfd,line); sendXML(sockfd,line);
} }
while(1);
fclose(fp); fclose(fp);
while(1);
//exit(0); //exit(0);
return 0; return 0;
......
No preview for this file type
...@@ -12,7 +12,16 @@ ...@@ -12,7 +12,16 @@
#include<unistd.h> #include<unistd.h>
#include<strings.h> #include<strings.h>
#include<string.h> #include<string.h>
void dostuff(int); /* function prototype */ #include<pthread.h>
#include "toxml.h"
#include "jobs.h"
jobs_queue *job_head=NULL;
jobs_queue *job_tail=NULL;
pthread_mutex_t job_q_lock;
void *serve(void*); /* function prototype */
void error(char *msg) void error(char *msg)
{ {
perror(msg); perror(msg);
...@@ -23,7 +32,7 @@ int main(int argc, char *argv[]) ...@@ -23,7 +32,7 @@ int main(int argc, char *argv[])
{ {
int sockfd, newsockfd, portno, clilen, pid; int sockfd, newsockfd, portno, clilen, pid;
struct sockaddr_in serv_addr, cli_addr; struct sockaddr_in serv_addr, cli_addr;
pthread_mutex_init(&job_q_lock,NULL);
sockfd = socket(AF_INET, SOCK_STREAM, 0); sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) if (sockfd < 0)
error("ERROR opening socket"); error("ERROR opening socket");
...@@ -39,37 +48,81 @@ int main(int argc, char *argv[]) ...@@ -39,37 +48,81 @@ int main(int argc, char *argv[])
while (1) { while (1) {
newsockfd = accept(sockfd, newsockfd = accept(sockfd,
(struct sockaddr *) &cli_addr, &clilen); (struct sockaddr *) &cli_addr, &clilen);
if (newsockfd < 0) /*if (newsockfd < 0)
error("ERROR on accept"); error("ERROR on accept");
pid = fork(); pid = fork();
if (pid < 0) if (pid < 0)
error("ERROR on fork"); error("ERROR on fork");
if (pid == 0) { if (pid == 0) {
close(sockfd); close(sockfd);
dostuff(newsockfd); serve(newsockfd);
exit(0); exit(0);
} }
else close(newsockfd); else close(newsockfd);*/
pthread_t thr;
int *arg;
arg=malloc(sizeof(*arg));
*arg=newsockfd;
pthread_create(&thr,NULL,serve,arg);
} /* end of while */ } /* end of while */
return 0; /* we never get here */ return 0; /* we never get here */
} }
/******** DOSTUFF() ********************* /******** serve() *********************
There is a separate instance of this function There is a separate instance of this function
for each connection. It handles all communication for each connection. It handles all communication
once a connnection has been established. once a connnection has been established.
*****************************************/ *****************************************/
void dostuff (int sock) void * serve (void* socket)
{ {
int sock;
int n; int n;
char buffer[256]; long tid=pthread_self();
sock=*((int*) socket);
char buffer[300000];
while(1){ while(1){
bzero(buffer,256); //zeroing all the value in buffer
n = read(sock,buffer,255); bzero(buffer,300000);
//reading the socket
n = read(sock,buffer,300000);
if (n < 0) error("ERROR reading from socket"); if (n < 0) error("ERROR reading from socket");
printf("Here is the message:\n %s\n",buffer); else if (n==0) {
n = write(sock,"I got your message\n",19); printf("Client has been closed,Killing the thread\n");
pthread_cancel(tid);
}
else{
//Creating a structure to add in linked list of job queue
jobs_queue *job=(jobs_queue *)malloc((sizeof(jobs_queue)));
job->fd=sock;
job->next=NULL;
strcpy(job->xml,buffer);
//Adding to the chain in Job Queue
pthread_mutex_lock(&job_q_lock);
if(job_head==NULL){
job_head=job;
job_tail=job;
}
else{
job_tail->next=job;
job_tail=job;
}
pthread_mutex_unlock(&job_q_lock);
//Printing the linked list
jobs_queue *p=job_head;
while(p!=NULL){
printf("%d ",p->fd);
p=p->next;
}
printf("\n");
//Acknowledging the client to send next data
n = write(sock,"ACK\n",4);
if (n < 0) error("ERROR writing to socket"); if (n < 0) error("ERROR writing to socket");
} }
}
} }
...@@ -12,7 +12,16 @@ ...@@ -12,7 +12,16 @@
#include<unistd.h> #include<unistd.h>
#include<strings.h> #include<strings.h>
#include<string.h> #include<string.h>
void dostuff(int); /* function prototype */ #include<pthread.h>
#include "toxml.h"
#include "jobs.h"
jobs_queue *job_head=NULL;
jobs_queue *job_tail=NULL;
pthread_mutex_t job_q_lock;
void *serve(void*); /* function prototype */
void error(char *msg) void error(char *msg)
{ {
perror(msg); perror(msg);
...@@ -23,7 +32,7 @@ int main(int argc, char *argv[]) ...@@ -23,7 +32,7 @@ int main(int argc, char *argv[])
{ {
int sockfd, newsockfd, portno, clilen, pid; int sockfd, newsockfd, portno, clilen, pid;
struct sockaddr_in serv_addr, cli_addr; struct sockaddr_in serv_addr, cli_addr;
pthread_mutex_init(&job_q_lock,NULL);
sockfd = socket(AF_INET, SOCK_STREAM, 0); sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) if (sockfd < 0)
error("ERROR opening socket"); error("ERROR opening socket");
...@@ -39,37 +48,81 @@ int main(int argc, char *argv[]) ...@@ -39,37 +48,81 @@ int main(int argc, char *argv[])
while (1) { while (1) {
newsockfd = accept(sockfd, newsockfd = accept(sockfd,
(struct sockaddr *) &cli_addr, &clilen); (struct sockaddr *) &cli_addr, &clilen);
if (newsockfd < 0) /*if (newsockfd < 0)
error("ERROR on accept"); error("ERROR on accept");
pid = fork(); pid = fork();
if (pid < 0) if (pid < 0)
error("ERROR on fork"); error("ERROR on fork");
if (pid == 0) { if (pid == 0) {
close(sockfd); close(sockfd);
dostuff(newsockfd); serve(newsockfd);
exit(0); exit(0);
} }
else close(newsockfd); else close(newsockfd);*/
pthread_t thr;
int *arg;
arg=malloc(sizeof(*arg));
*arg=newsockfd;
pthread_create(&thr,NULL,serve,arg);
} /* end of while */ } /* end of while */
return 0; /* we never get here */ return 0; /* we never get here */
} }
/******** DOSTUFF() ********************* /******** serve() *********************
There is a separate instance of this function There is a separate instance of this function
for each connection. It handles all communication for each connection. It handles all communication
once a connnection has been established. once a connnection has been established.
*****************************************/ *****************************************/
void dostuff (int sock) void * serve (void* socket)
{ {
int sock;
int n; int n;
char buffer[256]; long tid=pthread_self();
sock=*((int*) socket);
char buffer[300000];
while(1){ while(1){
bzero(buffer,256); //zeroing all the value in buffer
n = read(sock,buffer,255); bzero(buffer,300000);
//reading the socket
n = read(sock,buffer,300000);
if (n < 0) error("ERROR reading from socket"); if (n < 0) error("ERROR reading from socket");
printf("Here is the message:\n %s\n",buffer); else if (n==0) {
n = write(sock,"I got your message\n",19); printf("Client has been closed,Killing the thread\n");
pthread_cancel(tid);
}
else{
//Creating a structure to add in linked list of job queue
jobs_queue *job=(jobs_queue *)malloc((sizeof(jobs_queue)));
job->fd=sock;
job->next=NULL;
strcpy(job->xml,buffer);
//Adding to the chain in Job Queue
pthread_mutex_lock(&job_q_lock);
if(job_head==NULL){
job_head=job;
job_tail=job;
}
else{
job_tail->next=job;
job_tail=job;
}
pthread_mutex_unlock(&job_q_lock);
//Printing the linked list
/*jobs_queue *p=job_head;
while(p!=NULL){
printf("%d ",p->fd);
p=p->next;
}
printf("\n");
*/
//Acknowledging the client to send next data
n = write(sock,"ACK\n",4);
if (n < 0) error("ERROR writing to socket"); if (n < 0) error("ERROR writing to socket");
} }
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment