146 lines
4.1 KiB
C++
Executable file
146 lines
4.1 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);
|
|
|
|
/*--------------------------------------------------------------------------*/
|
|
/* 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){
|
|
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("newthrread");
|
|
cout << "\nChan: " << 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(string name){
|
|
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(Request_Buffer->get_val() != 0){
|
|
|
|
string req = Request_Buffer->pop();
|
|
cout << "Request: " << req << endl;
|
|
string response = chan->send_request(req);
|
|
cout << "Response: " << response << endl;
|
|
|
|
}
|
|
chan->send_request("quit");
|
|
}
|
|
|
|
string local_send_request(){
|
|
|
|
}
|