jens@0
|
1 |
//
|
jens@0
|
2 |
// BLIPConnection.h
|
jens@0
|
3 |
// MYNetwork
|
jens@0
|
4 |
//
|
jens@0
|
5 |
// Created by Jens Alfke on 5/10/08.
|
jens@0
|
6 |
// Copyright 2008 Jens Alfke. All rights reserved.
|
jens@0
|
7 |
//
|
jens@0
|
8 |
|
jens@0
|
9 |
#import "TCPConnection.h"
|
jens@0
|
10 |
#import "TCPListener.h"
|
jens@0
|
11 |
@class BLIPRequest, BLIPResponse, BLIPDispatcher;
|
jens@0
|
12 |
@protocol BLIPConnectionDelegate;
|
jens@0
|
13 |
|
jens@0
|
14 |
|
jens@0
|
15 |
/** Represents a connection to a peer, using the BLIP protocol over a TCP socket.
|
jens@0
|
16 |
Outgoing connections are made simply by instantiating a BLIPConnection via -initToAddress:.
|
jens@0
|
17 |
Incoming connections are usually set up by a BLIPListener and passed to the listener's
|
jens@0
|
18 |
delegate.
|
jens@0
|
19 |
Most of the API is inherited from TCPConnection. */
|
jens@0
|
20 |
@interface BLIPConnection : TCPConnection
|
jens@0
|
21 |
{
|
jens@0
|
22 |
BLIPDispatcher *_dispatcher;
|
jens@0
|
23 |
}
|
jens@0
|
24 |
|
jens@0
|
25 |
/** The delegate object that will be called when the connection opens, closes or receives messages. */
|
jens@0
|
26 |
@property (assign) id<BLIPConnectionDelegate> delegate;
|
jens@0
|
27 |
|
jens@0
|
28 |
@property (readonly) BLIPDispatcher *dispatcher;
|
jens@0
|
29 |
|
jens@0
|
30 |
/** Creates an outgoing request, with no properties.
|
jens@0
|
31 |
The body may be nil.
|
jens@0
|
32 |
To send it, call -send. */
|
jens@0
|
33 |
- (BLIPRequest*) requestWithBody: (NSData*)body;
|
jens@0
|
34 |
|
jens@0
|
35 |
/** Creates an outgoing request.
|
jens@0
|
36 |
The body or properties may be nil.
|
jens@0
|
37 |
To send it, call -send. */
|
jens@0
|
38 |
- (BLIPRequest*) requestWithBody: (NSData*)body
|
jens@0
|
39 |
properties: (NSDictionary*)properies;
|
jens@0
|
40 |
|
jens@0
|
41 |
/** Sends a request over this connection.
|
jens@0
|
42 |
(Actually, it queues it to be sent; this method always returns immediately.)
|
jens@0
|
43 |
Call this instead of calling -send on the request itself, if the request was created with
|
jens@0
|
44 |
+[BLIPRequest requestWithBody:] and hasn't yet been assigned to any connection.
|
jens@0
|
45 |
This method will assign it to this connection before sending it.
|
jens@0
|
46 |
The request's matching response object will be returned, or nil if the request couldn't be sent. */
|
jens@0
|
47 |
- (BLIPResponse*) sendRequest: (BLIPRequest*)request;
|
jens@0
|
48 |
@end
|
jens@0
|
49 |
|
jens@0
|
50 |
|
jens@0
|
51 |
|
jens@0
|
52 |
/** The delegate messages that BLIPConnection will send,
|
jens@0
|
53 |
in addition to the ones inherited from TCPConnectionDelegate. */
|
jens@0
|
54 |
@protocol BLIPConnectionDelegate <TCPConnectionDelegate>
|
jens@0
|
55 |
|
jens@0
|
56 |
/** Called when a BLIPRequest is received from the peer, if there is no BLIPDispatcher
|
jens@0
|
57 |
rule to handle it.
|
jens@0
|
58 |
The delegate should get the request's response object, fill in its data and properties
|
jens@0
|
59 |
or error property, and send it.
|
jens@0
|
60 |
If it doesn't explicitly send a response, a default empty one will be sent;
|
jens@0
|
61 |
to prevent this, call -deferResponse on the request if you want to send a response later. */
|
jens@0
|
62 |
- (void) connection: (BLIPConnection*)connection receivedRequest: (BLIPRequest*)request;
|
jens@0
|
63 |
|
jens@0
|
64 |
@optional
|
jens@0
|
65 |
/** Called when a BLIPResponse (to one of your requests) is received from the peer.
|
jens@0
|
66 |
This is called <i>after</i> the response object's onComplete target, if any, is invoked. */
|
jens@0
|
67 |
- (void) connection: (BLIPConnection*)connection receivedResponse: (BLIPResponse*)response;
|
jens@0
|
68 |
@end
|
jens@0
|
69 |
|
jens@0
|
70 |
|
jens@0
|
71 |
|
jens@0
|
72 |
|
jens@0
|
73 |
/** A "server" that listens on a TCP socket for incoming BLIP connections and creates
|
jens@0
|
74 |
BLIPConnection instances to handle them.
|
jens@0
|
75 |
Most of the API is inherited from TCPListener. */
|
jens@0
|
76 |
@interface BLIPListener : TCPListener
|
jens@0
|
77 |
{
|
jens@0
|
78 |
BLIPDispatcher *_dispatcher;
|
jens@0
|
79 |
}
|
jens@0
|
80 |
|
jens@0
|
81 |
@property (readonly) BLIPDispatcher *dispatcher;
|
jens@0
|
82 |
|
jens@0
|
83 |
@end
|