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() : DirectD::DirectD() :
_app_pid(0),
_reader(&_cm, 1), _writer(&_cm, 0), _listener(&_cm, 0), _reader(&_cm, 1), _writer(&_cm, 0), _listener(&_cm, 0),
_shutdown(false) { _shutdown(false) {
} }
@ -143,6 +142,8 @@ DirectD::~DirectD() {
_cm.close_connection((*ci)); _cm.close_connection((*ci));
} }
_connections.clear(); _connections.clear();
kill_all();
} }
int int
@ -211,16 +212,29 @@ DirectD::server_ready(const string& client_host, int port) {
void void
DirectD::start_app(const string& cmd) { DirectD::start_app(const string& cmd) {
nout<<"start_app(cmd="<<cmd<<")"<<endl; nout<<"start_app(cmd="<<cmd<<")"<<endl;
_app_pid=StartApp(cmd); _pids.push_back(StartApp(cmd));
nout<<" _app_pid="<<_app_pid<<endl; nout<<" pid="<<_pids.back()<<endl;
} }
void void
DirectD::kill_app() { DirectD::kill_app(int index) {
if (_app_pid) { int i = _pids.size() - 1 - index % _pids.size();
nout<<"trying kill "<<_app_pid<<endl; PidStack::iterator pi = _pids.begin() + i;
TerminateApp(_app_pid, 1000); 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 void

View File

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

View File

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

View File

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

View File

@ -30,7 +30,7 @@ DirectDServer::handle_command(const string& cmd) {
if (cmd.size()==1) { if (cmd.size()==1) {
switch (cmd[0]) { switch (cmd[0]) {
case 'k': case 'k':
kill_app(); kill_app(0);
break; break;
case 'q': case 'q':
_shutdown=true; _shutdown=true;
@ -41,6 +41,14 @@ DirectDServer::handle_command(const string& cmd) {
} }
} else { } else {
switch (cmd[0]) { 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 '!': { case '!': {
string c=cmd.substr(1, string::npos); string c=cmd.substr(1, string::npos);
//read_command(c); //read_command(c);