1994-11-04 16:29:24 +00:00
|
|
|
#include "second-server.h"
|
|
|
|
#include "second-client.h"
|
1996-04-17 18:40:03 +00:00
|
|
|
#include <gnustep/base/Connection.h>
|
|
|
|
#include <gnustep/base/TcpPort.h>
|
|
|
|
#include <gnustep/base/String.h>
|
|
|
|
#include <gnustep/base/Notification.h>
|
|
|
|
#include <gnustep/base/Invocation.h>
|
1996-03-07 18:15:25 +00:00
|
|
|
|
1996-07-15 18:41:44 +00:00
|
|
|
/* This function will be called by an Invocation object that will be
|
|
|
|
registered to fire every time an InPort accepts a new client. */
|
1996-03-07 18:15:25 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
1996-07-15 18:41:44 +00:00
|
|
|
/* This function will be called by an Invocation object that will be
|
|
|
|
registered to fire every time an InPort client disconnects. */
|
1996-03-07 18:15:25 +00:00
|
|
|
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
|
|
|
|
1996-07-15 18:41:44 +00:00
|
|
|
/* The implementation of the object that will be registered with D.O.
|
|
|
|
as the server. */
|
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);
|
|
|
|
|
|
|
|
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-19 01:00:17 +00:00
|
|
|
id connection = [notification object];
|
|
|
|
if ([connection isKindOf: [Connection class]])
|
1994-11-04 16:29:24 +00:00
|
|
|
{
|
|
|
|
int arrayCount = [array count];
|
1996-03-19 01:00:17 +00:00
|
|
|
int i;
|
1994-11-04 16:29:24 +00:00
|
|
|
|
1996-03-19 01:00:17 +00:00
|
|
|
printf(">>> Connection 0x%x invalidated\n", (unsigned)connection);
|
1994-11-04 16:29:24 +00:00
|
|
|
|
1996-03-19 01:00:17 +00:00
|
|
|
/* Remember to avoid calling -isEqual: on the proxies of the
|
|
|
|
invalidated Connection. */
|
|
|
|
for (i = arrayCount-1; i >= 0; i--)
|
|
|
|
{
|
|
|
|
id o = [array objectAtIndex: i];
|
|
|
|
if ([o isProxy]
|
|
|
|
&& [o connectionForProxy] == connection)
|
1994-11-04 16:29:24 +00:00
|
|
|
{
|
1996-03-19 01:00:17 +00:00
|
|
|
printf(">>> Removing proxy 0x%x\n", (unsigned)o);
|
|
|
|
[array removeObjectAtIndex: i];
|
1994-11-04 16:29:24 +00:00
|
|
|
}
|
1996-03-19 01:00:17 +00:00
|
|
|
}
|
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;
|
|
|
|
|
1996-03-18 21:52:41 +00:00
|
|
|
[NSObject enableDoubleReleaseCheck: YES];
|
|
|
|
|
1994-11-04 16:29:24 +00:00
|
|
|
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);
|
|
|
|
}
|