V6
This commit is contained in:
parent
950ec22244
commit
2f3436e5f9
3 changed files with 89 additions and 75 deletions
|
@ -1,8 +1,6 @@
|
|||
|
||||
#include "semaphore.h"
|
||||
#include "BoundedBuffer.h"
|
||||
//#include "semaphore.h"
|
||||
//#include <stdio>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
|
|
@ -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<string> data;
|
||||
|
||||
//BoundedBuffer(
|
||||
public:
|
||||
BoundedBuffer(){empty = new Semaphore(100);}//default b value
|
||||
|
||||
void set_b(int val_b){ b = val_b;}
|
||||
BoundedBuffer(int b){
|
||||
b_val = b;
|
||||
empty = new Semaphore(b_val);
|
||||
}
|
||||
|
||||
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
|
||||
|
|
125
simpleclient.cpp
125
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 */
|
||||
|
@ -60,9 +64,6 @@ 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){
|
||||
if(pid != 0){
|
||||
execve("dataserver", argv, argv);
|
||||
}else{
|
||||
cout << "CLIENT STARTED:" << endl;
|
||||
cout << "Establishing control channel... " << flush;
|
||||
control chan("control", RequestChannel::CLIENT_SIDE);
|
||||
RequestChannel 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{
|
||||
execve("dataserver", argv, argv);
|
||||
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*
|
||||
|
||||
*/
|
||||
|
||||
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(){
|
||||
|
||||
}
|
||||
|
|
Reference in a new issue