working on distrib infrastructure more
git-svn-id: svn://anubis/fart/trunk@221 7f9b0f55-74a9-4bce-be96-3c2cd072584d
This commit is contained in:
parent
0e818d1a92
commit
74399fe145
@ -2,6 +2,13 @@
|
||||
#include "distrib.h"
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/ip.h>
|
||||
using namespace std;
|
||||
|
||||
int distrib::readHostFile(const char * filename)
|
||||
@ -21,3 +28,80 @@ int distrib::readHostFile(const char * filename)
|
||||
ifs.close();
|
||||
return 0;
|
||||
}
|
||||
|
||||
int distrib::connect()
|
||||
{
|
||||
int err = 0;
|
||||
for (int i = 0, sz = m_hosts.size(); i < sz; i++)
|
||||
{
|
||||
err += connect(m_hosts[i]);
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
int distrib::connect(const string & host)
|
||||
{
|
||||
int id = fork();
|
||||
if (id < 0) /* check for fork() error */
|
||||
{
|
||||
cerr << "Error forking: " << id << endl;
|
||||
return 1;
|
||||
}
|
||||
else if (id > 0) /* in the parent */
|
||||
{
|
||||
m_children.push_back(id);
|
||||
}
|
||||
else /* in the child */
|
||||
{
|
||||
execlp("ssh", "ssh", host.c_str(),
|
||||
"fart", "--host", m_servername.c_str(),
|
||||
"--port", m_serverport,
|
||||
NULL);
|
||||
cerr << "Error " << errno << " with execlp()!" << endl;
|
||||
exit(33);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int distrib::startServer()
|
||||
{
|
||||
char hostname[1000];
|
||||
gethostname(&hostname[0], 1000);
|
||||
m_servername = hostname;
|
||||
|
||||
m_listen_socket = socket(PF_INET, SOCK_STREAM, 0);
|
||||
if ( m_listen_socket == -1 )
|
||||
{
|
||||
cerr << "Error " << errno << " creating listen socket!" << endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
if ( listen(m_listen_socket, 5) == -1 )
|
||||
{
|
||||
cerr << "Error " << errno << " when trying to listen!" << endl;
|
||||
return 2;
|
||||
}
|
||||
|
||||
struct sockaddr_in addr;
|
||||
int addr_len = sizeof(struct sockaddr_in);
|
||||
getsockname(m_listen_socket,
|
||||
(struct sockaddr *) &addr,
|
||||
(socklen_t *) &addr_len);
|
||||
|
||||
int ip_addr = ntohl(addr.sin_addr.s_addr);
|
||||
m_serverport = ntohs(addr.sin_port);
|
||||
cout << "Listening on "
|
||||
<< (unsigned int) ((ip_addr >> 24) & 0xFF)
|
||||
<< '.'
|
||||
<< (unsigned int) ((ip_addr >> 16) & 0xFF)
|
||||
<< '.'
|
||||
<< (unsigned int) ((ip_addr >> 8) & 0xFF)
|
||||
<< '.'
|
||||
<< (unsigned int) (ip_addr & 0xFF)
|
||||
<< ':'
|
||||
<< m_serverport
|
||||
<< endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -9,9 +9,16 @@ class distrib
|
||||
{
|
||||
public:
|
||||
int readHostFile(const char * filename);
|
||||
int connect();
|
||||
int startServer();
|
||||
|
||||
protected:
|
||||
int connect(const std::string & host);
|
||||
std::vector<std::string> m_hosts;
|
||||
std::vector<int> m_children;
|
||||
std::string m_servername;
|
||||
int m_serverport;
|
||||
int m_listen_socket;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
Loading…
x
Reference in New Issue
Block a user