Prechádzať zdrojové kódy

added kill all; fixed test client

Dave Schuyler 23 rokov pred
rodič
commit
f43c59c642

+ 21 - 7
direct/src/directd/directd.cxx

@@ -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);
-  nout<<"    _app_pid="<<_app_pid<<endl;
+  _pids.push_back(StartApp(cmd));
+  nout<<"    pid="<<_pids.back()<<endl;
+}
+
+void
+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
 void
-DirectD::kill_app() {
-  if (_app_pid) {
-    nout<<"trying kill "<<_app_pid<<endl;
-    TerminateApp(_app_pid, 1000);
+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

+ 14 - 9
direct/src/directd/directd.h

@@ -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
-  //                       client_ready() or "!".
-  //                "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
-  //                       client_ready(), it prefixes "!" for you.
+  //                "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
+  //                            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;
 
 

+ 1 - 1
direct/src/directdServer/directdClient.cxx

@@ -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;
 }
 }

+ 1 - 1
direct/src/directdServer/directdClient.h

@@ -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();

+ 9 - 1
direct/src/directdServer/directdServer.cxx

@@ -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);