2 Copyright (C) 2002-2005 SKYRIX Software AG
4 This file is part of SOPE.
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
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.
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
22 #include "SoSubscription.h"
23 #include <NGStreams/NGDatagramPacket.h>
24 #include <NGStreams/NGDatagramSocket.h>
25 #include <NGStreams/NGInternetSocketAddress.h>
26 #include <NGStreams/NGInternetSocketDomain.h>
29 @implementation SoSubscription
31 static BOOL debugOn = YES;
33 - (id)initWithID:(NSString *)_sid
34 url:(NSURL *)_url observer:(NSURL *)_callback
35 type:(NSString *)_type delay:(NSTimeInterval)_delay
36 lifetime:(NSTimeInterval)_lifetime
38 if ((self = [super init])) {
39 self->subscriptionType = [_type copy];
40 self->sid = [_sid copy];
41 self->object = [_url retain];
42 self->observer = [_callback retain];
43 self->lifetime = _lifetime;
46 self->expireDate = [[NSDate alloc] initWithTimeIntervalSinceNow:_lifetime];
57 [self->object release];
58 [self->observer release];
59 [self->expireDate release];
60 [self->subscriptionType release];
66 - (NSDate *)expirationDate {
67 return self->expireDate;
69 - (NSString *)subscriptionID {
73 - (BOOL)hasEventsPending {
74 return self->pending > 0 ? YES : NO;
83 - (BOOL)isValidForURL:(NSURL *)_url {
90 if (self->expireDate == nil)
94 return [now timeIntervalSince1970] > [self->expireDate timeIntervalSince1970]
98 - (BOOL)renewSubscription {
99 [self->expireDate release];
100 self->expireDate = nil;
103 [[NSDate alloc] initWithTimeIntervalSinceNow:self->lifetime];
109 - (BOOL)sendNotification {
110 static NGDatagramSocket *socket = nil;
111 NGInternetSocketAddress *address;
112 NGDatagramPacket *packet;
116 [self debugWithFormat:@"sending notification ..."];
118 /* construct HTTP-over-UDP request */
120 ms = [NSMutableString stringWithCapacity:16];
121 [ms appendString:@"NOTIFY "];
122 [ms appendString:[self->observer absoluteString]];
123 [ms appendString:@" HTTP/1.1\r\n"];
124 [ms appendString:@"Subscription-id: "];
125 [ms appendString:self->sid];
126 [ms appendString:@"\r\n"];
130 data = [ms dataUsingEncoding:NSUTF8StringEncoding];
131 packet = (NGDatagramPacket *)[NGDatagramPacket packetWithData:data];
132 address = [NGInternetSocketAddress addressWithPort:
133 [[self->observer port] intValue]
135 [self->observer host]];
138 socket = [[NGDatagramSocket alloc] initWithDomain:
139 [NGInternetSocketDomain domain]];
141 [packet setReceiver:address];
145 return [socket sendPacket:packet timeout:3.0];
150 - (BOOL)isDebuggingEnabled {
154 @end /* SoSubscription */