libs-base/Testing/tcpport-server.m

129 lines
3.5 KiB
Mathematica
Raw Normal View History

/* Test/example program for the base library
Copyright (C) 2005 Free Software Foundation, Inc.
Copying and distribution of this file, with or without modification,
are permitted in any medium without royalty provided the copyright
notice and this notice are preserved.
This file is part of the GNUstep Base Library.
*/
#include <stdio.h>
#include <GNUstepBase/TcpPort.h>
#include <GNUstepBase/Invocation.h>
#include <Foundation/NSNotification.h>
#include <Foundation/NSRunLoop.h>
@interface Dummy: NSObject
+ (id) announce_new_connection: notification;
+ (id) announce_broken_connection: notification;
@end
@implementation Dummy
+ announce_new_connection: notification
{
id in_port = [notification object];
id out_port = [notification userInfo];
NSLog (@"{%@}\n\tconnected to\n\t{%@}\n",
[out_port description], [in_port description]);
NSLog (@"Now servicing %d connection(s).\n",
[in_port numberOfConnectedOutPorts]);
return nil;
}
+ announce_broken_connection: notification
{
id in_port = [notification object];
id out_port = [notification userInfo];
NSLog (@"{%@}\n\tdisconnected from\n\t{%@}\n",
[out_port description], [in_port description]);
NSLog (@"Now servicing %d connection(s).\n",
[in_port numberOfConnectedOutPorts]);
return nil;
}
@end
static id port = nil;
id handle_incoming_packet (TcpInPacket *packet)
{
static unsigned message_count = 0;
id reply_port;
message_count++;
fprintf (stdout, "received >");
fwrite ([packet streamBuffer] + [packet streamBufferPrefix],
[packet streamEofPosition], 1, stdout);
fprintf (stdout, "<\n");
reply_port = [packet replyOutPort];
[packet release];
packet = [[TcpOutPacket alloc] initForSendingWithCapacity: 100
replyInPort: port];
[packet writeFormat: @"Your's was my message number %d",
message_count];
[reply_port sendPacket: packet timeout:10.0];
[packet release];
return nil;
}
int main (int argc, char *argv[])
{
if (argc > 1)
port = [TcpInPort newForReceivingFromRegisteredName:
[NSString stringWithCString: argv[1]]];
else
port = [TcpInPort newForReceivingFromRegisteredName: @"tcpport-test"];
[[NSNotificationCenter defaultCenter]
addObserver: [Dummy class]
selector: @selector(announce_broken_connection:)
name: InPortClientBecameInvalidNotification
object: port];
[[NSNotificationCenter defaultCenter]
addObserver: [Dummy class]
selector: @selector(announce_new_connection:)
name: InPortAcceptedClientNotification
object: port];
printf ("Waiting for connections.\n");
#if 1
[port setReceivedPacketInvocation:
[[[ObjectFunctionInvocation alloc]
initWithObjectFunction: handle_incoming_packet]
autorelease]];
[[NSRunLoop currentRunLoop] addPort: port
forMode: NSDefaultRunLoopMode];
[[NSRunLoop currentRunLoop] 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 timeout: 20.0];
[packet release];
}
}
#endif
fprintf (stdout, "Timed out. Exiting.\n");
exit (0);
}