1994-11-04 16:29:24 +00:00
|
|
|
#include "second-server.h"
|
|
|
|
#include "second-client.h"
|
|
|
|
#include <objects/Connection.h>
|
1996-03-07 18:15:25 +00:00
|
|
|
#include <objects/TcpPort.h>
|
1995-03-23 03:58:37 +00:00
|
|
|
#include <objects/String.h>
|
1996-03-06 14:48:59 +00:00
|
|
|
#include <objects/Notification.h>
|
1996-03-07 18:15:25 +00:00
|
|
|
#include <objects/Invocation.h>
|
|
|
|
|
|
|
|
id announce_new_port (id notification)
|
|
|
|
{
|
|
|
|
id in_port = [notification object];
|
|
|
|
id out_port = [notification userInfo];
|
|
|
|
printf ("{%@}\n\tconnected to\n\t{%@}\n",
|
|
|
|
[out_port description], [in_port description]);
|
|
|
|
printf ("Now servicing %d connection(s).\n",
|
|
|
|
[in_port numberOfConnectedOutPorts]);
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
id announce_broken_port (id notification)
|
|
|
|
{
|
|
|
|
id in_port = [notification object];
|
|
|
|
id out_port = [notification userInfo];
|
|
|
|
printf ("{%@}\n\tdisconnected from\n\t{%@}\n",
|
|
|
|
[out_port description], [in_port description]);
|
|
|
|
printf ("Now servicing %d connection(s).\n",
|
|
|
|
[in_port numberOfConnectedOutPorts]);
|
|
|
|
return nil;
|
|
|
|
}
|
1994-11-04 16:29:24 +00:00
|
|
|
|
|
|
|
@implementation SecondServer
|
|
|
|
|
|
|
|
- init
|
|
|
|
{
|
|
|
|
[super init];
|
|
|
|
array = [[Array alloc] init];
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- addRemoteObject: o
|
|
|
|
{
|
|
|
|
const char *s;
|
|
|
|
[array addObject:o];
|
|
|
|
|
|
|
|
/* This next line is a callback */
|
|
|
|
s = [o appellation];
|
|
|
|
printf("Added remote object with appellation %s\n", s);
|
|
|
|
|
|
|
|
/* Free it because the remote messaging system malloc'ed it for us,
|
|
|
|
and we don't need it anymore. */
|
|
|
|
(*objc_free)((void*)s);
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- array
|
|
|
|
{
|
|
|
|
return array;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (Connection*) connection: ancestor didConnect: newConn
|
|
|
|
{
|
1996-03-06 14:48:59 +00:00
|
|
|
printf(">>>>New connection 0x%x created\n", (unsigned)newConn);
|
|
|
|
[NotificationDispatcher
|
|
|
|
addObserver: self
|
|
|
|
selector: @selector(connectionBecameInvalid:)
|
|
|
|
name: ConnectionBecameInvalidNotification
|
|
|
|
object: newConn];
|
|
|
|
[newConn setDelegate: self];
|
1994-11-04 16:29:24 +00:00
|
|
|
return newConn;
|
|
|
|
}
|
|
|
|
|
1996-03-06 14:48:59 +00:00
|
|
|
- connectionBecameInvalid: notification
|
1994-11-04 16:29:24 +00:00
|
|
|
{
|
1996-03-06 14:48:59 +00:00
|
|
|
id sender = [notification object];
|
1994-11-04 16:29:24 +00:00
|
|
|
if ([sender isKindOf:[Connection class]])
|
|
|
|
{
|
|
|
|
id remotes = [sender proxies];
|
|
|
|
int remotesCount = [remotes count];
|
|
|
|
int arrayCount = [array count];
|
|
|
|
int i, j;
|
|
|
|
|
1996-03-06 14:48:59 +00:00
|
|
|
printf(">>> Connection 0x%x invalidated\n", (unsigned)sender);
|
1994-11-04 16:29:24 +00:00
|
|
|
|
|
|
|
/* This contortion avoids Array's calling -isEqual: on the proxy */
|
|
|
|
for (j = 0; j < remotesCount; j++)
|
|
|
|
for (i = 0; i < arrayCount; i++)
|
|
|
|
if ([remotes objectAtIndex:j] == [array objectAtIndex:i])
|
|
|
|
{
|
|
|
|
printf("removing remote proxy from the list\n");
|
|
|
|
[array removeObjectAtIndex:j];
|
|
|
|
break;
|
|
|
|
}
|
1995-06-30 20:28:51 +00:00
|
|
|
[remotes release];
|
1994-11-04 16:29:24 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
[self error:"non-Connection sent invalidation"];
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
id s;
|
|
|
|
id c;
|
|
|
|
|
|
|
|
s = [[SecondServer alloc] init];
|
|
|
|
|
1996-03-07 18:15:25 +00:00
|
|
|
[NotificationDispatcher
|
|
|
|
addInvocation: [[ObjectFunctionInvocation alloc]
|
|
|
|
initWithObjectFunction: announce_broken_port]
|
|
|
|
name: InPortClientBecameInvalidNotification
|
|
|
|
object: nil];
|
|
|
|
[NotificationDispatcher
|
|
|
|
addInvocation: [[ObjectFunctionInvocation alloc]
|
|
|
|
initWithObjectFunction: announce_new_port]
|
|
|
|
name: InPortAcceptedClientNotification
|
|
|
|
object: nil];
|
|
|
|
|
|
|
|
if (argc > 1)
|
|
|
|
c = [Connection newRegisteringAtName: [String stringWithCString: argv[1]]
|
|
|
|
withRootObject:s];
|
|
|
|
else
|
|
|
|
c = [Connection newRegisteringAtName: @"secondserver" withRootObject: s];
|
1994-11-04 16:29:24 +00:00
|
|
|
printf("Regsitered server object on localhost with name `secondserver'\n");
|
|
|
|
|
|
|
|
[c setDelegate:s];
|
1996-03-06 14:48:59 +00:00
|
|
|
[NotificationDispatcher
|
|
|
|
addObserver: s
|
|
|
|
selector: @selector(connectionBecameInvalid:)
|
|
|
|
name: ConnectionBecameInvalidNotification
|
|
|
|
object: c];
|
1994-11-04 16:29:24 +00:00
|
|
|
|
|
|
|
[c runConnection];
|
|
|
|
|
|
|
|
exit(0);
|
|
|
|
}
|