libs-gui/Tools/speech_recognizer/GSSpeechRecognitionServer.m
2020-02-08 06:08:27 -05:00

127 lines
2.9 KiB
Objective-C

/* Implementation of class GSSpeechRecognitionServer
Copyright (C) 2019 Free Software Foundation, Inc.
By: Gregory John Casamento
Date: Fri Dec 6 04:55:59 EST 2019
This file is part of the GNUstep Library.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110 USA.
*/
#import "GSSpeechRecognitionServer.h"
#import "GSSpeechRecognitionEngine.h"
#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
{
NSConnection *connection = [NSConnection defaultConnection];
[connection setRootObject: _sharedInstance];
RETAIN(connection);
if (NO == [connection registerName: @"GSSpeechRecognitionServer"])
{
NSLog(@"Could not register name, make sure another server is not running.");
return;
}
[[NSRunLoop currentRunLoop] run];
}
+ (id)sharedServer
{
_clients++;
NSLog(@"NSSpeechRecognizer server connection count = %d after connection", _clients);
return _sharedInstance;
}
- (id)init
{
if (nil == (self = [super init]))
{
return nil;
}
_engine = [GSSpeechRecognitionEngine defaultSpeechRecognitionEngine];
if (nil == _engine)
{
[self release];
return nil;
}
else
{
NSLog(@"Got engine %@", _engine);
}
return self;
}
- (void) startListening
{
[_engine startListening];
}
- (void) stopListening
{
[_engine stopListening];
}
@end