Collapse server into normal instance

This commit is contained in:
Gregory John Casamento 2020-02-08 02:45:40 -05:00
parent 3468a92539
commit 60cf308471
4 changed files with 36 additions and 72 deletions

View file

@ -36,23 +36,34 @@
id _speechRecognitionServer = nil;
BOOL _serverLaunchTested = NO;
static int clients;
#define SPEECH_RECOGNITION_SERVER @"GSSpeechRecognitionServer"
@interface NSObject (GSSpeechRecognitionServer)
- (NSSpeechRecognizer *) newRecognizer;
@end
@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])
{
_speechRecognitionServer = [NSConnection
rootProxyForConnectionWithRegisteredName: SPEECH_RECOGNITION_SERVER
host: nil];
RETAIN(_speechRecognitionServer);
if (nil == _speechRecognitionServer)
{
NSWorkspace *ws = [NSWorkspace sharedWorkspace];
@ -60,10 +71,12 @@ BOOL _serverLaunchTested = NO;
showIcon: NO
autolaunch: NO];
}
else
{
NSLog(@"Server found in +initialize");
}
[[NSNotificationCenter defaultCenter]
addObserver: self
selector: @selector(connectionDied:)
name: NSConnectionDidDieNotification
object: nil];
}
}
@ -94,18 +107,12 @@ BOOL _serverLaunchTested = NO;
selector: @selector(processNotification:)
name: GSSpeechRecognizerDidRecognizeWordNotification
object: nil];
}
return self;
}
+ (id) allocWithZone: (NSZone *)aZone
{
if (self == [NSSpeechRecognizer class])
{
if (nil == _speechRecognitionServer && !_serverLaunchTested)
{
unsigned int i = 0;
// Wait for up to five seconds for the server to launch, then give up.
for (i=0 ; i < 50 ; i++)
{
@ -116,6 +123,7 @@ BOOL _serverLaunchTested = NO;
if (nil != _speechRecognitionServer)
{
NSLog(@"Server found!!!");
clients++;
break;
}
[NSThread sleepForTimeInterval: 0.1];
@ -125,12 +133,8 @@ BOOL _serverLaunchTested = NO;
// launch the next time if it didn't work this time.
_serverLaunchTested = YES;
}
// If there is no server, this will return nil
return [_speechRecognitionServer newRecognizer];
}
return [super allocWithZone: aZone];
return self;
}
// Delegate
@ -188,11 +192,11 @@ BOOL _serverLaunchTested = NO;
// Listening
- (void) startListening
{
[self subclassResponsibility: _cmd];
[_speechRecognitionServer startListening];
}
- (void) stopListening
{
[self subclassResponsibility: _cmd];
[_speechRecognitionServer stopListening];
}
@end

View file

@ -14,7 +14,6 @@ GSSpeechRecognitionServer_LANGUAGES = English
GSSpeechRecognitionServer_OBJC_FILES = \
GSSpeechRecognitionEngine.m \
GSSpeechRecognitionServer.m \
GSSpeechRecognizer.m \
main.m

View file

@ -27,29 +27,29 @@
#import "GSSpeechRecognizer.h"
#import <Foundation/Foundation.h>
static GSSpeechRecognitionServer *sharedInstance;
static GSSpeechRecognitionServer *_sharedInstance;
@implementation GSSpeechRecognitionServer
+ (void)initialize
{
sharedInstance = [self new];
_sharedInstance = [[self alloc] init];
}
+ (void)start
{
NSConnection *connection = [NSConnection defaultConnection];
[connection setRootObject: sharedInstance];
[connection setRootObject: _sharedInstance];
if (NO == [connection registerName: @"GSSpeechRecognitionServer"])
{
return;
}
[[NSRunLoop currentRunLoop] run];
// [[NSRunLoop currentRunLoop] run];
}
+ (id)sharedServer
{
return sharedInstance;
return _sharedInstance;
}
- (id)init
@ -74,13 +74,6 @@ static GSSpeechRecognitionServer *sharedInstance;
return self;
}
- (id)newRecognizer
{
GSSpeechRecognizer *r = [[GSSpeechRecognizer alloc] init];
RETAIN(r);
return r;
}
- (void) startListening
{
[_engine startListening];

View file

@ -25,44 +25,12 @@
#import "GSSpeechRecognizer.h"
static GSSpeechRecognitionServer *server;
static int clients;
@interface GSSpeechRecognizer (Private)
+ (void)connectionDied: (NSNotification*)aNotification;
@end
@implementation GSSpeechRecognizer
+ (void)initialize
{
if (self == [GSSpeechRecognizer class])
{
server = [GSSpeechRecognitionServer sharedServer];
RETAIN(server);
[[NSNotificationCenter defaultCenter]
addObserver: self
selector: @selector(connectionDied:)
name: NSConnectionDidDieNotification
object: nil];
}
}
/**
* 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];
}
}
}
/**
* If no clients have been active for some time, kill the speech server to