libs-gui/Tools/speech_recognizer/GSSpeechRecognitionServer.m

128 lines
2.9 KiB
Mathematica
Raw Normal View History

2020-02-07 09:35:35 +00:00
/* 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.
*/
2020-02-01 10:43:06 +00:00
#import "GSSpeechRecognitionServer.h"
#import "GSSpeechRecognitionEngine.h"
2020-01-30 21:28:18 +00:00
#import <Foundation/Foundation.h>
2020-02-08 07:45:40 +00:00
static GSSpeechRecognitionServer *_sharedInstance;
static int _clients = 0;
2020-02-01 10:43:06 +00:00
@implementation GSSpeechRecognitionServer
2020-01-30 21:28:18 +00:00
/**
* 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);
}
}
}
2020-01-30 21:28:18 +00:00
+ (void)initialize
{
2020-02-08 07:45:40 +00:00
_sharedInstance = [[self alloc] init];
_clients = 0;
[[NSNotificationCenter defaultCenter]
addObserver: self
selector: @selector(connectionDied:)
name: NSConnectionDidDieNotification
object: nil];
2020-01-30 21:28:18 +00:00
}
2020-02-01 10:43:06 +00:00
2020-01-30 21:28:18 +00:00
+ (void)start
{
2020-02-01 10:43:06 +00:00
NSConnection *connection = [NSConnection defaultConnection];
2020-02-08 07:45:40 +00:00
[connection setRootObject: _sharedInstance];
2020-02-08 08:58:13 +00:00
RETAIN(connection);
2020-02-01 10:43:06 +00:00
if (NO == [connection registerName: @"GSSpeechRecognitionServer"])
{
NSLog(@"Could not register name, make sure another server is not running.");
2020-02-01 10:43:06 +00:00
return;
}
2020-02-08 08:58:13 +00:00
[[NSRunLoop currentRunLoop] run];
2020-01-30 21:28:18 +00:00
}
2020-02-01 10:43:06 +00:00
2020-01-30 21:28:18 +00:00
+ (id)sharedServer
{
_clients++;
NSLog(@"NSSpeechRecognizer server connection count = %d after connection", _clients);
2020-02-08 07:45:40 +00:00
return _sharedInstance;
2020-01-30 21:28:18 +00:00
}
2020-02-01 10:43:06 +00:00
2020-01-30 21:28:18 +00:00
- (id)init
{
2020-02-07 09:25:39 +00:00
if (nil == (self = [super init]))
{
return nil;
}
2020-02-03 16:02:03 +00:00
_engine = [GSSpeechRecognitionEngine defaultSpeechRecognitionEngine];
2020-02-07 09:25:39 +00:00
2020-02-03 16:02:03 +00:00
if (nil == _engine)
2020-02-01 10:43:06 +00:00
{
[self release];
return nil;
}
2020-02-06 09:29:51 +00:00
else
{
NSLog(@"Got engine %@", _engine);
}
2020-02-07 09:25:39 +00:00
2020-02-01 10:43:06 +00:00
return self;
2020-01-30 21:28:18 +00:00
}
2020-02-01 10:43:06 +00:00
2020-02-03 06:02:50 +00:00
- (void) startListening
{
[_engine startListening];
2020-02-03 06:02:50 +00:00
}
- (void) stopListening
{
[_engine stopListening];
2020-02-03 06:02:50 +00:00
}
2020-01-30 21:28:18 +00:00
@end