1996-03-01 16:26:49 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <objects/TcpPort.h>
|
1996-03-06 14:45:09 +00:00
|
|
|
#include <objects/Notification.h>
|
|
|
|
#include <objects/Invocation.h>
|
1996-03-12 19:20:07 +00:00
|
|
|
#include <objects/RunLoop.h>
|
1996-03-01 16:26:49 +00:00
|
|
|
|
1996-03-06 14:45:09 +00:00
|
|
|
id announce_new_connection (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_connection (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;
|
|
|
|
}
|
|
|
|
|
1996-03-12 19:20:07 +00:00
|
|
|
static id port = nil;
|
|
|
|
|
|
|
|
id handle_incoming_packet (id packet)
|
1996-03-01 16:26:49 +00:00
|
|
|
{
|
1996-03-12 19:20:07 +00:00
|
|
|
static unsigned message_count = 0;
|
1996-03-06 14:45:09 +00:00
|
|
|
id reply_port;
|
|
|
|
|
1996-03-12 19:20:07 +00:00
|
|
|
message_count++;
|
|
|
|
fprintf (stdout, "received >");
|
|
|
|
fwrite ([packet streamBuffer] + [packet streamBufferPrefix],
|
|
|
|
[packet streamEofPosition], 1, stdout);
|
|
|
|
fprintf (stdout, "<\n");
|
|
|
|
reply_port = [packet replyPort];
|
|
|
|
[packet release];
|
|
|
|
|
|
|
|
packet = [[TcpPacket alloc] initForSendingWithCapacity: 100
|
|
|
|
replyPort: port];
|
|
|
|
[packet writeFormat: @"Your's was my message number %d",
|
|
|
|
message_count];
|
|
|
|
[reply_port sendPacket: packet withTimeout: 20 * 1000];
|
|
|
|
[packet release];
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main (int argc, char *argv[])
|
|
|
|
{
|
1996-03-06 14:45:09 +00:00
|
|
|
if (argc > 1)
|
|
|
|
port = [TcpInPort newForReceivingFromRegisteredName:
|
|
|
|
[NSString stringWithCString: argv[1]]];
|
|
|
|
else
|
|
|
|
port = [TcpInPort newForReceivingFromRegisteredName: @"tcpport-test"];
|
|
|
|
|
|
|
|
[NotificationDispatcher
|
1996-03-07 00:36:15 +00:00
|
|
|
addInvocation: [[ObjectFunctionInvocation alloc]
|
|
|
|
initWithObjectFunction: announce_broken_connection]
|
1996-03-06 14:45:09 +00:00
|
|
|
name: InPortClientBecameInvalidNotification
|
|
|
|
object: port];
|
|
|
|
[NotificationDispatcher
|
1996-03-07 00:36:15 +00:00
|
|
|
addInvocation: [[ObjectFunctionInvocation alloc]
|
|
|
|
initWithObjectFunction: announce_new_connection]
|
1996-03-06 14:45:09 +00:00
|
|
|
name: InPortAcceptedClientNotification
|
|
|
|
object: port];
|
1996-03-01 16:26:49 +00:00
|
|
|
|
1996-03-06 14:45:09 +00:00
|
|
|
printf ("Waiting for connections.\n");
|
|
|
|
|
1996-03-12 19:20:07 +00:00
|
|
|
#if 1
|
|
|
|
[port setPacketInvocation:
|
|
|
|
[[[ObjectFunctionInvocation alloc]
|
|
|
|
initWithObjectFunction: handle_incoming_packet]
|
|
|
|
autorelease]];
|
|
|
|
[port addToRunLoop: [RunLoop currentInstance] forMode: nil];
|
|
|
|
[[RunLoop currentInstance] run];
|
|
|
|
#else
|
|
|
|
{
|
|
|
|
id packet;
|
|
|
|
unsigned message_count = 0;
|
|
|
|
id reply_port;
|
|
|
|
|
|
|
|
while ((packet = [port receivePacketWithTimeout: -1]))
|
|
|
|
{
|
|
|
|
message_count++;
|
|
|
|
fprintf (stdout, "received >");
|
|
|
|
fwrite ([packet streamBuffer] + [packet streamBufferPrefix],
|
|
|
|
[packet streamEofPosition], 1, stdout);
|
|
|
|
fprintf (stdout, "<\n");
|
|
|
|
reply_port = [packet replyPort];
|
|
|
|
[packet release];
|
|
|
|
|
|
|
|
packet = [[TcpPacket alloc] initForSendingWithCapacity: 100
|
|
|
|
replyPort: port];
|
|
|
|
[packet writeFormat: @"Your's was my message number %d",
|
|
|
|
message_count];
|
|
|
|
[reply_port sendPacket: packet withTimeout: 20 * 1000];
|
|
|
|
[packet release];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
1996-03-01 16:26:49 +00:00
|
|
|
fprintf (stdout, "Timed out. Exiting.\n");
|
|
|
|
|
|
|
|
exit (0);
|
|
|
|
}
|