Author: rfm
Date: Sun Nov  2 17:06:16 2014
New Revision: 38149

URL: http://svn.gna.org/viewcvs/gnustep?rev=38149&view=rev
Log:
add restart command

Modified:
    libs/ec/trunk/ChangeLog
    libs/ec/trunk/EcCommand.m

Modified: libs/ec/trunk/ChangeLog
URL: 
http://svn.gna.org/viewcvs/gnustep/libs/ec/trunk/ChangeLog?rev=38149&r1=38148&r2=38149&view=diff
==============================================================================
--- libs/ec/trunk/ChangeLog     (original)
+++ libs/ec/trunk/ChangeLog     Sun Nov  2 17:06:16 2014
@@ -5,6 +5,9 @@
        Add method to register a user default / configuration key to have
        updates for a default automatically trigger a method to handle it,
        and to provide 'help' documentation for command line arguments.
+       * EcCommand.m:
+       Implement 'restart' command for when we want to quit a server and
+       have it start up again.
 
 2014-11-01  Richard Frith-Macdonald <[email protected]>
 

Modified: libs/ec/trunk/EcCommand.m
URL: 
http://svn.gna.org/viewcvs/gnustep/libs/ec/trunk/EcCommand.m?rev=38149&r1=38148&r2=38149&view=diff
==============================================================================
--- libs/ec/trunk/EcCommand.m   (original)
+++ libs/ec/trunk/EcCommand.m   Sun Nov  2 17:06:16 2014
@@ -189,6 +189,7 @@
                      name: (NSString*)n
                 transient: (BOOL)t;
 - (void) reply: (NSString*) msg to: (NSString*)n from: (NSString*)c;
+- (NSArray*) restartAll;
 - (void) terminate;
 - (void) timedOut: (NSTimer*)t;
 - (void) unregisterByObject: (id)obj;
@@ -754,7 +755,8 @@
          if ([wd length] == 0)
            {
              m = @"Commands are -\n"
-             @"Help\tArchive\tControl\tLaunch\tList\tMemory\tQuit\tTell\n\n"
+             @"Help\tArchive\tControl\tLaunch\tList\tMemory\t"
+              @"Quit\tRestart\tTell\n\n"
              @"Type 'help' followed by a command word for details.\n"
              @"A command line consists of a sequence of words, "
              @"the first of which is the command to be executed. "
@@ -817,7 +819,16 @@
                      @"Quit all\n"
                      @"Shuts down all client processes.\n"
                      @"Quit self\n"
-                     @"Shuts down the command server for this host.\n";
+                     @"Shuts down the Command server for this host.\n";
+               }
+             else if (comp(wd, @"Restart") >= 0)
+               {
+                 m = @"Restart 'name'\n"
+                     @"Shuts down and starts the named client process(es).\n"
+                     @"Restart all\n"
+                     @"Shuts down and starts all client processes.\n"
+                     @"Restart self\n"
+                     @"Shuts down and starts Command server for this host.\n";
                }
              else if (comp(wd, @"Tell") >= 0)
                {
@@ -1152,6 +1163,93 @@
              m = @"Quit what?.\n";
            }
        }
