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

220 lines
6.2 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;
2015-11-09 17:17:18 -06:00
2015-11-05 18:33:21 -06:00
/*--------------------------------------------------------------------------*/
/* 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-10 15:47:13 -06:00
void* local_send_request(string name, string response);
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-10 15:47:13 -06:00
if(pid == 0){
2015-11-09 16:44:58 -06:00
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-10 15:47:13 -06:00
for(int i = 0; i < 3; i++){
pid = fork();
if(pid){ //if in the parent still
if(i == 0){
2015-11-09 16:44:58 -06:00
Req_Thread(" Joe Smith");
2015-11-10 15:47:13 -06:00
}else if(i == 1){
2015-11-09 16:44:58 -06:00
Req_Thread(" Jane Smith");
2015-11-10 15:47:13 -06:00
}else if(i == 2){
2015-11-09 16:44:58 -06:00
Req_Thread(" John Doe");
2015-11-10 15:47:13 -06:00
}
continue;
}else if (pid == 0){
break;
} else {
exit(1);
2015-11-09 16:44:58 -06:00
}
}
2015-11-10 15:47:13 -06:00
2015-11-06 12:53:10 -06:00
2015-11-10 15:51:05 -06:00
<<<<<<< HEAD
2015-11-09 16:44:58 -06:00
string channel_name;
for(int i=0; i < w; i++){
2015-11-10 15:49:32 -06:00
channel_name = chan.send_request("newthread");
2015-11-09 17:18:35 -06:00
cout << "\nChan: " << channel_name << endl;
2015-11-09 16:44:58 -06:00
RequestChannel* worker_ret = new RequestChannel(channel_name, RequestChannel::CLIENT_SIDE);
2015-11-10 15:49:32 -06:00
pid = fork();
if(pid != 0) {
Worker_Thread(argv, worker_ret);
break;
}
2015-11-09 16:44:58 -06:00
}
2015-11-10 15:49:32 -06:00
2015-11-09 16:44:58 -06:00
chan.send_request("quit");
2015-11-05 18:33:21 -06:00
2015-11-10 15:51:05 -06:00
=======
2015-11-09 18:16:10 -06:00
2015-11-10 15:47:13 -06:00
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.
}
2015-11-09 18:16:10 -06:00
for(int i=0; i < 3; i++){
//stat thread
2015-11-10 15:47:13 -06:00
//pid = fork();
2015-11-09 18:16:10 -06:00
if(pid != 0){
break;
}
2015-11-10 15:47:13 -06:00
}
2015-11-09 18:16:10 -06:00
2015-11-10 15:47:13 -06:00
}else{
execve("dataserver", argv, argv);
2015-11-10 15:51:05 -06:00
>>>>>>> 7f3859db688a7e8629c7a88e7baad87ad6df405d
2015-11-09 16:44:58 -06:00
}
2015-11-09 18:16:10 -06:00
usleep(1000000);
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-10 15:51:05 -06:00
<<<<<<< HEAD
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-10 15:49:32 -06:00
while(true){
2015-11-09 16:44:58 -06:00
2015-11-10 15:51:05 -06:00
=======
2015-11-06 12:53:10 -06:00
while(true){
2015-11-10 15:51:05 -06:00
>>>>>>> 7f3859db688a7e8629c7a88e7baad87ad6df405d
2015-11-09 16:44:58 -06:00
string req = Request_Buffer->pop();
2015-11-09 17:17:18 -06:00
cout << "Request: " << req << endl;
2015-11-09 16:44:58 -06:00
string response = chan->send_request(req);
2015-11-09 17:17:18 -06:00
cout << "Response: " << response << endl;
2015-11-10 15:47:13 -06:00
//local_send_request(req, response);
2015-11-06 12:53:10 -06:00
}
2015-11-10 15:47:13 -06:00
//chan->send_request("quit");
2015-11-06 12:53:10 -06:00
}
2015-11-06 11:30:46 -06:00
2015-11-09 18:16:10 -06:00
void* Req_Thread(string name){
2015-11-10 15:47:13 -06:00
for(int i = 0; i < n; i++){
cout << "data " << name << endl;
Request_Buffer->push("data" + name);
}
2015-11-09 18:16:10 -06:00
}
2015-11-10 15:47:13 -06:00
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";
}
2015-11-09 16:44:58 -06:00
}
2015-11-10 15:47:13 -06:00