]> git.saurik.com Git - apple/security.git/blob - Network/transfer.cpp
Security-54.tar.gz
[apple/security.git] / Network / transfer.cpp
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 // transfer - the embodiment of a single transfer transaction
21 //
22 #include "transfer.h"
23 #include "netmanager.h"
24 #include "netconnection.h"
25 #include "protocol.h"
26 #include "neterror.h"
27
28
29 namespace Security {
30 namespace Network {
31
32
33 //
34 // Construct and destroy Transfer objects
35 //
36 Transfer::Transfer(Protocol &proto, const Target &tgt, Operation op, IPPort defPort)
37 : protocol(proto),
38 target(tgt.host.defaultPort(defPort), tgt.path),
39 mState(cold), mOperation(op), mConnection(NULL),
40 mSource(NULL), mSink(NULL),
41 mShareConnections(proto.manager.reuseConnections()),
42 mErrorStatus(defaultOSStatusError)
43 {
44 debug("netxfer", "%p created for protocol %p(%s) target %s operation %d",
45 this, &proto, proto.name(), target.urlForm().c_str(), mOperation);
46
47 parameters(protocol.manager); // inherit environment from manager object
48 mObserver = protocol.manager.observer();
49 }
50
51 Transfer::~Transfer()
52 {
53 debug("netxfer", "transfer %p destroyed", this);
54 }
55
56
57 //
58 // Generic error management.
59 // These defaults do (almost) nothing useful; they should be overridden by
60 // each Protocol's Transfer object.
61 //
62 Transfer::ResultClass Transfer::resultClass() const
63 {
64 switch (state()) {
65 case failed:
66 return unclassifiedFailure;
67 case finished:
68 return success;
69 default:
70 Error::throwMe();
71 }
72 }
73
74 OSStatus Transfer::errorStatus() const
75 {
76 assert(state() == failed);
77 return mErrorStatus;
78 }
79
80 string Transfer::errorDescription() const
81 {
82 assert(state() == failed);
83 return mErrorDescription;
84 }
85
86
87 //
88 // Restart trampoline
89 //
90 void Transfer::restart()
91 {
92 assert(mConnection);
93 return mConnection->restart();
94 }
95
96
97 //
98 // Notify any observer
99 //
100 void Transfer::observe(Observer::Events events, const void *info)
101 {
102 if (mObserver && mObserver->wants(events))
103 mObserver->observe(events, this, info);
104 }
105
106
107 //
108 // Set yourself to be successfully done
109 //
110 void Transfer::finish()
111 {
112 debug("xferengine", "transfer %p is finishing up", this);
113 mState = finished;
114 if (isDocked())
115 mConnection->undock();
116 protocol.manager.done(this);
117 observe(Observer::transferComplete);
118 }
119
120
121 //
122 // Set yourself to have failed
123 //
124 void Transfer::fail()
125 {
126 debug("xferengine", "transfer %p is failing", this);
127 mState = failed;
128 if (isDocked())
129 mConnection->undock();
130 protocol.manager.done(this);
131 observe(Observer::transferFailed);
132 }
133
134
135 //
136 // This default implementation of abort() simply fails.
137 // This is not likely to be enough for most protocols.
138 //
139 void Transfer::abort()
140 {
141 observe(Observer::aborting);
142 if (isDocked())
143 mConnection->retain(false); // indeterminate state; don't keep it
144 fail();
145 }
146
147
148 } // end namespace Network
149 } // end namespace Security