+      else if (comp(wd, @"restart") >= 0)
+       {
+         wd = cmdWord(cmd, 1);
+         if ([wd length] > 0)
+           {
+             if (comp(wd, @"self") == 0)
+               {
+                 if (terminating == nil)
+                   {
+                     NS_DURING
+                       {
+                          [self information: @"Re-starting Command server\n"
+                                       from: t
+                                         to: f
+                                       type: LT_AUDIT];
+                         [control unregister: self];
+                       }
+                     NS_HANDLER
+                       {
+                         NSLog(@"Exception unregistering from Control: %@",
+                           localException);
+                       }
+                     NS_ENDHANDLER
+                     exit(1);          // Watcher should restart us
+                   }
+                 else
+                   {
+                     m = @"Already terminating!\n";
+                   }
+               }
+             else if (comp(wd, @"all") == 0)
+               {
+                 NSArray       *a;
+
+                  a = [self restartAll];
+                 if ([a count] == 0)
+                   {
+                     m = @"All clients have been shut down for restart.\n";
+                   }
+                 else if ([a count] == 1)
+                   {
+                     m = @"One client did not shut down for restart.\n";
+                   }
+                 else
+                   {
+                     m = @"Some clients did not shut down for restart.\n";
+                   }
+               }
+             else
+               {
+                 NSArray       *a = [self findAll: clients byAbbreviation: wd];
+                 unsigned      i;
+                 BOOL          found = NO;
+                  NSDate        *when;
+
+                  when = [NSDate dateWithTimeIntervalSinceNow: 30.0 - DLY];
+                 for (i = 0; i < [a count]; i++)
+                   {
+                     EcClientI *c = [a objectAtIndex: i];
+
+                     NS_DURING
+                       {
+                         [launches setObject: when forKey: [c name]];
+                         m = [m stringByAppendingFormat: 
+                           @"  The process '%@' should restart shortly.\n",
+                           [c name]];
+                         [[c obj] cmdQuit: 0];
+                         found = YES;
+                       }
+                     NS_HANDLER
+                       {
+                         NSLog(@"Caught exception: %@", localException);
+                       }
+                     NS_ENDHANDLER
+                   } 
+                 if (NO == found)
+                   {
+                     m = [NSString stringWithFormat: 
+                       @"Nothing to restart as '%@'\n", wd];
+                   }
+               }
+           }
+         else
+           {
+             m = @"Restart what?.\n";
+           }
+       }
       else if (comp(wd, @"tell") >= 0)
        {
          wd = cmdWord(cmd, 1);
@@ -2172,6 +2270,88 @@
     }
 }
 
+- (NSArray*) restartAll
+{
+  NSMutableArray       *a = nil;
+
+  /* Quit tasks, but don't suspend them.
+   */
+  if ([clients count] > 0)
+    {
+      unsigned i;
+      unsigned j;
+      NSDate    *when;
+
+
+      /* We tell all connected clients to quit.
+       */
+      a = [[clients mutableCopy] autorelease];
+      i = [a count];
+      when = [NSDate dateWithTimeIntervalSinceNow: 30.0 - DLY];
+      while (i-- > 0)
+       {
+         EcClientI     *c = [a objectAtIndex: i];
+
+         if ([clients indexOfObjectIdenticalTo: c] == NSNotFound)
+            {
+              [a removeObjectAtIndex: i];
+            }
+          else
+           {
+             NS_DURING
+               {
+                 [launches setObject: when forKey: [c name]];
+                 [[c obj] cmdQuit: 0];
+               }
+             NS_HANDLER
+               {
+                 NSLog(@"Caught exception: %@", localException);
+               }
+             NS_ENDHANDLER
+           }
+       }
+
+      /* Give the clients a short time to quit, and re-send
+       * the instruction to any which haven't budged.
+       */
+      for (j = 0; j < 15; j++)
+       {
+         NSDate        *next = [NSDate dateWithTimeIntervalSinceNow: 2.0];
+
+         while ([a count] > 0 && [next timeIntervalSinceNow] > 0.0)
+           {
+             [[NSRunLoop currentRunLoop] runMode: NSDefaultRunLoopMode
+                                      beforeDate: next];
+           }
+         i = [a count];
+          when = [NSDate dateWithTimeIntervalSinceNow: 30.0 - DLY];
+          while (i-- > 0)
+           {
+              EcClientI        *c = [a objectAtIndex: i];
+
+              if ([clients indexOfObjectIdenticalTo: c] == NSNotFound)
+                {
+                  [a removeObjectAtIndex: i];
+                }
+              else
+               {
+                 NS_DURING
+                   {
+                      [launches setObject: when forKey: [c name]];
+                      [[c obj] cmdQuit: 0];
+                   }
+                 NS_HANDLER
+                   {
+                     NSLog(@"Caught exception: %@", localException);
+                   }
+                 NS_ENDHANDLER
+               }
+           }
+       }
+    }
+  return a;
+}
+
 - (NSString*) makeSpace
 {
   NSInteger             deleteAfter;


_______________________________________________
Gnustep-cvs mailing list
[email protected]
https://mail.gna.org/listinfo/gnustep-cvs

Reply via email to