]> err.no Git - sope/blob - sope-core/samples/httpu_notify.m
deleted moved files (not sure what went wrong before ...)
[sope] / sope-core / samples / httpu_notify.m
1 /*
2   Copyright (C) 2000-2003 SKYRIX Software AG
3
4   This file is part of OGo
5
6   OGo is free software; you can redistribute it and/or modify it under
7   the terms of the GNU Lesser General Public License as published by the
8   Free Software Foundation; either version 2, or (at your option) any
9   later version.
10
11   OGo is distributed in the hope that it will be useful, but WITHOUT ANY
12   WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14   License for more details.
15
16   You should have received a copy of the GNU Lesser General Public
17   License along with OGo; see the file COPYING.  If not, write to the
18   Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19   02111-1307, USA.
20 */
21 // $Id$
22
23 #import <Foundation/Foundation.h>
24 #include <NGStreams/NGDatagramPacket.h>
25 #include <NGStreams/NGDatagramSocket.h>
26 #include <NGStreams/NGInternetSocketAddress.h>
27 #include <NGStreams/NGInternetSocketDomain.h>
28
29 static void run(void) {
30   NSUserDefaults *ud;
31   NSString *sid;
32   NSURL    *observer;
33   NGDatagramSocket *socket = nil;
34   NGInternetSocketAddress *address;
35   NGDatagramPacket *packet;
36   NSMutableString  *ms;
37   NSData           *data;
38   
39   ud  = [NSUserDefaults standardUserDefaults];
40   sid      = [ud stringForKey:@"sid"];
41   observer = [NSURL URLWithString:[ud stringForKey:@"url"]];
42   
43   if (observer  == nil) {
44     NSLog(@"missing observer ! (use -url to specify one !)");
45     exit(2);
46   }
47   
48   /* construct HTTP-over-UDP request */
49   
50   ms = [NSMutableString stringWithCapacity:16];
51   [ms appendString:@"NOTIFY "];
52   [ms appendString:[observer absoluteString]];
53   [ms appendString:@" HTTP/1.1\r\n"];
54   
55   /* notifications without sid are "teardown's" */
56   if ([sid length] > 0) {
57     [ms appendString:@"Subscription-id: "];
58     [ms appendString:sid];
59     [ms appendString:@"\r\n"];
60   }
61   
62   /* send packet */
63   
64   data    = [ms dataUsingEncoding:NSUTF8StringEncoding];
65   packet  = [NGDatagramPacket packetWithData:data];
66   address = [NGInternetSocketAddress addressWithPort:
67                                        [[observer port] intValue]
68                                      onHost:
69                                        [observer host]];
70   
71   socket = [[NGDatagramSocket alloc] initWithDomain:
72                                        [NGInternetSocketDomain domain]];
73   [packet setReceiver:address];
74   
75   if (![socket sendPacket:packet timeout:3.0]) {
76     NSLog(@"could not send packet %@ on socket %@ !");
77     exit(1);
78   }
79 }
80
81 int main(int argc, char **argv, char **env) {
82   NSAutoreleasePool *pool;
83   
84   pool = [[NSAutoreleasePool alloc] init];
85 #if LIB_FOUNDATION_LIBRARY
86   [NSProcessInfo initializeWithArguments:argv count:argc environment:env];
87 #endif
88   
89   run();
90   
91   exit(0);
92   return 0;
93 }