]> git.saurik.com Git - apple/security.git/blob - Network/netmanager.h
Security-179.tar.gz
[apple/security.git] / Network / netmanager.h
1 /*
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
3 *
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
8 * using this file.
9 *
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
16 */
17
18
19 //
20 // manager - network protocol core manager class
21 //
22 #ifndef _H_NETMANAGER
23 #define _H_NETMANAGER
24
25 #include <Security/ip++.h>
26 #include <Security/timeflow.h>
27 #include <Security/tqueue.h>
28 #include "xfercore.h"
29 #include "connectionpool.h"
30 #include "target.h"
31 #include "parameters.h"
32 #include "observer.h"
33 #include <set>
34 #include <map>
35
36
37 using namespace IPPlusPlus;
38
39
40 namespace Security {
41 namespace Network {
42
43
44 class Protocol;
45 class Transfer;
46 class Connection;
47
48
49 //
50 // A Manager object represents the top-level operations controller.
51 // You would usually only have one per process, though you *can*
52 // have more than one - they would not interact at all, and each
53 // Protocol, Transfer, etc. object could only belong to one of them.
54 //
55 class Manager : public ParameterPointer {
56 public:
57 Manager();
58 virtual ~Manager();
59
60 public:
61 void add(Transfer *xfer);
62 void remove(Transfer *xfer);
63 void start(Transfer *xfer);
64 void abort(Transfer *xfer);
65
66 Observer *observer() const { return mObserver; }
67 void observer(Observer *ob) { mObserver = ob; }
68
69 public: // meant for just Transfer and Connection
70 void done(Transfer *xfer);
71
72 void addIO(TransferEngine::Client *client);
73 void removeIO(TransferEngine::Client *client);
74
75 public: // meant just for Connection
76 template <class ProtoConnection>
77 ProtoConnection *findConnection(const HostTarget &host)
78 { return safe_cast<ProtoConnection *>(pickConnection(host)); }
79
80 void retainConnection(Connection *connection);
81 void closeConnection(Connection *connection);
82
83 public:
84 void step(); // one small step for URLkind...
85 void run(); // run until no more work
86 void run(Time::Absolute upTo); // run until some future time
87
88 public:
89 bool reuseConnections() const { return mRetainConnections; }
90 void reuseConnections(bool retain); // global connection reuse override
91 void flushConnections(); // clear connection cache (expensive)
92
93 public:
94 class Timer : private ScheduleQueue<Time::Absolute>::Event {
95 friend class Manager;
96 protected:
97 virtual ~Timer() { }
98
99 Time::Absolute when() const { return Event::when(); }
100 bool scheduled() const { return Event::scheduled(); }
101
102 public:
103 virtual void action() = 0;
104 };
105
106 virtual void setTimer(Timer *timer, Time::Absolute when);
107 void setTimer(Timer *timer, Time::Interval offset)
108 { setTimer(timer, Time::now() + offset); }
109
110 virtual void clearTimer(Timer *timer);
111
112 protected:
113 virtual void runTimers(); // run ready timers
114 virtual void prepare(); // setup for engine
115
116 private:
117 void doStep(); // internal operative step
118 Connection *pickConnection(const HostTarget &host);
119
120 private:
121 typedef map<string, Protocol *> ProtoMap;
122 ProtoMap mProtocols; // map of registered protocols
123
124 private:
125 typedef set<Transfer *> TransferSet;
126 TransferSet mTransfers; // set of active transfers (prelim)
127 uint32 mActiveTransfers; // number of active transfers
128
129 private:
130 TransferEngine mEngine; // transfer core engine
131 ConnectionPool mConnections; // pool of retained (live) Connections
132 set<Connection *> mMorgue; // Connections we should destroy
133 bool mRetainConnections; // global connection-reuse enable
134 Observer *mObserver; // default observer (NULL if none)
135
136 ScheduleQueue<Time::Absolute> mTimers; // timer queue
137 };
138
139
140 } // end namespace Network
141 } // end namespace Security
142
143
144 #endif /* _H_NETMANAGER */