diff --git a/BoundedBuffer.cpp b/BoundedBuffer.cpp index 292a9e4..40f1516 100755 --- a/BoundedBuffer.cpp +++ b/BoundedBuffer.cpp @@ -1,8 +1,6 @@ #include "semaphore.h" #include "BoundedBuffer.h" -//#include "semaphore.h" -//#include #include #include @@ -14,9 +12,11 @@ void BoundedBuffer::push(string item){ full->V();//increment the number of full slots now. } -void BoundedBuffer::pop(){ - full.P(); - data.pop(item); - empty.V(); +string BoundedBuffer::pop(){ + string item = data.back(); + full->P(); + data.pop_back(); + empty->V(); + return item; } \ No newline at end of file diff --git a/BoundedBuffer.h b/BoundedBuffer.h index 2420fdd..f058c87 100755 --- a/BoundedBuffer.h +++ b/BoundedBuffer.h @@ -9,23 +9,32 @@ using namespace std; -extern unsigned int n, b, w; +//extern unsigned int n, b, w; class BoundedBuffer{ + int b_val; + Semaphore *full = new Semaphore(0); //initialized to 0, Since there are 0 full slots - Semaphore *empty = new Semaphore(b); //initialized to b, Since all the slots are empty + Semaphore *empty;// = new Semaphore(b); //initialized to b, Since all the slots are empty Semaphore *mutex = new Semaphore(1); vector data; //BoundedBuffer( +public: + BoundedBuffer(){empty = new Semaphore(100);}//default b value + + BoundedBuffer(int b){ + b_val = b; + empty = new Semaphore(b_val); + } - void set_b(int val_b){ b = val_b;} + void set_b(int b){ b_val = b;} void push(string item); - void pop(); + string pop(); //can't be larger than b strings //b = user input diff --git a/simpleclient.cpp b/simpleclient.cpp index b7b9863..a7f589c 100755 --- a/simpleclient.cpp +++ b/simpleclient.cpp @@ -44,13 +44,17 @@ using namespace std; /* CONSTANTS */ /*--------------------------------------------------------------------------*/ - /* -- (none) -- */ +//RequestChannel* control; +BoundedBuffer* Request_Buffer; +BoundedBuffer* Response_Buffers[3]; +int n = 1000, b = 100, w = 5;//set defaults /*--------------------------------------------------------------------------*/ /* FORWARDS */ /*--------------------------------------------------------------------------*/ - /* -- (none) -- */ +void* Req_Thread(string name); +void* Worker_Thread(char* arg[], RequestChannel* chan); /*--------------------------------------------------------------------------*/ /* MAIN FUNCTION */ @@ -59,10 +63,7 @@ using namespace std; int main(int argc, char * argv[]) { int option = -1; - - unsigned int n; - unsigned int b; - unsigned int w; + while ((option = getopt(argc, argv, "n:b:w:")) != -1){ switch (option){ case 'n' : @@ -70,6 +71,7 @@ int main(int argc, char * argv[]) { break; case 'b' : b = atoi(optarg); + Request_Buffer = new BoundedBuffer(b); break; case 'w' : w = atoi(optarg); @@ -77,70 +79,73 @@ int main(int argc, char * argv[]) { } } - cout << "TEST: " << n << b << w << endl; - - BoundedBuffer buff(); - Semaphore sema(5); - cout << "CLIENT STARTED:" << endl; - pid_t pid = fork(); - if(pid == 0){ - cout << "Establishing control channel... " << flush; - control chan("control", RequestChannel::CLIENT_SIDE); - cout << "done." << endl; - - /* -- Start sending a sequence of requests */ - -/* - string reply1 = chan.send_request("hello"); - cout << "Reply to request 'hello' is '" << reply1 << "'" << endl; - - string reply2 = chan.send_request("data Joe Smith"); - cout << "Reply to request 'data Joe Smith' is '" << reply2 << "'" << endl; - - string reply3 = chan.send_request("data Jane Smith"); - cout << "Reply to request 'data Jane Smith' is '" << reply3 << "'" << endl; - - string reply5 = chan.send_request("newthread"); - cout << "Reply to request 'newthread' is " << reply5 << "'" << endl; - RequestChannel chan2(reply5, RequestChannel::CLIENT_SIDE); - - string reply6 = chan2.send_request("data John Doe"); - cout << "Reply to request 'data John Doe' is '" << reply6 << "'" << endl; - - string reply7 = chan2.send_request("quit"); - cout << "Reply to request 'quit' is '" << reply7 << "'" << endl; - - string reply4 = chan.send_request("quit"); - cout << "Reply to request 'quit' is '" << reply4 << "'" << endl; -*/ - - }else{ + if(pid != 0){ execve("dataserver", argv, argv); - } + }else{ + cout << "CLIENT STARTED:" << endl; + cout << "Establishing control channel... " << flush; + RequestChannel chan("control", RequestChannel::CLIENT_SIDE); + cout << "done." << endl; + for(int i=0; i < n*3; i++){ + switch(i%3){ + case 0: + Req_Thread(" Joe Smith"); + cout << "Joe\n"; + break; + case 1: + Req_Thread(" Jane Smith"); + cout << "Jane\n"; + break; + case 2: + Req_Thread(" John Doe"); + cout << "John\n"; + break; + } + } + + string channel_name; + for(int i=0; i < w; i++){ + channel_name = chan.send_request("newthread"); + cout << "Chan: " << channel_name << endl; + RequestChannel* worker_ret = new RequestChannel(channel_name, RequestChannel::CLIENT_SIDE); + Worker_Thread(argv, worker_ret); + } + + chan.send_request("quit"); + + } usleep(1000000); } -void Req_Thread(RequestChannel chan){ - //handles the data moving between the Client and Bounded Buffer - for(int i=0; i < 3; i++){ - chan.send_request("newthread"); - } -} - -void Worker_Thread(void* arg){ - //handles the data moving between the Server and Bounded Buffer - channel_name = control.send_request("newthread"); - RequestChannel * channel_ret = new RequestChannel(channel_name, CLIENT_SIDE); - while(true){ - string req = BB.pull(); - string resp = channel_ret -> send_request(req); - } -} - - /* -void* -*/ \ No newline at end of file + +*/ + +void* Req_Thread(string name){ + cout << "Name: " << name << endl; + Request_Buffer->push("data" + name); +} + +void* Worker_Thread(char* arg[], RequestChannel* chan){ + //handles the data moving between the Server and Bounded Buffer + //control chan("control", RequestChannel::CLIENT_SIDE); + //string channel_name = control.send_request("newthread"); + //RequestChannel * channel_ret = new RequestChannel(channel_name, RequestChannel::CLIENT_SIDE); + while(true){ + + + string req = Request_Buffer->pop(); + string response = chan->send_request(req); + + //string local_send_request(req); + } +} + + + +string local_send_request(){ + +}