1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
|
/* FIXME: not used! just an unimplemented idea.
*/
#import "STRemoteConversation.h"
#import <Foundation/NSException.h>
#import <Foundation/NSNotification.h>
#import <Foundation/NSConnection.h>
#import <Foundation/NSDistantObject.h>
#import <Foundation/NSPortNameServer.h>
#import "STEnvironment.h"
@implementation STRemoteConversation
- initWithEnvironmentName:(NSString *)envName
host:(NSString *)host
language:(NSString *)langName
{
if ((self = [super init]) != nil)
{
if (!envName)
{
[NSException raise:@"STConversationException"
format:@"Unspecified environment name for a distant conversation"];
[self release];
return nil;
}
languageName = RETAIN(langName);
environmentName = RETAIN(envName);
hostName = RETAIN(host);
[self open];
}
return self;
}
- (void)open
{
NSString *envProcName;
NSPortNameServer *nameServer;
if (connection)
{
[NSException raise:@"STConversationException"
format:@"Unable to open conversation: already opened."];
return;
}
envProcName = [NSString stringWithFormat:@"STEnvironment:%@",
environmentName];
if ([hostName length] > 0)
nameServer = [NSSocketPortNameServer sharedInstance];
else
nameServer = [NSPortNameServer systemDefaultPortNameServer];
connection = [NSConnection connectionWithRegisteredName:envProcName
host:hostName
usingNameServer:nameServer];
RETAIN(connection);
if (!connection)
{
[NSException raise:@"STConversationException"
format:@"Connection failed for environment '%@'",
environmentName];
return;
}
environmentProcess = RETAIN([connection rootProxy]);
proxy = RETAIN([environmentProcess createConversation]);
[proxy setProtocolForProxy:@protocol(STConversation)];
if (languageName && ![languageName isEqual:@""])
{
[proxy setLanguage: languageName];
}
[[NSNotificationCenter defaultCenter]
addObserver: self
selector: @selector(connectionDidDie:)
name: NSConnectionDidDieNotification
object: connection];
}
- (void)close
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
RELEASE(proxy);
proxy = nil;
RELEASE(environmentProcess);
environmentProcess = nil;
[connection invalidate];
RELEASE(connection);
connection = nil;
}
- (void)dealloc
{
/* After this we should not have any connection, environmentProcess
nor proxy */
[self close];
RELEASE(environmentName);
RELEASE(hostName);
[super dealloc];
}
- (void)setLanguage:(NSString *)newLanguage
{
ASSIGN(languageName, newLanguage);
[proxy setLanguage:newLanguage];
}
/** Return name of the language used in the receiver conversation */
- (NSString *)language
{
return proxy != nil ? [proxy language] : languageName;
}
- (STEnvironment *)environment
{
/* FIXME: return local description */
return nil;
}
- (void)interpretScript:(bycopy NSString *)aString
{
[proxy interpretScript:aString];
}
- (id)result
{
return [proxy result];
}
- (bycopy id)resultByCopy
{
return [proxy resultByCopy];
}
- (void)connectionDidDie:(NSNotification *)notification
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
if(proxy)
{
NSLog(@"Connection did die for %@ (%@)", self, environmentName);
}
else
{
NSLog(@"Closing conversation (%@) with %@", self, environmentName);
}
RELEASE(proxy);
proxy = nil;
RELEASE(environmentProcess);
environmentProcess = nil;
RELEASE(connection);
connection = nil;
}
@end
|