added pthread support for starting the server listen/accept loop
git-svn-id: svn://anubis/fart/trunk@224 7f9b0f55-74a9-4bce-be96-3c2cd072584d
This commit is contained in:
parent
6fdaf01214
commit
d6aa5b6e69
2
Makefile
2
Makefile
@ -9,7 +9,7 @@ export CPPFLAGS += -I"$(shell pwd)"
|
||||
endif
|
||||
export CXXFLAGS := -Wall -O2
|
||||
|
||||
LDFLAGS := -lfl
|
||||
LDFLAGS := -lfl -lpthread
|
||||
|
||||
SUBDIRS := util shapes main parser distrib
|
||||
|
||||
|
@ -31,7 +31,7 @@ int distrib::readHostFile(const char * filename)
|
||||
return 0;
|
||||
}
|
||||
|
||||
int distrib::connect(const std::vector<std::string> & client_options)
|
||||
int distrib::startClients(const std::vector<std::string> & client_options)
|
||||
{
|
||||
int err = 0;
|
||||
for (int i = 0, sz = m_hosts.size(); i < sz; i++)
|
||||
@ -91,33 +91,33 @@ int distrib::connect(const string & host,
|
||||
return 0;
|
||||
}
|
||||
|
||||
int distrib::startServer(const std::vector<std::string> & client_options)
|
||||
void distrib_server(distrib * the_distrib)
|
||||
{
|
||||
char hostname[1000];
|
||||
gethostname(&hostname[0], 1000);
|
||||
m_servername = hostname;
|
||||
the_distrib->m_servername = hostname;
|
||||
|
||||
m_listen_socket = socket(PF_INET, SOCK_STREAM, 0);
|
||||
if ( m_listen_socket == -1 )
|
||||
int listen_socket = socket(PF_INET, SOCK_STREAM, 0);
|
||||
if ( listen_socket == -1 )
|
||||
{
|
||||
cerr << "Error " << errno << " creating listen socket!" << endl;
|
||||
return 1;
|
||||
exit(39);
|
||||
}
|
||||
|
||||
if ( listen(m_listen_socket, 5) == -1 )
|
||||
if ( listen(listen_socket, 5) == -1 )
|
||||
{
|
||||
cerr << "Error " << errno << " when trying to listen!" << endl;
|
||||
return 2;
|
||||
exit(40);
|
||||
}
|
||||
|
||||
struct sockaddr_in addr;
|
||||
int addr_len = sizeof(struct sockaddr_in);
|
||||
getsockname(m_listen_socket,
|
||||
getsockname(listen_socket,
|
||||
(struct sockaddr *) &addr,
|
||||
(socklen_t *) &addr_len);
|
||||
|
||||
int ip_addr = ntohl(addr.sin_addr.s_addr);
|
||||
m_serverport = ntohs(addr.sin_port);
|
||||
the_distrib->m_serverport = ntohs(addr.sin_port);
|
||||
cout << "Listening on "
|
||||
<< (unsigned int) ((ip_addr >> 24) & 0xFF)
|
||||
<< '.'
|
||||
@ -127,12 +127,18 @@ int distrib::startServer(const std::vector<std::string> & client_options)
|
||||
<< '.'
|
||||
<< (unsigned int) (ip_addr & 0xFF)
|
||||
<< ':'
|
||||
<< m_serverport
|
||||
<< the_distrib->m_serverport
|
||||
<< endl;
|
||||
}
|
||||
|
||||
connect(client_options);
|
||||
|
||||
return 0;
|
||||
int distrib::startServer()
|
||||
{
|
||||
int ret = pthread_create(&m_server_thread,
|
||||
NULL,
|
||||
(void * (*)(void *)) distrib_server,
|
||||
this);
|
||||
usleep(100000);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int distrib::startClient(const char * server, int port)
|
||||
|
@ -4,16 +4,18 @@
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <pthread.h>
|
||||
|
||||
class distrib
|
||||
{
|
||||
public:
|
||||
int readHostFile(const char * filename);
|
||||
int startServer(const std::vector<std::string> & client_options);
|
||||
int startServer();
|
||||
int startClient(const char * server, int port);
|
||||
int startClients(const std::vector<std::string> & client_options);
|
||||
friend void distrib_server(distrib * the_distrib);
|
||||
|
||||
protected:
|
||||
int connect(const std::vector<std::string> & client_options);
|
||||
int connect(const std::string & host,
|
||||
const std::vector<std::string> & client_options);
|
||||
|
||||
@ -22,6 +24,7 @@ class distrib
|
||||
std::string m_servername;
|
||||
int m_serverport;
|
||||
int m_listen_socket;
|
||||
pthread_t m_server_thread;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
@ -102,7 +102,8 @@ Scene::Scene(const map<string, const char *> & options,
|
||||
{
|
||||
if (m_hosts_file != "")
|
||||
m_distrib.readHostFile(m_hosts_file.c_str());
|
||||
m_distrib.startServer(m_client_options);
|
||||
m_distrib.startServer();
|
||||
m_distrib.startClients(m_client_options);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
Loading…
x
Reference in New Issue
Block a user