This repository has been archived on 2025-04-11. You can view files and clone it, but cannot push or open issues or pull requests.
csce313-mp4pinie64backup/simpleclient.cpp

152 lines
4.1 KiB
C++
Raw Normal View History

2015-11-05 18:33:21 -06:00
/*
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"
2015-11-06 11:56:25 -06:00
#include "BoundedBuffer.h"
2015-11-05 18:33:21 -06:00
using namespace std;
/*--------------------------------------------------------------------------*/
/* DATA STRUCTURES */
/*--------------------------------------------------------------------------*/
/* -- (none) -- */
/*--------------------------------------------------------------------------*/
/* CONSTANTS */
/*--------------------------------------------------------------------------*/
2015-11-09 16:44:58 -06:00
//RequestChannel* control;
BoundedBuffer* Request_Buffer;
BoundedBuffer* Response_Buffers[3];
int n = 1000, b = 100, w = 5;//set defaults
2015-11-05 18:33:21 -06:00
/*--------------------------------------------------------------------------*/
/* FORWARDS */
/*--------------------------------------------------------------------------*/
2015-11-09 16:44:58 -06:00
void* Req_Thread(string name);
void* Worker_Thread(char* arg[], RequestChannel* chan);
2015-11-05 18:33:21 -06:00
/*--------------------------------------------------------------------------*/
/* MAIN FUNCTION */
/*--------------------------------------------------------------------------*/
int main(int argc, char * argv[]) {
2015-11-06 11:56:25 -06:00
2015-11-06 11:30:46 -06:00
int option = -1;
2015-11-09 16:44:58 -06:00
2015-11-06 11:30:46 -06:00
while ((option = getopt(argc, argv, "n:b:w:")) != -1){
switch (option){
case 'n' :
n = atoi(optarg);
break;
case 'b' :
b = atoi(optarg);
2015-11-09 16:44:58 -06:00
Request_Buffer = new BoundedBuffer(b);
2015-11-06 11:30:46 -06:00
break;
case 'w' :
w = atoi(optarg);
break;
}
}
2015-11-05 18:33:21 -06:00
pid_t pid = fork();
2015-11-09 16:44:58 -06:00
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;
2015-11-06 12:53:10 -06:00
2015-11-09 16:44:58 -06:00
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;
}
}
2015-11-06 12:53:10 -06:00
2015-11-09 16:44:58 -06:00
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");
2015-11-05 18:33:21 -06:00
2015-11-09 16:44:58 -06:00
}
2015-11-05 18:33:21 -06:00
usleep(1000000);
}
2015-11-06 11:30:46 -06:00
2015-11-09 16:44:58 -06:00
/*
*/
void* Req_Thread(string name){
cout << "Name: " << name << endl;
Request_Buffer->push("data" + name);
2015-11-06 12:53:10 -06:00
}
2015-11-09 16:44:58 -06:00
void* Worker_Thread(char* arg[], RequestChannel* chan){
2015-11-06 12:53:10 -06:00
//handles the data moving between the Server and Bounded Buffer
2015-11-09 16:44:58 -06:00
//control chan("control", RequestChannel::CLIENT_SIDE);
//string channel_name = control.send_request("newthread");
//RequestChannel * channel_ret = new RequestChannel(channel_name, RequestChannel::CLIENT_SIDE);
2015-11-06 12:53:10 -06:00
while(true){
2015-11-09 16:44:58 -06:00
string req = Request_Buffer->pop();
string response = chan->send_request(req);
//string local_send_request(req);
2015-11-06 12:53:10 -06:00
}
}
2015-11-06 11:30:46 -06:00
2015-11-09 16:44:58 -06:00
string local_send_request(){
}