Shut down server when all connections are dead

This commit is contained in:
Gregory John Casamento 2020-02-08 06:08:27 -05:00
parent eda8259f53
commit 07bf3d32e4
3 changed files with 42 additions and 27 deletions

View file

@ -36,30 +36,11 @@
id _speechRecognitionServer = nil;
BOOL _serverLaunchTested = NO;
static int clients;
#define SPEECH_RECOGNITION_SERVER @"GSSpeechRecognitionServer"
@implementation NSSpeechRecognizer
/**
* If the remote end exits before freeing the GSSpeechRecognizer then we need
* to send it a -release message to make sure it dies.
*/
+ (void)connectionDied: (NSNotification*)aNotification
{
NSEnumerator *e = [[[aNotification object] localObjects] objectEnumerator];
NSObject *o = nil;
for (o = [e nextObject] ; nil != o ; o = [e nextObject])
{
if ([o isKindOfClass: self])
{
[o release];
}
}
}
+ (void) initialize
{
if (self == [NSSpeechRecognizer class])
@ -71,12 +52,6 @@ static int clients;
showIcon: NO
autolaunch: NO];
}
[[NSNotificationCenter defaultCenter]
addObserver: self
selector: @selector(connectionDied:)
name: NSConnectionDidDieNotification
object: nil];
}
}
@ -123,7 +98,6 @@ static int clients;
if (nil != _speechRecognitionServer)
{
NSLog(@"Server found!!!");
clients++;
break;
}
[NSThread sleepForTimeInterval: 0.1];

View file

@ -27,12 +27,49 @@
#import <Foundation/Foundation.h>
static GSSpeechRecognitionServer *_sharedInstance;
static int _clients = 0;
@implementation GSSpeechRecognitionServer
/**
* Monitor connection...
*/
+ (void)connectionDied: (NSNotification*)aNotification
{
NSArray *objs = [[aNotification object] localObjects];
NSEnumerator *en = [objs objectEnumerator];
id o = nil;
if(_clients > 0)
{
_clients--;
}
if(_clients <= 0)
{
NSLog(@"Client count is zero, exiting");
exit(0);
}
NSLog(@"NSSpeechRecognizer server connection count = %d after disconnection", _clients);
while((o = [en nextObject]) != nil)
{
if ([o isKindOfClass: self])
{
RELEASE(o);
}
}
}
+ (void)initialize
{
_sharedInstance = [[self alloc] init];
_clients = 0;
[[NSNotificationCenter defaultCenter]
addObserver: self
selector: @selector(connectionDied:)
name: NSConnectionDidDieNotification
object: nil];
}
+ (void)start
@ -42,6 +79,7 @@ static GSSpeechRecognitionServer *_sharedInstance;
RETAIN(connection);
if (NO == [connection registerName: @"GSSpeechRecognitionServer"])
{
NSLog(@"Could not register name, make sure another server is not running.");
return;
}
[[NSRunLoop currentRunLoop] run];
@ -49,6 +87,8 @@ static GSSpeechRecognitionServer *_sharedInstance;
+ (id)sharedServer
{
_clients++;
NSLog(@"NSSpeechRecognizer server connection count = %d after connection", _clients);
return _sharedInstance;
}

View file

@ -151,7 +151,7 @@ static const arg_t cont_args_def[] = {
utt_started = NO;
NSLog(@"Ready....");
while(YES)
while([_listeningThread isCancelled] == NO)
{
if ((k = ad_read(ad, adbuf, 2048)) < 0)
{
@ -208,6 +208,7 @@ static const arg_t cont_args_def[] = {
- (void) stopListening
{
NSLog(@"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ STOP SERVER");
[_listeningThread cancel];
RELEASE(_listeningThread);
_listeningThread = nil;