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 #define PRINT(x) printf x; 14 15 #ifndef LIBNETAPI_DEPRECATED 16 using namespace BPrivate::Network; 17 #endif 18 19 #ifdef LIBNETAPI_DEPRECATED 20 BUrlSynchronousRequest::BUrlSynchronousRequest(BUrlRequest& request) 21 : 22 BUrlRequest(request.Url(), NULL, request.Context(), 23 "BUrlSynchronousRequest", request.Protocol()), 24 fRequestComplete(false), 25 fWrappedRequest(request) 26 { 27 } 28 29 #else 30 31 BUrlSynchronousRequest::BUrlSynchronousRequest(BUrlRequest& request) 32 : 33 BUrlRequest(request.Url(), request.Output(), NULL, request.Context(), 34 "BUrlSynchronousRequest", request.Protocol()), 35 fRequestComplete(false), 36 fWrappedRequest(request) 37 { 38 } 39 #endif // LIBNETAPI_DEPRECATED 40 41 42 status_t 43 BUrlSynchronousRequest::Perform() 44 { 45 fWrappedRequest.SetListener(this); 46 fRequestComplete = false; 47 48 thread_id worker = fWrappedRequest.Run(); 49 // TODO something to do with the thread_id maybe ? 50 51 if (worker < B_OK) 52 return worker; 53 else 54 return B_OK; 55 } 56 57 58 status_t 59 BUrlSynchronousRequest::WaitUntilCompletion() 60 { 61 while (!fRequestComplete) 62 snooze(10000); 63 64 return B_OK; 65 } 66 67 68 void 69 BUrlSynchronousRequest::ConnectionOpened(BUrlRequest*) 70 { 71 PRINT(("SynchronousRequest::ConnectionOpened()\n")); 72 } 73 74 75 void 76 BUrlSynchronousRequest::HostnameResolved(BUrlRequest*, const char* ip) 77 { 78 PRINT(("SynchronousRequest::HostnameResolved(%s)\n", ip)); 79 } 80 81 82 void 83 BUrlSynchronousRequest::ResponseStarted(BUrlRequest*) 84 { 85 PRINT(("SynchronousRequest::ResponseStarted()\n")); 86 } 87 88 89 #ifdef LIBNETAPI_DEPRECATED 90 void 91 BUrlSynchronousRequest::HeadersReceived(BUrlRequest*, const BUrlResult& result) 92 { 93 PRINT(("SynchronousRequest::HeadersReceived()\n")); 94 } 95 96 97 void 98 BUrlSynchronousRequest::DataReceived(BUrlRequest*, const char*, 99 off_t, ssize_t size) 100 { 101 PRINT(("SynchronousRequest::DataReceived(%zd)\n", size)); 102 } 103 104 105 void 106 BUrlSynchronousRequest::DownloadProgress(BUrlRequest*, 107 ssize_t bytesReceived, ssize_t bytesTotal) 108 { 109 PRINT(("SynchronousRequest::DownloadProgress(%zd, %zd)\n", bytesReceived, 110 bytesTotal)); 111 } 112 113 114 void 115 BUrlSynchronousRequest::UploadProgress(BUrlRequest*, ssize_t bytesSent, 116 ssize_t bytesTotal) 117 { 118 PRINT(("SynchronousRequest::UploadProgress(%zd, %zd)\n", bytesSent, 119 bytesTotal)); 120 } 121 122 #else 123 124 void 125 BUrlSynchronousRequest::HeadersReceived(BUrlRequest*) 126 { 127 PRINT(("SynchronousRequest::HeadersReceived()\n")); 128 } 129 130 131 void 132 BUrlSynchronousRequest::BytesWritten(BUrlRequest* caller, size_t bytesWritten) 133 { 134 PRINT(("SynchronousRequest::BytesWritten(%" B_PRIdSSIZE ")\n", 135 bytesWritten)); 136 } 137 138 139 void 140 BUrlSynchronousRequest::DownloadProgress(BUrlRequest*, 141 off_t bytesReceived, off_t bytesTotal) 142 { 143 PRINT(("SynchronousRequest::DownloadProgress(%" B_PRIdOFF ", %" B_PRIdOFF 144 ")\n", bytesReceived, bytesTotal)); 145 } 146 147 148 void 149 BUrlSynchronousRequest::UploadProgress(BUrlRequest*, off_t bytesSent, 150 off_t bytesTotal) 151 { 152 PRINT(("SynchronousRequest::UploadProgress(%" B_PRIdOFF ", %" B_PRIdOFF 153 ")\n", bytesSent, bytesTotal)); 154 } 155 #endif // LIBNETAPI_DEPRECATED 156 157 158 void 159 BUrlSynchronousRequest::RequestCompleted(BUrlRequest* caller, bool success) 160 { 161 PRINT(("SynchronousRequest::RequestCompleted(%s) : %s\n", (success?"true":"false"), 162 strerror(caller->Status()))); 163 fRequestComplete = true; 164 } 165