]> err.no Git - sope/blob - sope-core/samples/httpu_notify.m
started ngcal tool
[sope] / sope-core / samples / httpu_notify.m
1 /*
2   Copyright (C) 2000-2005 SKYRIX Software AG
3
4   This file is part of SOPE.
5
6   SOPE 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   SOPE 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 SOPE; 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
22 #import <Foundation/Foundation.h>
23 #include <NGStreams/NGDatagramPacket.h>
24 #include <NGStreams/NGDatagramSocket.h>
25 #include <NGStreams/NGInternetSocketAddress.h>
26 #include <NGStreams/NGInternetSocketDomain.h>
27
28 static void run(void) {
29   NSUserDefaults *ud;
30   NSString *sid;
31   NSURL    *observer;
32   NGDatagramSocket *socket = nil;
33   NGInternetSocketAddress *address;
34   NGDatagramPacket *packet;
35   NSMutableString  *ms;
36   NSData           *data;
37   
38   ud  = [NSUserDefaults standardUserDefaults];
39   sid      = [ud stringForKey:@"sid"];
40   observer = [NSURL URLWithString:[ud stringForKey:@"url"]];
41   
42   if (observer  == nil) {
43     NSLog(@"missing observer ! (use -url to specify one !)");
44     exit(2);
45   }
46   
47   /* construct HTTP-over-UDP request */
48   
49   ms = [NSMutableString stringWithCapacity:16];
50   [ms appendString:@"NOTIFY "];
51   [ms appendString:[observer absoluteString]];
52   [ms appendString:@" HTTP/1.1\r\n"];
53   
54   /* notifications without sid are "teardown's" */
55   if ([sid length] > 0) {
56     [ms appendString:@"Subscription-id: "];
57     [ms appendString:sid];
58     [ms appendString:@"\r\n"];
59   }
60   
61   /* send packet */
62   
63   data    = [ms dataUsingEncoding:NSUTF8StringEncoding];
64   packet  = [NGDatagramPacket packetWithData:data];
65   address = [NGInternetSocketAddress addressWithPort:
66                                        [[observer port] intValue]
67                                      onHost:
68                                        [observer host]];
69   
70   socket = [[NGDatagramSocket alloc] initWithDomain:
71                                        [NGInternetSocketDomain domain]];
72   [packet setReceiver:address];
73   
74   if (![socket sendPacket:packet timeout:3.0]) {
75     NSLog(@"could not send packet %@ on socket %@ !");
76     exit(1);
77   }
78 }
79
80 int main(int argc, char **argv, char **env) {
81   NSAutoreleasePool *pool;
82   
83   pool = [[NSAutoreleasePool alloc] init];
84 #if LIB_FOUNDATION_LIBRARY
85   [NSProcessInfo initializeWithArguments:argv count:argc environment:env];
86 #endif
87   
88   run();
89   
90   exit(0);
91   return 0;
92 }