Written some more code for ddcd
This commit is contained in:
parent
4737a7e808
commit
461390edf7
4 changed files with 151 additions and 88 deletions
113
ddcd.cpp
113
ddcd.cpp
|
@ -30,14 +30,16 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
|
||||||
#include "ddcd.h"
|
#include "ddcd.h"
|
||||||
|
|
||||||
|
|
||||||
int host_port = 0;
|
int host_port = 0;
|
||||||
char host_address[100] = "127.0.0.1";
|
char host_address[100] = "127.0.0.1";
|
||||||
|
int thread_cntr = 0;
|
||||||
|
|
||||||
|
//CLI parameters
|
||||||
int decimation = 0;
|
int decimation = 0;
|
||||||
float transition_bw = 0.05;
|
float transition_bw = 0.05;
|
||||||
int bufsize = 1024;
|
int bufsize = 1024; //! currently unused
|
||||||
int bufcnt = 1024;
|
int bufcnt = 1024;
|
||||||
int maxbufcnt = 1000
|
|
||||||
int thread_cntr = 0;
|
|
||||||
char ddc_method_str[100] = "td";
|
char ddc_method_str[100] = "td";
|
||||||
ddc_method_t ddc_method;
|
ddc_method_t ddc_method;
|
||||||
|
|
||||||
|
@ -59,10 +61,11 @@ int main(int argc, char* argv[])
|
||||||
{"address", required_argument, 0, 'a' },
|
{"address", required_argument, 0, 'a' },
|
||||||
{"decimation", required_argument, 0, 'd' },
|
{"decimation", required_argument, 0, 'd' },
|
||||||
{"bufsize", required_argument, 0, 'b' },
|
{"bufsize", required_argument, 0, 'b' },
|
||||||
|
{"bufcnt", required_argument, 0, 'n' },
|
||||||
{"method", required_argument, 0, 'm' },
|
{"method", required_argument, 0, 'm' },
|
||||||
{"transition", required_argument, 0, 't' }
|
{"transition", required_argument, 0, 't' }
|
||||||
};
|
};
|
||||||
c = getopt_long(argc, argv, "p:a:d:b:m:t:", long_options, &option_index);
|
c = getopt_long(argc, argv, "p:a:d:b:n:m:t:", long_options, &option_index);
|
||||||
if(c==-1) break;
|
if(c==-1) break;
|
||||||
switch (c)
|
switch (c)
|
||||||
{
|
{
|
||||||
|
@ -79,6 +82,9 @@ int main(int argc, char* argv[])
|
||||||
case 'b':
|
case 'b':
|
||||||
bufsize=atoi(optarg);
|
bufsize=atoi(optarg);
|
||||||
break;
|
break;
|
||||||
|
case 'n':
|
||||||
|
bufcnt=atoi(optarg);
|
||||||
|
break;
|
||||||
case 'm':
|
case 'm':
|
||||||
ddc_method_str[100-1]=0;
|
ddc_method_str[100-1]=0;
|
||||||
strncpy(ddc_method_str,optarg,100-1);
|
strncpy(ddc_method_str,optarg,100-1);
|
||||||
|
@ -99,8 +105,9 @@ int main(int argc, char* argv[])
|
||||||
if(decimation<0) print_exit(MSG_START "invalid value for --decimation (should be >0).\n");
|
if(decimation<0) print_exit(MSG_START "invalid value for --decimation (should be >0).\n");
|
||||||
if(decimation==1) fprintf(stderr, MSG_START "decimation = 1, just copying raw samples.\n");
|
if(decimation==1) fprintf(stderr, MSG_START "decimation = 1, just copying raw samples.\n");
|
||||||
if(transition_bw<0||transition_bw>0.5) print_exit(MSG_START "invalid value for --transition (should be between 0 and 0.5).\n");
|
if(transition_bw<0||transition_bw>0.5) print_exit(MSG_START "invalid value for --transition (should be between 0 and 0.5).\n");
|
||||||
|
if(bufsize<0) print_exit(MSG_START "invalid value for --bufsize (should be >0)\n");
|
||||||
if(decimation==1); //don't do anything then
|
if(bufcnt<0) print_exit(MSG_START "invalid value for --bufcnt (should be >0)\n");
|
||||||
|
if(decimation==1); //don't do anything then //!will have to take care about this later
|
||||||
else if(!strcmp(ddc_method_str,"td"))
|
else if(!strcmp(ddc_method_str,"td"))
|
||||||
{
|
{
|
||||||
ddc_method = M_TD;
|
ddc_method = M_TD;
|
||||||
|
@ -161,9 +168,16 @@ int main(int argc, char* argv[])
|
||||||
maxfd(&highfd, input_fd);
|
maxfd(&highfd, input_fd);
|
||||||
|
|
||||||
//Set stdin and listen_socket to non-blocking
|
//Set stdin and listen_socket to non-blocking
|
||||||
if(set_nonblocking(input_fd) || set_nonblocking(listen_socket)) //don't do it before subprocess fork!
|
if(set_nonblocking(input_fd) || set_nonblocking(listen_socket))
|
||||||
error_exit(MSG_START "cannot set_nonblocking()");
|
error_exit(MSG_START "cannot set_nonblocking()");
|
||||||
|
|
||||||
|
//Create tsmpool
|
||||||
|
tsmpool* pool = new tsmpool(bufsize, bufcnt);
|
||||||
|
|
||||||
|
unsigned char* current_write_buffer = pool->get_write_buffer();
|
||||||
|
int index_in_current_write_buffer = 0;
|
||||||
|
|
||||||
|
|
||||||
for(;;)
|
for(;;)
|
||||||
{
|
{
|
||||||
//Let's wait until there is any new data to read, or any new connection!
|
//Let's wait until there is any new data to read, or any new connection!
|
||||||
|
@ -172,29 +186,42 @@ int main(int argc, char* argv[])
|
||||||
//Is there a new client connection?
|
//Is there a new client connection?
|
||||||
if( (new_socket = accept(listen_socket, (struct sockaddr*)&addr_cli, &addr_cli_len)) != -1)
|
if( (new_socket = accept(listen_socket, (struct sockaddr*)&addr_cli, &addr_cli_len)) != -1)
|
||||||
{
|
{
|
||||||
this_client = new client_t;
|
clients_close_all_finished();
|
||||||
this_client->error = 0;
|
int new_client_id = clients_get_new_id();
|
||||||
memcpy(&this_client->addr, &addr_cli, sizeof(this_client->addr));
|
if(new_client_id!=-1 && pthread_create(&new_client->thread, NULL, client_thread , (void*)&new_client)<0)
|
||||||
this_client->socket = new_socket;
|
|
||||||
this_client->id = thread_cntr++;
|
|
||||||
|
|
||||||
if(pthread_create(&this_client->thread, NULL, client_thread , (void*)&this_client)<0)
|
|
||||||
{
|
{
|
||||||
//We're the parent
|
//We're the parent
|
||||||
clients.push_back(this_client);
|
client_t* new_client = new client_t;
|
||||||
fprintf(stderr, MSG_START "pthread_create() done, this_client->id: %d\n", this_client->id);
|
new_client->error = 0;
|
||||||
|
memcpy(&new_client->addr, &addr_cli, sizeof(new_client->addr));
|
||||||
|
new_client->socket = new_socket;
|
||||||
|
new_client->id = new_client_id;
|
||||||
|
new_client->status = CS_CREATED;
|
||||||
|
clients.push_back(new_client);
|
||||||
|
fprintf(stderr, MSG_START "pthread_create() done, new_client->id: %d\n", new_client->id);
|
||||||
|
}
|
||||||
|
else fprintf(stderr, MSG_START "pthread_create() failed.");
|
||||||
|
}
|
||||||
|
|
||||||
|
if(index_in_current_write_buffer >= bufsize)
|
||||||
|
{
|
||||||
|
current_write_buffer = pool->get_write_buffer();
|
||||||
|
index_in_current_write_buffer = 0;
|
||||||
|
}
|
||||||
|
int retval = read(input_fd, current_write_buffer + index_in_current_write_buffer, bufsize - index_in_current_write_buffer);
|
||||||
|
if(retval>0)
|
||||||
|
{
|
||||||
|
index_in_current_write_buffer += retval;
|
||||||
|
}
|
||||||
|
else if(retval==0)
|
||||||
|
{
|
||||||
|
//!end of input stream, close clients and exit
|
||||||
|
print_exit(MSG_START "end of input, exiting.\n")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
float* pool_next = pool->get_write_buffer();
|
#if 0
|
||||||
|
|
||||||
int retval = read(input_fd, pool_next, mainpool->size);
|
|
||||||
if(retval==0)
|
|
||||||
{
|
|
||||||
//end of input stream, close clients and exit
|
|
||||||
}
|
|
||||||
else if(retval != -1)
|
|
||||||
{
|
|
||||||
for (int i=0; i<clients.size(); i++)
|
for (int i=0; i<clients.size(); i++)
|
||||||
{
|
{
|
||||||
if(write(clients[i]->pipefd[1], buf, retval)==-1)
|
if(write(clients[i]->pipefd[1], buf, retval)==-1)
|
||||||
|
@ -228,14 +255,37 @@ int main(int argc, char* argv[])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//TODO: at the end, server closes pipefd[1] for client
|
//TODO: at the end, server closes pipefd[1] for client
|
||||||
|
#endif
|
||||||
|
|
||||||
}
|
int clients_get_new_id()
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void* client_thread (void* param)
|
|
||||||
{
|
{
|
||||||
|
int new_id=-1;
|
||||||
|
for(int i=0; i<clients.size(); i++) if (new_id<clients[i]->id) new_id = clients[i]->id;
|
||||||
|
if(new_id!=INT_MAX) return ++new_id; //will also work if clients is empty (will return -1+1==0)
|
||||||
|
//should test this part, too:
|
||||||
|
for(new_id=0;new_id<INT_MAX;new_id++)
|
||||||
|
{
|
||||||
|
int found = 0;
|
||||||
|
for(int i=0; i<clients.size(); i++) if (clients[i]->id==new_id) { found = 1; break; }
|
||||||
|
if(found) continue;
|
||||||
|
else return new_id;
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
void clients_close_all_finished()
|
||||||
|
{
|
||||||
|
for(int i=0;i<clients.size();i++)
|
||||||
|
{
|
||||||
|
if(clients[i]->status == CS_THREAD_FINISHED) clients.erase(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void* client_thread (void* param) //!TODO
|
||||||
|
{
|
||||||
|
client_t* me_the_client = (client_t*)param;
|
||||||
|
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
void error_exit(const char* why)
|
void error_exit(const char* why)
|
||||||
|
@ -254,4 +304,3 @@ void maxfd(int* maxfd, int fd)
|
||||||
{
|
{
|
||||||
if(fd>=*maxfd) *maxfd=fd+1;
|
if(fd>=*maxfd) *maxfd=fd+1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
15
ddcd.h
15
ddcd.h
|
@ -9,6 +9,7 @@
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <limits.h>
|
||||||
|
|
||||||
#define SOFTWARE_NAME "ddcd"
|
#define SOFTWARE_NAME "ddcd"
|
||||||
#define MSG_START SOFTWARE_NAME ": "
|
#define MSG_START SOFTWARE_NAME ": "
|
||||||
|
@ -19,17 +20,25 @@ typedef enum ddc_method_e
|
||||||
M_FASTDDC
|
M_FASTDDC
|
||||||
} ddc_method_t;
|
} ddc_method_t;
|
||||||
|
|
||||||
|
typedef enum client_status_e
|
||||||
|
{
|
||||||
|
CS_CREATED,
|
||||||
|
CS_THREAD_RUNNING,
|
||||||
|
CS_THREAD_FINISHED
|
||||||
|
} client_status_t;
|
||||||
|
|
||||||
|
|
||||||
typedef struct client_s
|
typedef struct client_s
|
||||||
{
|
{
|
||||||
struct sockaddr_in addr;
|
struct sockaddr_in addr;
|
||||||
int id;
|
int id;
|
||||||
int socket;
|
int socket;
|
||||||
int error;
|
int error; //set to non-zero on error (data transfer failed)
|
||||||
pthread_t thread;
|
pthread_t thread;
|
||||||
|
client_status_t status;
|
||||||
|
|
||||||
} client_t;
|
} client_t;
|
||||||
|
|
||||||
void print_exit(const char* why);
|
void print_exit(const char* why);
|
||||||
void error_exit(const char* why);
|
void error_exit(const char* why);
|
||||||
void maxfd(int* maxfd, int fd);
|
void maxfd(int* maxfd, int fd);
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,10 @@
|
||||||
|
#include "tsmpool.h"
|
||||||
|
|
||||||
tsmpool::tsmpool(size_t size, int num)
|
tsmpool::tsmpool(size_t size, int num)
|
||||||
{
|
{
|
||||||
this->threads_cntr = 0;
|
this->threads_cntr = 0;
|
||||||
this->num = num;
|
|
||||||
this->size = size;
|
this->size = size;
|
||||||
|
this->num = num; //number of buffers of (size) to alloc
|
||||||
this->ok = 1;
|
this->ok = 1;
|
||||||
this->lowest_read_index = -1;
|
this->lowest_read_index = -1;
|
||||||
if (pthread_mutex_init(&this->mutex, NULL) != 0) this->ok=0;
|
if (pthread_mutex_init(&this->mutex, NULL) != 0) this->ok=0;
|
||||||
|
@ -19,7 +21,7 @@ void* tsmpool::get_write_buffer()
|
||||||
|
|
||||||
tsmthread_t* tsmpool::register_thread()
|
tsmthread_t* tsmpool::register_thread()
|
||||||
{
|
{
|
||||||
if(!ok) return -1;
|
if(!ok) return NULL;
|
||||||
pthread_mutex_lock(&this->mutex);
|
pthread_mutex_lock(&this->mutex);
|
||||||
tsmthread_t* thread = new tsmthread_t;
|
tsmthread_t* thread = new tsmthread_t;
|
||||||
thread->read_index = write_index;
|
thread->read_index = write_index;
|
||||||
|
|
|
@ -1,3 +1,8 @@
|
||||||
|
//tsmpool stands for Thread-Safe Memory Pool.
|
||||||
|
|
||||||
|
//It implements a big circular buffer that one thread writes into, and multiple threads read from.
|
||||||
|
//The reader threads have lower priority than the writer thread (they can be left behind if the don't read fast enough).
|
||||||
|
|
||||||
typedef struct tsmthread_s
|
typedef struct tsmthread_s
|
||||||
{
|
{
|
||||||
int read_index; //it always points to the next buffer to be read
|
int read_index; //it always points to the next buffer to be read
|
||||||
|
@ -25,5 +30,3 @@ public:
|
||||||
int index_next(int index) { return (index+1==size)?0:index; }
|
int index_next(int index) { return (index+1==size)?0:index; }
|
||||||
int index_before(int index) { return (index-1<0)?size-1:index; }
|
int index_before(int index) { return (index-1<0)?size-1:index; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue