27 extern "C" int vs_access(
const char *name,
int mode );
31 #define vs_access access
48 extern "C" int lstat(
const char *name,
struct stat *buf );
51 namespace VsnetDownload
62 COUT<<
"Enter "<<__PRETTY_FUNCTION__<<endl;
64 const char **
c = local_search_paths;
66 _local_search_paths.push_back( *c );
74 COUT<<
"Enter "<<__PRETTY_FUNCTION__<<endl;
88 COUT<<
"Enter "<<__PRETTY_FUNCTION__<<endl;
90 list< Item* >::iterator it;
93 for (it = items.begin(); it != items.end(); it++) {
97 _pending.push( item );
101 for (it = items.begin(); it != items.end(); it++) {
113 COUT<<
"Enter "<<__PRETTY_FUNCTION__<<endl;
120 private_eval_resolve_response( sock, buffer );
123 private_eval_download_error( sock, buffer );
129 private_eval_download( sock, buffer, sc );
135 COUT<<
"unexpected subcommand "<<sc<<
", ignoring"<<endl;
142 private_lower_poll();
145 void Manager::private_lower_poll()
148 if ( _pending.empty() ) {
152 COUT<<
"Enter "<<__PRETTY_FUNCTION__<<endl;
155 while (_pending.empty() ==
false) {
156 Item *
i = _pending.front();
160 if ( private_lower_test_access( i ) ) {
166 collect[i->
getSock()].push_back( i );
174 for (it = collect.begin(); it != collect.end(); it++) {
175 ItemList *cl = &it->second;
176 if (cl->empty() ==
false) {
180 assert( cl->size() < 0xffff );
182 for (ItemList_I strit = cl->begin(); strit != cl->end(); strit++) {
183 netbuf.
addChar( (*strit)->getFileType() );
184 netbuf.
addString( (*strit)->getFilename() );
198 ItemMapMap_I it = _asked.find( sock );
199 if ( it == _asked.end() ) {
200 COUT<<
"received resolve response for unasked socket "<<sock<<endl;
202 ItemList requestlist;
204 ItemMap *cl = &it->second;
206 for (num = respbuffer.
getShort(); num > 0; num--) {
207 string filename = respbuffer.
getString();
208 char ok = respbuffer.
getChar();
210 ItemMap_I mapi = cl->find( filename );
211 if ( mapi != cl->end() ) {
213 COUT<<
"file "<<filename<<
" found on server"<<endl;
215 requestlist.push_back( mapi->second );
217 COUT<<
"file "<<filename<<
" not found on server"<<endl;
222 COUT<<
"file "<<filename<<
" not in request list any more"<<endl;
225 if (requestlist.empty() ==
false) {
226 short num = requestlist.size();
231 for (rli = requestlist.begin(); rli != requestlist.end(); rli++) {
232 reqbuffer.
addChar( (*rli)->getFileType() );
233 reqbuffer.
addString( (*rli)->getFilename() );
248 ItemMapMap_I it = _asked.find( sock );
249 if ( it == _asked.end() ) {
250 COUT<<
"received resolve response for unasked socket "<<sock<<endl;
252 string filename = respbuffer.
getString();
253 ItemMap *cl = &it->second;
254 ItemMap_I mapi = cl->find( filename );
255 if ( mapi != cl->end() ) {
256 COUT<<
"file "<<filename<<
" not found on server"<<endl;
260 COUT<<
"file "<<filename<<
" not in request list any more"<<endl;
267 ItemMapMap_I it = _asked.find( sock );
268 if ( it == _asked.end() ) {
269 COUT<<
"received resolve response for unasked socket "<<sock<<endl;
271 COUT<<
"got data in a "<<sc<<endl;
277 ItemMap *cl = &it->second;
278 ItemMap_I mapi = cl->find( filename );
279 if ( mapi != cl->end() ) {
280 ItemSockMap_I smi = _currentItems.find( sock );
281 if ( smi != _currentItems.end() ) {
283 _currentItems.erase( smi );
286 mapi->second->setSize( sz );
287 mapi->second->append( buffer.
getBuffer( sz ), sz );
292 COUT<<
"file "<<filename<<
" not in request list any more"<<endl;
300 ItemMap *cl = &it->second;
301 ItemMap_I mapi = cl->find( filename );
302 if ( mapi != cl->end() ) {
303 ItemSockMap_I smi = _currentItems.find( sock );
304 if ( smi != _currentItems.end() ) {
306 _currentItems.erase( smi );
308 _currentItems.insert( ItemSockPair( sock, mapi->second ) );
312 mapi->second->setSize( len );
313 mapi->second->append( buffer.
getBuffer( sz ), sz );
318 COUT<<
"file "<<filename<<
" not in request list any more"<<endl;
326 ItemSockMap_I smi = _currentItems.find( sock );
327 if ( smi != _currentItems.end() ) {
329 smi->second->append( buffer.
getBuffer( sz ), sz );
334 _currentItems.erase( smi );
337 COUT<<
"no current item for this socket"<<endl;
343 COUT<<
"programming error"<<endl;
349 bool Manager::private_lower_test_access(
Item *i )
351 COUT<<
"Enter "<<__PRETTY_FUNCTION__<<endl;
354 for (vector< string >::const_iterator it = _local_search_paths.begin();
355 it != _local_search_paths.end();
357 string path = *it+
"/"+
file;
359 COUT<<
"Found local file "<<path.c_str()<<endl;
373 DownloadItem::DownloadItem(
SOCKETALT sock,
bool error,
const string &file ) :
409 DownloadItem( sock, false, file )
417 if (_handle)
delete _handle;
427 return _size-_offset;
433 _handle->
Read( (
char*) buf, sz );
453 if (_buf)
delete[] _buf;
463 return _size-_offset;
482 COUT<<
"Enter "<<__PRETTY_FUNCTION__<<endl;
484 const char **
c = local_search_paths;
486 _local_search_paths.push_back( *c );
494 COUT<<
"Enter "<<__PRETTY_FUNCTION__<<endl;
501 COUT<<
"Enter "<<__PRETTY_FUNCTION__<<endl;
506 COUT<<
" *** "<<
" cmd "<<c<<endl;
520 for (iter = r->
files.begin(); iter != r->
files.end(); iter++) {
523 string file = iter->file;
527 int zoneid = atoi( file.c_str() );
533 respbuffer.
addChar( ok ? 1 : 0 );
549 for (iter = r->
files.begin(); iter != r->
files.end(); iter++) {
551 string file = iter->file;
567 size_t bytes = f->
Size();
575 _download.push( di );
601 COUT<<
"Enter "<<__PRETTY_FUNCTION__<<endl;
603 _lower_download.erase( s );
609 while (_download.empty() ==
false) {
610 COUT<<
"Enter "<<__PRETTY_FUNCTION__<<endl;
612 DownloadItemPtr item = _download.front();
614 ItemMap_I it = _lower_download.find( item->getSock() );
615 if ( it == _lower_download.end() ) {
616 ItemQueuePtr
q(
new ItemQueue );
617 _lower_download.insert( ItemMapPair( item->getSock(),
q ) );
618 it = _lower_download.find( item->getSock() );
620 it->second->push( item );
624 list< SOCKETALT >tbd;
625 list< SOCKETALT >::const_iterator tbdi;
628 for (it = _lower_download.begin(); it != _lower_download.end(); it++) {
629 COUT<<
"Enter "<<__PRETTY_FUNCTION__<<endl;
631 bool done = private_lower_try_push_queue( it->first, it->second );
633 tbd.push_back( it->first );
640 for (tbdi = tbd.begin(); tbdi != tbd.end(); tbdi++)
641 _lower_download.erase( *tbdi );
648 bool Manager::private_lower_try_push_queue(
SOCKETALT sock, ItemQueuePtr
q )
650 COUT<<
"Enter "<<__PRETTY_FUNCTION__<<endl;
651 if (!q->empty() && sock.
queueLen(
LOPRI ) <= _packetWorkahead) {
654 DownloadItemPtr item = q->front();
655 if ( item->error() ) {
666 size_t l = sock.optPayloadSize();
667 if ( !sock.isTcp() ) {
675 string f( item->file() );
676 if (item->offset() == 0) {
689 if (item->remainingSize() <= l) {
692 short sz = item->remainingSize();
695 string f( item->file() );
703 item->copyFromFile( buf, sz );
709 respbuffer.
addInt32( item->remainingSize() );
715 item->copyFromFile( buf, l );
730 COUT<<
"Enter "<<__PRETTY_FUNCTION__<<endl;
731 for (vector< string >::const_iterator it = _local_search_paths.begin();
732 it != _local_search_paths.end();
734 string path = *it+
"/"+
file;
736 string ffile( file );
738 COUT<<
"Found local file "<<path.c_str()<<endl;
742 COUT<<
"Didn't find local file for "<<file.c_str()<<endl;
748 COUT<<
"Enter "<<__PRETTY_FUNCTION__<<endl;
753 COUT<<
"Opened local file "<<file<<endl;
771 COUT<<
"Couldn't open local file for "<<file<<endl;
776 size_t Manager::private_file_size(
const string &file )
778 COUT<<
"Enter "<<__PRETTY_FUNCTION__<<endl;
781 if (::
lstat( file.c_str(), &buf ) == 0) {
797 fp =
fopen( name,
"rb" );
804 fp =
fopen( name,
"r+b" );
818 FILE *f = VSFileSystem::OpenFile( name,
"rb" );
819 if (f == NULL)
return -1;
821 if (VSFileSystem::Fseek( f, 0, SEEK_END ) == 0) {
822 buf->
st_size = VSFileSystem::Ftell( f );
823 if (buf->
st_size >= 0) retval = 0;
825 VSFileSystem::Close( f );
830 using namespace VsnetDownload::Client;
848 return "Unknown state";