219 lines
6.2 KiB
C++
Executable file
219 lines
6.2 KiB
C++
Executable file
/*
|
|
File: simpleclient.C
|
|
|
|
Author: R. Bettati
|
|
Department of Computer Science
|
|
Texas A&M University
|
|
Date : 2013/01/31
|
|
|
|
Simple client main program for MP3 in CSCE 313
|
|
*/
|
|
|
|
/*--------------------------------------------------------------------------*/
|
|
/* DEFINES */
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
/* -- (none) -- */
|
|
|
|
/*--------------------------------------------------------------------------*/
|
|
/* INCLUDES */
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
#include <cassert>
|
|
#include <cstring>
|
|
#include <iostream>
|
|
#include <sys/types.h>
|
|
#include <sys/stat.h>
|
|
|
|
#include <errno.h>
|
|
#include <unistd.h>
|
|
|
|
#include "reqchannel.h"
|
|
#include "semaphore.h"
|
|
#include "BoundedBuffer.h"
|
|
|
|
using namespace std;
|
|
|
|
|
|
/*--------------------------------------------------------------------------*/
|
|
/* DATA STRUCTURES */
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
/* -- (none) -- */
|
|
|
|
/*--------------------------------------------------------------------------*/
|
|
/* CONSTANTS */
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
//RequestChannel* control;
|
|
BoundedBuffer* Request_Buffer;
|
|
BoundedBuffer* Response_Buffers[3];
|
|
int n = 1000, b = 100, w = 5;//set defaults
|
|
|
|
/*--------------------------------------------------------------------------*/
|
|
/* FORWARDS */
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
void* Req_Thread(string name);
|
|
void* Worker_Thread(char* arg[], RequestChannel* chan);
|
|
void* local_send_request(string name, string response);
|
|
|
|
/*--------------------------------------------------------------------------*/
|
|
/* MAIN FUNCTION */
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
int main(int argc, char * argv[]) {
|
|
|
|
int option = -1;
|
|
|
|
while ((option = getopt(argc, argv, "n:b:w:")) != -1){
|
|
switch (option){
|
|
case 'n' :
|
|
n = atoi(optarg);
|
|
break;
|
|
case 'b' :
|
|
b = atoi(optarg);
|
|
Request_Buffer = new BoundedBuffer(b);
|
|
break;
|
|
case 'w' :
|
|
w = atoi(optarg);
|
|
break;
|
|
}
|
|
}
|
|
|
|
pid_t pid = fork();
|
|
|
|
if(pid == 0){
|
|
|
|
cout << "CLIENT STARTED:" << endl;
|
|
cout << "Establishing control channel... " << flush;
|
|
RequestChannel chan("control", RequestChannel::CLIENT_SIDE);
|
|
cout << "done." << endl;
|
|
|
|
for(int i = 0; i < 3; i++){
|
|
pid = fork();
|
|
if(pid){ //if in the parent still
|
|
if(i == 0){
|
|
Req_Thread(" Joe Smith");
|
|
}else if(i == 1){
|
|
Req_Thread(" Jane Smith");
|
|
}else if(i == 2){
|
|
Req_Thread(" John Doe");
|
|
}
|
|
continue;
|
|
}else if (pid == 0){
|
|
break;
|
|
} else {
|
|
exit(1);
|
|
}
|
|
}
|
|
|
|
|
|
<<<<<<< HEAD
|
|
string channel_name;
|
|
for(int i=0; i < w; i++){
|
|
channel_name = chan.send_request("newthread");
|
|
cout << "\nChan: " << channel_name << endl;
|
|
RequestChannel* worker_ret = new RequestChannel(channel_name, RequestChannel::CLIENT_SIDE);
|
|
pid = fork();
|
|
if(pid != 0) {
|
|
Worker_Thread(argv, worker_ret);
|
|
break;
|
|
}
|
|
}
|
|
|
|
|
|
chan.send_request("quit");
|
|
|
|
=======
|
|
|
|
|
|
RequestChannel* worker_ret;
|
|
string channel_name;
|
|
|
|
if(pid){
|
|
RequestChannel chan("control", RequestChannel::CLIENT_SIDE);
|
|
cout << "done." << endl;
|
|
for(int i=0; i < w; i++){
|
|
pid = fork();
|
|
if(pid) {
|
|
pid = fork();
|
|
channel_name = chan.send_request("newthread");
|
|
cout << "Chan: " << channel_name << endl;
|
|
worker_ret = new RequestChannel(channel_name, RequestChannel::CLIENT_SIDE);
|
|
Worker_Thread(argv, worker_ret);
|
|
continue;
|
|
}else if(pid == 0){
|
|
break;
|
|
}else{
|
|
exit(1);
|
|
}
|
|
}
|
|
wait();
|
|
//chan.send_request("quit");
|
|
} else {
|
|
//this is where the child threads from the
|
|
//req_thread threads end up.
|
|
}
|
|
|
|
|
|
for(int i=0; i < 3; i++){
|
|
//stat thread
|
|
|
|
//pid = fork();
|
|
if(pid != 0){
|
|
|
|
break;
|
|
}
|
|
}
|
|
|
|
}else{
|
|
execve("dataserver", argv, argv);
|
|
>>>>>>> 7f3859db688a7e8629c7a88e7baad87ad6df405d
|
|
}
|
|
usleep(1000000);
|
|
}
|
|
|
|
void* Worker_Thread(char* arg[], RequestChannel* chan){
|
|
<<<<<<< HEAD
|
|
//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){
|
|
|
|
=======
|
|
while(true){
|
|
>>>>>>> 7f3859db688a7e8629c7a88e7baad87ad6df405d
|
|
string req = Request_Buffer->pop();
|
|
cout << "Request: " << req << endl;
|
|
string response = chan->send_request(req);
|
|
cout << "Response: " << response << endl;
|
|
//local_send_request(req, response);
|
|
}
|
|
//chan->send_request("quit");
|
|
}
|
|
|
|
void* Req_Thread(string name){
|
|
for(int i = 0; i < n; i++){
|
|
cout << "data " << name << endl;
|
|
Request_Buffer->push("data" + name);
|
|
}
|
|
}
|
|
|
|
void* local_send_request(string name, string response){
|
|
//send to proper response buffer based on which name is found!
|
|
if(name.compare("data Joe Smith") == 0){
|
|
Response_Buffers[0]->push(response);
|
|
}else if(name.compare("data Jane Smith") == 0){
|
|
Response_Buffers[1]->push(response);
|
|
}else if(name.compare("data John Doe") == 0){
|
|
Response_Buffers[2]->push(response);
|
|
}else{
|
|
cout << "ERROR: Invalid push into Response Buffers\n";
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|