tcp: Fix 64 bit build with debugging features enabled.
[haiku.git] / src / kits / network / libnetapi / UrlRequest.cpp
blobc1e2042c149a94d84a5dc06c0ea3b7332751779d
1 /*
2 * Copyright 2010 Haiku Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
5 * Authors:
6 * Christophe Huriaux, c.huriaux@gmail.com
7 */
10 #include <UrlRequest.h>
11 #include <Debug.h>
12 #include <stdio.h>
15 static BReference<BUrlContext> gDefaultContext = new(std::nothrow) BUrlContext();
18 BUrlRequest::BUrlRequest(const BUrl& url, BUrlProtocolListener* listener,
19 BUrlContext* context, const char* threadName, const char* protocolName)
21 fUrl(url),
22 fContext(context),
23 fListener(listener),
24 fQuit(false),
25 fRunning(false),
26 fThreadStatus(B_NO_INIT),
27 fThreadId(0),
28 fThreadName(threadName),
29 fProtocol(protocolName)
31 if (fContext == NULL)
32 fContext = gDefaultContext;
36 BUrlRequest::~BUrlRequest()
38 Stop();
42 // #pragma mark URL protocol thread management
45 thread_id
46 BUrlRequest::Run()
48 // Thread already running
49 if (fRunning) {
50 PRINT(("BUrlRequest::Run() : Oops, already running ! "
51 "[urlProtocol=%p]!\n", this));
52 return fThreadId;
55 fThreadId = spawn_thread(BUrlRequest::_ThreadEntry, fThreadName,
56 B_NORMAL_PRIORITY, this);
58 if (fThreadId < B_OK)
59 return fThreadId;
61 fRunning = true;
63 status_t launchErr = resume_thread(fThreadId);
64 if (launchErr < B_OK) {
65 PRINT(("BUrlRequest::Run() : Failed to resume thread %" B_PRId32 "\n",
66 fThreadId));
67 return launchErr;
70 return fThreadId;
74 status_t
75 BUrlRequest::Pause()
77 // TODO
78 return B_ERROR;
82 status_t
83 BUrlRequest::Resume()
85 // TODO
86 return B_ERROR;
90 status_t
91 BUrlRequest::Stop()
93 if (!fRunning)
94 return B_ERROR;
96 fQuit = true;
97 return B_OK;
101 // #pragma mark URL protocol parameters modification
104 status_t
105 BUrlRequest::SetUrl(const BUrl& url)
107 // We should avoid to change URL while the thread is running ...
108 if (IsRunning())
109 return B_ERROR;
111 fUrl = url;
112 return B_OK;
116 status_t
117 BUrlRequest::SetContext(BUrlContext* context)
119 if (IsRunning())
120 return B_ERROR;
122 fContext = context;
123 return B_OK;
127 status_t
128 BUrlRequest::SetListener(BUrlProtocolListener* listener)
130 if (IsRunning())
131 return B_ERROR;
133 fListener = listener;
134 return B_OK;
138 // #pragma mark URL protocol parameters access
141 const BUrl&
142 BUrlRequest::Url() const
144 return fUrl;
148 BUrlContext*
149 BUrlRequest::Context() const
151 return fContext;
155 BUrlProtocolListener*
156 BUrlRequest::Listener() const
158 return fListener;
162 const BString&
163 BUrlRequest::Protocol() const
165 return fProtocol;
169 // #pragma mark URL protocol informations
172 bool
173 BUrlRequest::IsRunning() const
175 return fRunning;
179 status_t
180 BUrlRequest::Status() const
182 return fThreadStatus;
186 // #pragma mark Thread management
189 /*static*/ int32
190 BUrlRequest::_ThreadEntry(void* arg)
192 BUrlRequest* request = reinterpret_cast<BUrlRequest*>(arg);
193 request->fThreadStatus = B_BUSY;
194 request->_ProtocolSetup();
196 status_t protocolLoopExitStatus = request->_ProtocolLoop();
198 request->fRunning = false;
199 request->fThreadStatus = protocolLoopExitStatus;
201 if (request->fListener != NULL) {
202 request->fListener->RequestCompleted(request,
203 protocolLoopExitStatus == B_OK);
206 return B_OK;
210 void
211 BUrlRequest::_EmitDebug(BUrlProtocolDebugMessage type,
212 const char* format, ...)
214 if (fListener == NULL)
215 return;
217 va_list arguments;
218 va_start(arguments, format);
220 char debugMsg[1024];
221 vsnprintf(debugMsg, sizeof(debugMsg), format, arguments);
222 fListener->DebugMessage(this, type, debugMsg);
223 va_end(arguments);