added kill all; fixed test client

This commit is contained in:
Dave Schuyler 2002-05-07 04:45:06 +00:00
parent 9ea3af0608
commit f43c59c642
5 changed files with 46 additions and 19 deletions

View File

@ -131,7 +131,6 @@ namespace {
DirectD::DirectD() :
_app_pid(0),
_reader(&_cm, 1), _writer(&_cm, 0), _listener(&_cm, 0),
_shutdown(false) {
}
@ -143,6 +142,8 @@ DirectD::~DirectD() {
_cm.close_connection((*ci));
}
_connections.clear();
kill_all();
}
int
@ -211,18 +212,31 @@ DirectD::server_ready(const string& client_host, int port) {
void
DirectD::start_app(const string& cmd) {
nout<<"start_app(cmd="<<cmd<<")"<<endl;
_app_pid=StartApp(cmd);
nout<<" _app_pid="<<_app_pid<<endl;
_pids.push_back(StartApp(cmd));
nout<<" pid="<<_pids.back()<<endl;
}
void
DirectD::kill_app() {
if (_app_pid) {
nout<<"trying kill "<<_app_pid<<endl;
TerminateApp(_app_pid, 1000);
DirectD::kill_app(int index) {
int i = _pids.size() - 1 - index % _pids.size();
PidStack::iterator pi = _pids.begin() + i;
if (pi!=_pids.end()) {
nout<<"trying kill "<<(*pi)<<endl;
TerminateApp((*pi), 1000);
_pids.erase(pi);
}
}
void
DirectD::kill_all() {
PidStack::reverse_iterator pi;
for (pi = _pids.rbegin(); pi != _pids.rend(); ++pi) {
nout<<"trying kill "<<(*pi)<<endl;
TerminateApp((*pi), 1000);
}
_pids.clear();
}
void
DirectD::send_command(const string& cmd) {
NetDatagram datagram;

View File

@ -86,12 +86,15 @@ PUBLISHED:
// Description: Tell the server to do the command cmd.
// cmd is one of the following:
// "k" Kill the most recent application started with
// client_ready() or "!".
// "k[<n>]" Kill the most recent application
// started with client_ready() or "!".
// Or kill the nth most recent or 'a' for All.
// E.g. "k", "k0", "k2", "ka".
// "q" Tell the server to quit.
// "!cmd" Exectue the cmd on the server (this is a dos shell
// command; if you want a bash command, include bash
// in the command e.g. "!bash pwd"). When you call
// "!cmd" Exectue the cmd on the server (this
// is a dos shell command; if you want
// a bash command, include bash in the
// command e.g. "!bash pwd"). When you call
// client_ready(), it prefixes "!" for you.
// A new connection will be created and closed.
int tell_server(const string& server_host, int port, const string& cmd);
@ -129,7 +132,8 @@ PUBLISHED:
protected:
void start_app(const string& cmd);
void kill_app();
void kill_app(int index);
void kill_all();
virtual void handle_command(const string& cmd);
void handle_datagram(NetDatagram& datagram);
void send_one_message(const string& host_name,
@ -140,7 +144,8 @@ protected:
ConnectionWriter _writer;
QueuedConnectionListener _listener;
intptr_t _app_pid;
typedef pvector< long /*intptr_t*/ > PidStack;
PidStack _pids;
typedef pset< PT(Connection) > ConnectionSet;
ConnectionSet _connections;

View File

@ -84,7 +84,7 @@ main(int argc, char *argv[]) {
port=(atoi(argv[argc-1]));
}
DirectDClient directd;
directd.run_client(port);
directd.run_client(host, port);
return 0;
}

View File

@ -19,7 +19,7 @@
#include "directd.h"
// Description: DirectDClient is a test app for DriectDServer.
class EXPCL_DIRECT DirectDClient: public DirectD {
class DirectDClient: public DirectD {
public:
DirectDClient();
~DirectDClient();

View File

@ -30,7 +30,7 @@ DirectDServer::handle_command(const string& cmd) {
if (cmd.size()==1) {
switch (cmd[0]) {
case 'k':
kill_app();
kill_app(0);
break;
case 'q':
_shutdown=true;
@ -41,6 +41,14 @@ DirectDServer::handle_command(const string& cmd) {
}
} else {
switch (cmd[0]) {
case 'k':
if (cmd[1]=='a') {
kill_all();
} else {
int index = atoi(cmd.substr(1, string::npos).c_str());
kill_app(index);
}
break;
case '!': {
string c=cmd.substr(1, string::npos);
//read_command(c);