]> err.no Git - sope/blob - sope-appserver/NGObjWeb/WebDAV/SoSubscription.m
minor code cleanups
[sope] / sope-appserver / NGObjWeb / WebDAV / SoSubscription.m
1 /*
2   Copyright (C) 2002-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 #include "SoSubscription.h"
23 #include <NGStreams/NGDatagramPacket.h>
24 #include <NGStreams/NGDatagramSocket.h>
25 #include <NGStreams/NGInternetSocketAddress.h>
26 #include <NGStreams/NGInternetSocketDomain.h>
27 #include "common.h"
28
29 @implementation SoSubscription
30
31 static BOOL debugOn = YES;
32
33 - (id)initWithID:(NSString *)_sid
34   url:(NSURL *)_url observer:(NSURL *)_callback
35   type:(NSString *)_type delay:(NSTimeInterval)_delay
36   lifetime:(NSTimeInterval)_lifetime
37 {
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;
44     self->delay    = _delay;
45     
46     self->expireDate = [[NSDate alloc] initWithTimeIntervalSinceNow:_lifetime];
47   }
48   return self;
49 }
50 - (id)init {
51   [self release];
52   return nil;
53 }
54
55 - (void)dealloc {
56   [self->sid        release];
57   [self->object     release];
58   [self->observer   release];
59   [self->expireDate release];
60   [self->subscriptionType release];
61   [super dealloc];
62 }
63
64 /* accessors */
65
66 - (NSDate *)expirationDate {
67   return self->expireDate;
68 }
69 - (NSString *)subscriptionID {
70   return self->sid;
71 }
72
73 - (BOOL)hasEventsPending {
74   return self->pending > 0 ? YES : NO;
75 }
76
77 /* operations */
78
79 - (void)resetEvents {
80   self->pending = 0;
81 }
82
83 - (BOOL)isValidForURL:(NSURL *)_url {
84   return YES;
85 }
86
87 - (BOOL)isExpired {
88   NSDate *now;
89   
90   if (self->expireDate == nil) 
91     return NO;
92   
93   now = [NSDate date];
94   return [now timeIntervalSince1970] > [self->expireDate timeIntervalSince1970]
95     ? YES : NO;
96 }
97
98 - (BOOL)renewSubscription {
99   [self->expireDate release]; 
100   self->expireDate = nil;
101   
102   self->expireDate =
103     [[NSDate alloc] initWithTimeIntervalSinceNow:self->lifetime];
104   return YES;
105 }
106
107 /* send-notify */
108
109 - (BOOL)sendNotification {
110   static NGDatagramSocket *socket = nil;
111   NGInternetSocketAddress *address;
112   NGDatagramPacket *packet;
113   NSMutableString  *ms;
114   NSData           *data;
115   
116   [self debugWithFormat:@"sending notification ..."];
117   
118   /* construct HTTP-over-UDP request */
119   
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"];
127   
128   /* send packet */
129   
130   data    = [ms dataUsingEncoding:NSUTF8StringEncoding];
131   packet  = (NGDatagramPacket *)[NGDatagramPacket packetWithData:data];
132   address = [NGInternetSocketAddress addressWithPort:
133                                        [[self->observer port] intValue]
134                                      onHost:
135                                        [self->observer host]];
136   
137   if (socket == nil) {
138     socket = [[NGDatagramSocket alloc] initWithDomain:
139                                          [NGInternetSocketDomain domain]];
140   }
141   [packet setReceiver:address];
142   
143   self->pending++;
144   
145   return [socket sendPacket:packet timeout:3.0];
146 }
147
148 /* debugging */
149
150 - (BOOL)isDebuggingEnabled {
151   return debugOn;
152 }
153
154 @end /* SoSubscription */