reworking distrib & Scene to work together
git-svn-id: svn://anubis/fart/trunk@226 7f9b0f55-74a9-4bce-be96-3c2cd072584d
This commit is contained in:
1 parent
fdf34009bf
commit
33f554d714
5 files changed
+133
-34
No files matched your search
+50
-5
@@ -6,6 +6,8 @@
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#include <netdb.h> /* gethostbyname() */
|
||||
#include <string.h> /* memset() */
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/ip.h>
|
||||
@@ -39,15 +41,15 @@ int distrib::readHostFile(const char * filename)
|
||||
|
||||
int distrib::startClients(const std::vector<std::string> & client_options)
|
||||
{
|
||||
int err = 0;
|
||||
int ret = 0;
|
||||
for (int i = 0, sz = m_hosts.size(); i < sz; i++)
|
||||
{
|
||||
err += connect(m_hosts[i], client_options);
|
||||
ret += clientConnect(m_hosts[i], client_options);
|
||||
}
|
||||
return err;
|
||||
return ret;
|
||||
}
|
||||
|
||||
int distrib::connect(const string & host,
|
||||
int distrib::clientConnect(const string & host,
|
||||
const std::vector<std::string> & client_options)
|
||||
{
|
||||
int id = fork();
|
||||
@@ -59,6 +61,7 @@ int distrib::connect(const string & host,
|
||||
else if (id > 0) /* in the parent */
|
||||
{
|
||||
m_children.push_back(id);
|
||||
m_num_clients++;
|
||||
}
|
||||
else /* in the child */
|
||||
{
|
||||
@@ -144,8 +147,10 @@ void distrib_server(distrib * the_distrib)
|
||||
|
||||
int distrib::startServer()
|
||||
{
|
||||
m_server = true;
|
||||
|
||||
pthread_mutex_lock(&m_listen_mutex);
|
||||
|
||||
|
||||
/* start the listen thread */
|
||||
int ret = pthread_create(&m_server_thread,
|
||||
NULL,
|
||||
@@ -163,5 +168,45 @@ int distrib::startServer()
|
||||
|
||||
int distrib::startClient(const char * server, int port)
|
||||
{
|
||||
m_server = false;
|
||||
|
||||
m_client_socket = socket(PF_INET, SOCK_STREAM, 0);
|
||||
if (m_client_socket < 0)
|
||||
{
|
||||
cerr << "Error creating client socket: " << errno << endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
struct addrinfo hint;
|
||||
memset(&hint, 0, sizeof(hint));
|
||||
hint.ai_family = AF_INET;
|
||||
hint.ai_socktype = SOCK_STREAM;
|
||||
|
||||
struct addrinfo * res;
|
||||
char portstr[15];
|
||||
sprintf(portstr, "%d", port);
|
||||
getaddrinfo(server, portstr, &hint, &res);
|
||||
|
||||
if (connect(m_client_socket, res->ai_addr, res->ai_addrlen) == -1)
|
||||
{
|
||||
cerr << "Error connecting from client socket: " << errno << endl;
|
||||
return 2;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int distrib::send_data(int task, unsigned char * data, int num_bytes)
|
||||
{
|
||||
}
|
||||
|
||||
int distrib::getTask()
|
||||
{
|
||||
if (m_server)
|
||||
{
|
||||
}
|
||||
else
|
||||
{
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
+11
-2
@@ -14,20 +14,29 @@ class distrib
|
||||
int startServer();
|
||||
int startClient(const char * server, int port);
|
||||
int startClients(const std::vector<std::string> & client_options);
|
||||
int getNumClients() { return m_num_clients; }
|
||||
void set_data(unsigned char * data) { m_data = data; }
|
||||
int getTask();
|
||||
int send_data(int task, unsigned char * data, int num_bytes);
|
||||
|
||||
friend void distrib_server(distrib * the_distrib);
|
||||
|
||||
protected:
|
||||
int connect(const std::string & host,
|
||||
const std::vector<std::string> & client_options);
|
||||
int clientConnect(const std::string & host,
|
||||
const std::vector<std::string> & client_options);
|
||||
|
||||
std::vector<std::string> m_hosts;
|
||||
std::vector<int> m_children;
|
||||
std::string m_servername;
|
||||
int m_serverport;
|
||||
int m_listen_socket;
|
||||
int m_client_socket;
|
||||
pthread_t m_server_thread;
|
||||
pthread_cond_t m_listen_cond;
|
||||
pthread_mutex_t m_listen_mutex;
|
||||
int m_num_clients;
|
||||
unsigned char * m_data;
|
||||
bool m_server;
|
||||
};
|
||||
|
||||
#endif
|
||||
Reference in new issue
Block a user