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>
|
2015-11-11 04:56:41 -06:00
|
|
|
#include <pthread.h>
|
2015-11-05 18:33:21 -06:00
|
|
|
|
|
|
|
#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 */
|
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
|
2015-11-11 04:56:41 -06:00
|
|
|
/* none */
|
2015-11-05 18:33:21 -06:00
|
|
|
|
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
/* CONSTANTS */
|
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
|
2015-11-11 04:56:41 -06:00
|
|
|
|
|
|
|
BoundedBuffer Request_Buffer;
|
|
|
|
BoundedBuffer Response_Buffer[3];
|
|
|
|
BoundedBuffer RB1;
|
|
|
|
|
|
|
|
vector<int> John_vec(100);
|
|
|
|
vector<int> Jane_vec(100);
|
|
|
|
vector<int> Joe_vec(100);
|
|
|
|
|
2015-11-09 16:44:58 -06:00
|
|
|
int n = 1000, b = 100, w = 5;//set defaults
|
2015-11-05 18:33:21 -06:00
|
|
|
|
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
/* FORWARDS */
|
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
|
2015-11-11 04:56:41 -06:00
|
|
|
void* Req_Thread(void* args);
|
|
|
|
void* Worker_Thread(void* args);
|
|
|
|
void* Stat_Thread(void* args);
|
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);
|
|
|
|
break;
|
|
|
|
case 'w' :
|
|
|
|
w = atoi(optarg);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-05 18:33:21 -06:00
|
|
|
pid_t pid = fork();
|
|
|
|
|
2015-11-11 04:56:41 -06:00
|
|
|
if(pid != 0){
|
|
|
|
|
|
|
|
Request_Buffer.set_empty(b);
|
|
|
|
Response_Buffer[0].set_empty(b);
|
|
|
|
Response_Buffer[1].set_empty(b);
|
|
|
|
Response_Buffer[2].set_empty(b);
|
|
|
|
|
|
|
|
|
2015-11-10 15:47:13 -06:00
|
|
|
|
2015-11-09 16:44:58 -06:00
|
|
|
RequestChannel chan("control", RequestChannel::CLIENT_SIDE);
|
2015-11-11 15:34:16 -06:00
|
|
|
<<<<<<< HEAD
|
2015-11-11 04:56:41 -06:00
|
|
|
|
|
|
|
pthread_t request_thread_ids[3];
|
|
|
|
pthread_t worker_thread_ids[w];
|
|
|
|
pthread_t statistic_thread_ids[3];
|
|
|
|
RequestChannel* worker_return[w];
|
2015-11-11 15:34:16 -06:00
|
|
|
=======
|
2015-11-09 16:44:58 -06:00
|
|
|
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-11 15:34:16 -06:00
|
|
|
>>>>>>> 4a988cc17197484a727d1eafa31fc5dc8788b952
|
2015-11-09 18:16:10 -06:00
|
|
|
|
2015-11-10 15:47:13 -06:00
|
|
|
|
|
|
|
|
2015-11-11 04:56:41 -06:00
|
|
|
|
|
|
|
//---------------------------------------------
|
|
|
|
//------- pthread_create -------
|
|
|
|
//---------------------------------------------
|
|
|
|
int index[3];
|
|
|
|
for(int i=0; i < 3; i++){
|
|
|
|
index[i] = i;
|
|
|
|
pthread_create(&request_thread_ids[i], NULL, Req_Thread, (void *)&index[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
for(int i=0; i < w; i++){
|
|
|
|
string channel_name = chan.send_request("newthread");
|
|
|
|
worker_return[i] = new RequestChannel(channel_name, RequestChannel::CLIENT_SIDE);
|
|
|
|
pthread_create(&worker_thread_ids[i], NULL, Worker_Thread, worker_return[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
for(int i=0; i < 3; i++){
|
|
|
|
pthread_create(&statistic_thread_ids[i], NULL, Stat_Thread, (void *)&i);
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------
|
|
|
|
//-------- pthread_join --------
|
|
|
|
//---------------------------------------------
|
|
|
|
|
|
|
|
for(int i=0; i < 3; i++){
|
|
|
|
pthread_join(request_thread_ids[i], NULL);
|
2015-11-10 15:47:13 -06:00
|
|
|
}
|
2015-11-09 18:16:10 -06:00
|
|
|
|
2015-11-11 04:56:41 -06:00
|
|
|
for(int i=0; i < w; i++){
|
|
|
|
pthread_join(worker_thread_ids[i], NULL);
|
|
|
|
}
|
2015-11-09 18:16:10 -06:00
|
|
|
|
|
|
|
for(int i=0; i < 3; i++){
|
2015-11-11 04:56:41 -06:00
|
|
|
pthread_join(statistic_thread_ids[i], NULL);
|
|
|
|
}
|
2015-11-09 18:16:10 -06:00
|
|
|
|
2015-11-11 04:56:41 -06:00
|
|
|
for(int i=0; i < w; i++){
|
|
|
|
worker_return[i]->send_request("quit");
|
|
|
|
}
|
|
|
|
|
|
|
|
cout << "\n\n HISTOGRAM \n";
|
|
|
|
int ct = 0;
|
|
|
|
for(int i=0; i < 100; i++){
|
|
|
|
cout << "Num: " << i << "\tTimes: " << John_vec[i] << endl;
|
|
|
|
|
|
|
|
}
|
|
|
|
//STILL NEED TO OUTPUT THE HISTOGRAM SOMEHOW....
|
|
|
|
|
|
|
|
chan.send_request("quit");
|
2015-11-10 15:47:13 -06:00
|
|
|
}else{
|
2015-11-11 15:34:16 -06:00
|
|
|
<<<<<<< HEAD
|
2015-11-11 04:56:41 -06:00
|
|
|
execl("dataserver", 0);
|
2015-11-11 15:34:16 -06:00
|
|
|
=======
|
2015-11-10 15:47:13 -06:00
|
|
|
execve("dataserver", argv, argv);
|
2015-11-10 15:51:05 -06:00
|
|
|
>>>>>>> 7f3859db688a7e8629c7a88e7baad87ad6df405d
|
2015-11-11 15:34:16 -06:00
|
|
|
>>>>>>> 4a988cc17197484a727d1eafa31fc5dc8788b952
|
2015-11-09 16:44:58 -06:00
|
|
|
}
|
2015-11-09 18:16:10 -06:00
|
|
|
usleep(1000000);
|
2015-11-11 04:56:41 -06:00
|
|
|
|
2015-11-06 12:53:10 -06:00
|
|
|
}
|
|
|
|
|
2015-11-11 15:34:16 -06:00
|
|
|
<<<<<<< HEAD
|
2015-11-11 04:56:41 -06:00
|
|
|
void* Worker_Thread(void* arg){
|
|
|
|
RequestChannel* chann = (RequestChannel*)arg;
|
2015-11-06 12:53:10 -06:00
|
|
|
while(true){
|
2015-11-11 04:56:41 -06:00
|
|
|
string request = Request_Buffer.pop();
|
|
|
|
string response = chann->send_request(request);
|
|
|
|
if(request == "data Joe Smith"){
|
|
|
|
Response_Buffer[0].push(response);
|
|
|
|
}else if(request == "data Jane Smith"){
|
|
|
|
Response_Buffer[1].push(response);
|
|
|
|
}else if(request == "data John Doe"){
|
|
|
|
Response_Buffer[2].push(response);
|
|
|
|
}else if(request == "quit"){ //Break out of the thread if quit response is found
|
|
|
|
break;
|
|
|
|
}
|
2015-11-11 15:34:16 -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-11 15:34:16 -06:00
|
|
|
>>>>>>> 4a988cc17197484a727d1eafa31fc5dc8788b952
|
2015-11-06 12:53:10 -06:00
|
|
|
}
|
|
|
|
}
|
2015-11-06 11:30:46 -06:00
|
|
|
|
2015-11-11 04:56:41 -06:00
|
|
|
|
|
|
|
void* Req_Thread(void* arg){
|
|
|
|
int person = *(int*)arg;
|
|
|
|
cout << "PERSON: " << person << endl;
|
|
|
|
switch(person){
|
|
|
|
case 0:
|
|
|
|
for(int i=0; i < n; i++){ Request_Buffer.push("data Joe Smith"); cout << "JOE";}
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
for(int i=0; i < n; i++){ Request_Buffer.push("data Jane Smith"); cout << "JANE";}
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
for(int i=0; i < n; i++){ Request_Buffer.push("data John Doe"); cout << "JOHN";}
|
|
|
|
break;
|
2015-11-10 15:47:13 -06:00
|
|
|
}
|
2015-11-09 18:16:10 -06:00
|
|
|
}
|
|
|
|
|
2015-11-11 04:56:41 -06:00
|
|
|
void* Stat_Thread(void* arg){
|
2015-11-10 15:47:13 -06:00
|
|
|
//send to proper response buffer based on which name is found!
|
2015-11-11 04:56:41 -06:00
|
|
|
int person = *(int*)arg;
|
|
|
|
|
|
|
|
string num;
|
|
|
|
if(person == 0){
|
|
|
|
for(int i=0; i < n; i++){
|
|
|
|
num = Response_Buffer[0].pop();
|
|
|
|
int v = atoi(num.c_str());
|
|
|
|
John_vec[v] = John_vec[v] + 1;
|
|
|
|
}
|
|
|
|
}else if(person == 1){
|
|
|
|
for(int i=0; i < n; i++){
|
|
|
|
num = Response_Buffer[1].pop();
|
|
|
|
cout << "\nPERSON1: " << num << endl;
|
|
|
|
int v = atoi(num.c_str());
|
|
|
|
|
|
|
|
Jane_vec[v] = Jane_vec[v] + 1;
|
|
|
|
}
|
|
|
|
}else if(person == 2){
|
|
|
|
for(int i=0; i < n; i++){
|
|
|
|
num = Response_Buffer[2].pop();
|
|
|
|
int v = atoi(num.c_str());
|
|
|
|
John_vec[v] = John_vec[v] + 1;
|
|
|
|
}
|
2015-11-10 15:47:13 -06:00
|
|
|
}
|
2015-11-09 16:44:58 -06:00
|
|
|
}
|
2015-11-10 15:47:13 -06:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|