1 /*
2 * Copyright 2010 Haiku Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
4 *
5 * Authors:
6 * Christophe Huriaux, c.huriaux@gmail.com
7 */
8
9 #include <cstdio>
10
11 #include <UrlSynchronousRequest.h>
12
13 using namespace BPrivate::Network;
14
15
16 #define PRINT(x) printf x;
17
18
BUrlSynchronousRequest(BUrlRequest & request)19 BUrlSynchronousRequest::BUrlSynchronousRequest(BUrlRequest& request)
20 :
21 BUrlRequest(request.Url(), request.Output(), NULL, request.Context(),
22 "BUrlSynchronousRequest", request.Protocol()),
23 fRequestComplete(false),
24 fWrappedRequest(request)
25 {
26 }
27
28
29 status_t
Perform()30 BUrlSynchronousRequest::Perform()
31 {
32 fWrappedRequest.SetListener(this);
33 fRequestComplete = false;
34
35 thread_id worker = fWrappedRequest.Run();
36 // TODO something to do with the thread_id maybe ?
37
38 if (worker < B_OK)
39 return worker;
40 else
41 return B_OK;
42 }
43
44
45 status_t
WaitUntilCompletion()46 BUrlSynchronousRequest::WaitUntilCompletion()
47 {
48 while (!fRequestComplete)
49 snooze(10000);
50
51 return B_OK;
52 }
53
54
55 void
ConnectionOpened(BUrlRequest *)56 BUrlSynchronousRequest::ConnectionOpened(BUrlRequest*)
57 {
58 PRINT(("SynchronousRequest::ConnectionOpened()\n"));
59 }
60
61
62 void
HostnameResolved(BUrlRequest *,const char * ip)63 BUrlSynchronousRequest::HostnameResolved(BUrlRequest*, const char* ip)
64 {
65 PRINT(("SynchronousRequest::HostnameResolved(%s)\n", ip));
66 }
67
68
69 void
ResponseStarted(BUrlRequest *)70 BUrlSynchronousRequest::ResponseStarted(BUrlRequest*)
71 {
72 PRINT(("SynchronousRequest::ResponseStarted()\n"));
73 }
74
75
76 void
HeadersReceived(BUrlRequest *)77 BUrlSynchronousRequest::HeadersReceived(BUrlRequest*)
78 {
79 PRINT(("SynchronousRequest::HeadersReceived()\n"));
80 }
81
82
83 void
BytesWritten(BUrlRequest * caller,size_t bytesWritten)84 BUrlSynchronousRequest::BytesWritten(BUrlRequest* caller, size_t bytesWritten)
85 {
86 PRINT(("SynchronousRequest::BytesWritten(%" B_PRIdSSIZE ")\n",
87 bytesWritten));
88 }
89
90
91 void
DownloadProgress(BUrlRequest *,off_t bytesReceived,off_t bytesTotal)92 BUrlSynchronousRequest::DownloadProgress(BUrlRequest*,
93 off_t bytesReceived, off_t bytesTotal)
94 {
95 PRINT(("SynchronousRequest::DownloadProgress(%" B_PRIdOFF ", %" B_PRIdOFF
96 ")\n", bytesReceived, bytesTotal));
97 }
98
99
100 void
UploadProgress(BUrlRequest *,off_t bytesSent,off_t bytesTotal)101 BUrlSynchronousRequest::UploadProgress(BUrlRequest*, off_t bytesSent,
102 off_t bytesTotal)
103 {
104 PRINT(("SynchronousRequest::UploadProgress(%" B_PRIdOFF ", %" B_PRIdOFF
105 ")\n", bytesSent, bytesTotal));
106 }
107
108
109 void
RequestCompleted(BUrlRequest * caller,bool success)110 BUrlSynchronousRequest::RequestCompleted(BUrlRequest* caller, bool success)
111 {
112 PRINT(("SynchronousRequest::RequestCompleted(%s) : %s\n", (success?"true":"false"),
113 strerror(caller->Status())));
114 fRequestComplete = true;
115 }
116