File: vtkKWRemoteIOManager.cxx

package info (click to toggle)
volview 3.4-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 25,204 kB
  • sloc: cpp: 132,585; ansic: 11,612; tcl: 236; sh: 64; makefile: 25; xml: 8
file content (888 lines) | stat: -rw-r--r-- 26,223 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
/*=========================================================================

  Copyright (c) Kitware, Inc.
  All rights reserved.
  See Copyright.txt or http://www.kitware.com/VolViewCopyright.htm for details.

     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notice for more information.

=========================================================================*/
#include "vtkObject.h"
#include "vtkObjectFactory.h"
#include "vtkKWRemoteIOManager.h"
#include "vtkURIHandler.h"
#include "vtkMutexLock.h"
#include "vtkMultiThreader.h"
#include "vtkKWRemoteIOTask.h"
#include "vtkKWDataTransfer.h"
#include "vtkCallbackCommand.h"
#include "vtkKWRemoteIOUtilities.h"

#include "vtksys/SystemTools.hxx"

#ifdef linux 
#include "unistd.h"
#endif

#ifdef VTK_USE_WIN32_THREADS
#include <windows.h>
#include <winbase.h>
#endif 

#include <list>
#include <string>
#include <algorithm>
#include <set>
#include <queue>

vtkStandardNewMacro ( vtkKWRemoteIOManager );
vtkCxxRevisionMacro(vtkKWRemoteIOManager, "$Revision: 1.20 $");

void vtkKWRemoteIOManagerTimerCallback( ClientData arg )
{
  vtkKWRemoteIOManager *self = reinterpret_cast< vtkKWRemoteIOManager * >(arg);
  self->TimerCallback();
}

class ProcessingTaskQueue : public 
      std::queue<vtkSmartPointer<vtkKWRemoteIOTask> > {};

//----------------------------------------------------------------------------
vtkKWRemoteIOManager::vtkKWRemoteIOManager()
{
  this->DataTransferCollection = vtkCollection::New();
  this->CacheManager           = vtkKWCacheManager::New();

  // set up callback
  this->TransferUpdateCommand = vtkCallbackCommand::New();
  this->TransferUpdateCommand->SetClientData (
      reinterpret_cast<void *>(this) );
  this->AddObserver ( vtkKWRemoteIOManager::TransferUpdateEvent,  
                      this->TransferUpdateCommand);
  this->TransferStatusChangedCommand = vtkCallbackCommand::New();
  this->TransferStatusChangedCommand->SetClientData ( 
      reinterpret_cast<void *>(this) );
  this->AddObserver ( vtkKWRemoteIOManager::TransferStatusChangedEvent,  
                      this->TransferStatusChangedCommand);

  this->ProcessingThreader         = vtkMultiThreader::New();
  this->ProcessingThreadId         = -1;
  this->ProcessingThreadActive     = false;
  this->ProcessingThreadActiveLock = vtkMutexLock::New();
  this->ProcessingTaskQueueLock    = vtkMutexLock::New();
  this->InternalTaskQueue          = new ProcessingTaskQueue;  
  this->TimerToken                 = NULL;
  this->TimerDelay                 = 1000; // 1 second (1000 ms)
}

//----------------------------------------------------------------------------
vtkKWRemoteIOManager::~vtkKWRemoteIOManager()
{
  this->RemoveObservers( 
      vtkKWRemoteIOManager::TransferStatusChangedEvent,  
      this->TransferStatusChangedCommand);
  this->RemoveObservers( 
      vtkKWRemoteIOManager::TransferUpdateEvent,  
      this->TransferUpdateCommand);

  if ( this->TransferUpdateCommand )
    {
    this->TransferUpdateCommand->Delete();
    this->TransferUpdateCommand = NULL;
    }
  
  if ( this->TransferStatusChangedCommand )
    {
    this->TransferStatusChangedCommand->Delete();
    this->TransferStatusChangedCommand = NULL;
    }
  
  if ( this->DataTransferCollection )
    {
    this->DataTransferCollection->RemoveAllItems();
    this->DataTransferCollection->Delete();
    this->DataTransferCollection = NULL;
    }

  if ( this->CacheManager )
    {
    this->CacheManager->Delete();
    this->CacheManager = NULL;
    }

  // Note that TerminateThread does not kill a thread, it only waits
  // for the thread to finish.  We need to signal the thread that we
  // want to terminate

  this->TerminateProcessingThread();
  
  delete this->InternalTaskQueue;
  this->InternalTaskQueue = 0;

  if (this->ProcessingThreader)
    {  
    this->ProcessingThreader->Delete();
    }
  if (this->ProcessingThreadActiveLock)
    {
    this->ProcessingThreadActiveLock->Delete();
    }
  if (this->ProcessingTaskQueueLock)
    {
    this->ProcessingTaskQueueLock->Delete();
    }
}

//----------------------------------------------------------------------------
void vtkKWRemoteIOManager::PrintSelf(ostream& os, vtkIndent indent)
{
  this->vtkObject::PrintSelf(os, indent);
  os << indent << "DataTransferCollection: " << this->GetDataTransferCollection() << "\n";
  os << indent << "CacheManager: " << this->GetCacheManager() << "\n";
}

//----------------------------------------------------------------------------
void vtkKWRemoteIOManager::SetTransferStatus(
  vtkKWDataTransfer *transfer, int status)
{
  if (transfer != NULL)
    {
    if (transfer->GetTransferStatus() != status)
      {
      transfer->SetTransferStatus(status);
      }
    }
}

//----------------------------------------------------------------------------
int vtkKWRemoteIOManager::GetNumberOfDataTransfers()
{
  if (this->DataTransferCollection == NULL)
    {
    return 0;;
    }
  return this->DataTransferCollection->GetNumberOfItems();
}

//----------------------------------------------------------------------------
int vtkKWRemoteIOManager::GetTransferStatus( vtkKWDataTransfer *transfer)
{
  return ( transfer->GetTransferStatus() );
}

//----------------------------------------------------------------------------
vtkKWDataTransfer *vtkKWRemoteIOManager::CreateNewDataTransfer()
{
  vtkKWDataTransfer *transfer = vtkKWDataTransfer::New();
  transfer->SetTransferID(this->GetUniqueTransferID());
  return transfer;
}

//----------------------------------------------------------------------------
void vtkKWRemoteIOManager::AllTransfersClearedFromCache()
{
  if (this->DataTransferCollection == NULL)
    {
    return;
    }

  const int n = this->DataTransferCollection->GetNumberOfItems();
  for ( int i=0; i < n; i++ )
    {
    if (vtkKWDataTransfer *dt = vtkKWDataTransfer::SafeDownCast(
              this->DataTransferCollection->GetItemAsObject(i) ))
      {
      dt->SetTransferCached(0);
      }
    }
}

//----------------------------------------------------------------------------
void vtkKWRemoteIOManager::AddDataTransfer(vtkKWDataTransfer *transfer)
{
  if (transfer == NULL)
    {
    vtkErrorMacro("AddDataTransfer: can't add a null transfer");
    return;
    }

  if ( this->DataTransferCollection == NULL )
    {
    this->DataTransferCollection = vtkCollection::New();
    }

  vtkDebugMacro("AddDataTransfer: adding item");
  this->DataTransferCollection->AddItem(transfer);
  this->Modified();
}

//----------------------------------------------------------------------------
void vtkKWRemoteIOManager::RemoveDataTransfer ( vtkKWDataTransfer *transfer)
{
  if (this->DataTransferCollection == NULL)
    {
    return;
    }

  if (transfer != NULL)
    {
    this->DataTransferCollection->RemoveItem(transfer);
    this->Modified();
    }
}

//----------------------------------------------------------------------------
void vtkKWRemoteIOManager::RemoveDataTransfer( int transferID )
{
  if (this->DataTransferCollection == NULL)
    {
    return;
    }

  int n = this->DataTransferCollection->GetNumberOfItems();
  for ( int i=0; i < n; i++ )
    {
    if (vtkKWDataTransfer *dt = vtkKWDataTransfer::SafeDownCast(
              this->DataTransferCollection->GetItemAsObject(i) ))
      {
      if ( dt->GetTransferID() == transferID )
        {
        this->DataTransferCollection->RemoveItem ( i );
        this->Modified();
        break;
        }
      }
    }
}

//----------------------------------------------------------------------------
vtkKWDataTransfer* vtkKWRemoteIOManager::GetDataTransferByTransferID(
  int transferID)
{
  if (this->DataTransferCollection)
    {
    int n = this->DataTransferCollection->GetNumberOfItems();
    for (int i = 0; i < n; i++)
      {
      vtkKWDataTransfer *dt = vtkKWDataTransfer::SafeDownCast( 
        this->DataTransferCollection->GetItemAsObject(i));
      if (dt && transferID == dt->GetTransferID())
        {
        return dt;
        }
      }
    }
  
  return NULL;
}

//----------------------------------------------------------------------------
vtkKWDataTransfer* vtkKWRemoteIOManager::GetDataTransferByIdentifier(
  const char *identifier)
{
  if (this->DataTransferCollection && identifier)
    {
    int n = this->DataTransferCollection->GetNumberOfItems();
    for (int i = 0; i < n; i++)
      {
      vtkKWDataTransfer *dt = vtkKWDataTransfer::SafeDownCast( 
        this->DataTransferCollection->GetItemAsObject(i));
      if (dt && dt->GetIdentifier() && !strcmp(identifier, dt->GetIdentifier()))
        {
        return dt;
        }
      }
    }
  
  return NULL;
}

//----------------------------------------------------------------------------
void vtkKWRemoteIOManager::ClearDataTransfers( )
{
  if (this->DataTransferCollection == NULL)
    {
    return;
    }

  this->DataTransferCollection->RemoveAllItems();
  this->Modified();
}

//----------------------------------------------------------------------------
int vtkKWRemoteIOManager::QueueRead( vtkKWDataTransfer *transfer )
{
  if (transfer == NULL)
    {
    vtkErrorMacro("QueueRead: null input node!");
    return 0;
    }

  vtkKWCacheManager *cm = this->GetCacheManager();
  const char *source = transfer->GetSourceURI();

  vtkstd::string dest = transfer->GetDestinationURI();

  // If the destination file is a relative filename, make it absolute, by 
  // prepending the cache directory.
  if (!vtksys::SystemTools::FileIsFullPath(dest.c_str()))
    {
    vtkstd::string dest = cm->GetRemoteCacheDirectory();
    dest += "/";

    if (transfer->GetDestinationURI())
      {
      dest += transfer->GetDestinationURI();
      }
    else
      {
      const char *destFname = cm->GetFilenameFromURI(source);
      if (destFname == NULL)
        {
        vtkErrorMacro("Unable to get file name from source URI " << source);
        return 0;
        }
      dest += destFname;
      }
    }

  // Create the path where the file will reside.
  if (!vtksys::SystemTools::MakeDirectory(
       vtksys::SystemTools::GetFilenamePath(dest).c_str()))
    {
    vtkErrorMacro("Failed to create the path " <<
        vtksys::SystemTools::GetFilenamePath(dest).c_str() );
    return 0;
    }

  //--- check to see if RemoteCacheLimit is exceeded
  //--- check to see if FreeBufferSize is exceeded.
 
  //--- if force redownload is enabled, remove the old file from cache.
  if (cm->GetEnableForceRedownload () )
    {
    vtkDebugMacro("QueueRead: Calling remove from cache");
    this->GetCacheManager()->DeleteFromCache(dest.c_str());
    }
  
  //--- trigger logic to download, if there's cache space.
  //--- need to convert to bytes to get a more conservative guess.
  if ( (cm->GetCurrentCacheSize()*1000000.0) < 
      ((float)(cm->GetRemoteCacheLimit())*1000000.0) )
    {
    if ( cm->CachedFileExists(dest.c_str()) )
      {
      vtkDebugMacro( << dest.c_str() << " already exists in the cache.");
      return 1;
      }

    // This is where the final file will be downloaded.
    transfer->SetDestinationURI( dest.c_str() );

    if ( transfer->GetAsynchronous() )
      {
      vtkDebugMacro("QueueRead: Schedule an ASYNCHRONOUS data transfer");

      vtkKWRemoteIOTask *task = vtkKWRemoteIOTask::New();
      task->SetTypeToNetworking();

      // Pass the current data transfer, which has a pointer 
      // to the associated mrml node, as client data to the task.
      if ( !task )
        {
        transfer->Delete();
        return 0;
        }
      transfer->SetTransferStatus ( vtkKWDataTransfer::Pending );
      task->SetTaskFunction(this, (vtkKWRemoteIOTask::TaskFunctionPointer)
                            &vtkKWRemoteIOManager::ApplyTransfer, transfer);
    
      // Schedule the transfer
      if ( ! this->ScheduleTask( task ) )
        {
        transfer->SetTransferStatus( vtkKWDataTransfer::CompletedWithErrors);
        task->Delete();
        return 0;      
        }
      task->Delete();
      }
    else
      {
      vtkDebugMacro("QueueRead: Schedule a SYNCHRONOUS data transfer");

      transfer->SetTransferStatus( vtkKWDataTransfer::Running);

      // Add an observer for possible progress reporting
      vtkURIHandler *handler = transfer->GetHandler();
      unsigned long tag = 0;
      if (handler)
        {
        tag = transfer->AddObserver( 
          vtkKWRemoteIOManager::TransferUpdateEvent, this->TransferUpdateCommand);
        }

      
      // This transfers the data.
      this->ApplyTransfer ( transfer );

      // Remove the observer, now that the transfer is done.
      if (tag)
        {
        transfer->RemoveObserver(tag);
        }

      transfer->SetTransferStatus( vtkKWDataTransfer::Completed);
      }
    }

  return 1;
}

//----------------------------------------------------------------------------
int vtkKWRemoteIOManager::GetUniqueTransferID()
{
  
  //--- keep looping until we find an id that is unique
  int id = 1;
  int exists = 0;
  vtkKWDataTransfer *dt;
    
  if ( this->DataTransferCollection == NULL )
    {
    this->DataTransferCollection = vtkCollection::New();
    }

  // loop until found or return.
  while ( !exists )
    {
    
    // loop thru the existing data transfers
    int n = this->DataTransferCollection->GetNumberOfItems();
    vtkDebugMacro("GetUniqueTransferID: in loop, id = " << id << ", n = " << n);
    for ( int i=0; i < n; i++ )
      {
      dt = vtkKWDataTransfer::SafeDownCast(
          this->DataTransferCollection->GetItemAsObject ( i ) );
      if  ( dt != NULL && id == dt->GetTransferID() )
        {
        exists = 1;
        break;
        }
      }
    // finished looping -- did we find the id?
    if ( exists )
      {
      // if so, try a new id
      id++;
      exists = 0;
      }
    else
      {
      vtkDebugMacro("GetUniqueTransferID: in loop, returning id = " << id);
      return (id);
      }
    }
  vtkDebugMacro("GetUniqueTransferID: returning id = " << id);
  return ( id );
}

//----------------------------------------------------------------------------
void vtkKWRemoteIOManager::CreateProcessingThread()
{
  if (this->ProcessingThreadId == -1)
    {
    this->ProcessingThreadActiveLock->Lock();
    this->ProcessingThreadActive = true;
    this->ProcessingThreadActiveLock->Unlock();
    
    this->ProcessingThreadId = this->ProcessingThreader->SpawnThread(
              vtkKWRemoteIOManager::ProcessingThreaderCallback, this);

    // Start four network threads (TODO: make the number of threads a setting)
    this->NetworkingThreadIDs.push_back( this->ProcessingThreader
      ->SpawnThread(vtkKWRemoteIOManager::NetworkingThreaderCallback, this));

    /*
     * TODO: it looks like curl is not thread safe by default
     * - maybe there's a setting that cmcurl can have
     *   similar to the --enable-threading of the standard curl build
     *
    this->NetworkingThreadIDs.push_back ( this->ProcessingThreader
          ->SpawnThread(vtkKWRemoteIOManager::NetworkingThreaderCallback,
                    this) );
    this->NetworkingThreadIDs.push_back ( this->ProcessingThreader
          ->SpawnThread(vtkKWRemoteIOManager::NetworkingThreaderCallback,
                    this) );
    this->NetworkingThreadIDs.push_back ( this->ProcessingThreader
          ->SpawnThread(vtkKWRemoteIOManager::NetworkingThreaderCallback,
                    this) );
    */

    // Let's create a timer to notify us about the IO taking place on this
    // other thread, we've created.
    if (!this->TimerToken)
      {
      this->TimerToken = Tcl_CreateTimerHandler( 
                  this->TimerDelay, /* every 1000 ms */
                  vtkKWRemoteIOManagerTimerCallback,
                  (ClientData)this);
      }
    }
}

//----------------------------------------------------------------------------
void vtkKWRemoteIOManager::TerminateProcessingThread()
{
  if (this->ProcessingThreadId != -1 && this->ProcessingThreader)
    {
    this->ProcessingThreadActiveLock->Lock();
    this->ProcessingThreadActive = false;
    this->ProcessingThreadActiveLock->Unlock();

    this->ProcessingThreader->TerminateThread( this->ProcessingThreadId );
    this->ProcessingThreadId = -1;

    std::vector<int>::const_iterator idIterator;
    idIterator = this->NetworkingThreadIDs.begin();
    while (idIterator != this->NetworkingThreadIDs.end())
      {
      this->ProcessingThreader->TerminateThread( *idIterator );
      ++idIterator;
      }
    this->NetworkingThreadIDs.clear();

    // Remote timer callbacks.
    if ( this->TimerToken )
      {
      Tcl_DeleteTimerHandler( this->TimerToken );
      this->TimerToken = NULL;
      }    
    }
}


//----------------------------------------------------------------------------
VTK_THREAD_RETURN_TYPE vtkKWRemoteIOManager
::ProcessingThreaderCallback( void *arg )
{ 
#ifdef VTK_USE_WIN32_THREADS
  // Adjust the priority of this thread
  SetThreadPriority(GetCurrentThread(),
                    THREAD_PRIORITY_BELOW_NORMAL);
#endif

#ifdef VTK_USE_PTHREADS
  // Adjust the priority of all PROCESS level threads.  Not a perfect solution.
  nice(20);
#endif
    
  // pull out the reference to the appLogic
  vtkKWRemoteIOManager *self = (vtkKWRemoteIOManager*)
    (((vtkMultiThreader::ThreadInfo *)(arg))->UserData);

  // Tell the app to start processing any tasks slated for the
  // processing thread
  self->ProcessProcessingTasks();

  return VTK_THREAD_RETURN_VALUE;
}

//----------------------------------------------------------------------------
void vtkKWRemoteIOManager::ProcessProcessingTasks()
{
  int active = true;
  vtkSmartPointer<vtkKWRemoteIOTask> task = 0;
  
  while (active)
    {
    // Check to see if we should be shutting down
    this->ProcessingThreadActiveLock->Lock();
    active = this->ProcessingThreadActive;
    this->ProcessingThreadActiveLock->Unlock();

    if (active)
      {
      // pull a task off the queue
      this->ProcessingTaskQueueLock->Lock();
      if ((*this->InternalTaskQueue).size() > 0)
        {
        //std::cout << "Number of queued tasks: " 
        //  << (*this->InternalTaskQueue).size() << std::endl;
        
        // only handle processing tasks in this thread
        task = (*this->InternalTaskQueue).front();
        if ( task->GetType() == vtkKWRemoteIOTask::Processing )
          {
          (*this->InternalTaskQueue).pop();
          }
        else 
          {
          task = NULL;
          }
        }
      this->ProcessingTaskQueueLock->Unlock();
      
      // process the task (should this be in a separate thread?)
      if (task)
        {
        task->Execute();
        task = 0;
        }
      }

    // busy wait
    vtksys::SystemTools::Delay(100);
    }
}

//----------------------------------------------------------------------------
VTK_THREAD_RETURN_TYPE vtkKWRemoteIOManager
::NetworkingThreaderCallback( void *arg )
{
  
#ifdef VTK_USE_WIN32_THREADS
  // Adjust the priority of this thread
  SetThreadPriority(GetCurrentThread(),
                    THREAD_PRIORITY_BELOW_NORMAL);
#endif

#ifdef VTK_USE_PTHREADS
  // Adjust the priority of all PROCESS level threads.  Not a perfect solution.
  nice(20);
#endif
    
  // pull out the reference to the appLogic
  vtkKWRemoteIOManager *self = (vtkKWRemoteIOManager*)
    (((vtkMultiThreader::ThreadInfo *)(arg))->UserData);

  // Tell the app to start processing any tasks slated for the
  // processing thread
  self->ProcessNetworkingTasks();

  return VTK_THREAD_RETURN_VALUE;
}

//----------------------------------------------------------------------------
void vtkKWRemoteIOManager::ProcessNetworkingTasks()
{
  int active = true;
  vtkSmartPointer<vtkKWRemoteIOTask> task = 0;
  
  while (active)
    {
    // Check to see if we should be shutting down
    this->ProcessingThreadActiveLock->Lock();
    active = this->ProcessingThreadActive;
    this->ProcessingThreadActiveLock->Unlock();

    if (active)
      {
      // pull a task off the queue
      this->ProcessingTaskQueueLock->Lock();
      if ((*this->InternalTaskQueue).size() > 0)
        {
        // std::cout << "Number of queued tasks: " 
        //  << (*this->InternalTaskQueue).size() << std::endl;
        task = (*this->InternalTaskQueue).front();
        if ( task->GetType() == vtkKWRemoteIOTask::Networking )
          {
          (*this->InternalTaskQueue).pop();
          }
        else 
          {
          task = NULL;
          }
        }
      this->ProcessingTaskQueueLock->Unlock();
      
      // process the task (should this be in a separate thread?)
      if (task)
        {
        task->Execute();
        task = 0;
        }
      }

    // busy wait
    vtksys::SystemTools::Delay(100);
    }
}

//----------------------------------------------------------------------------
int vtkKWRemoteIOManager::ScheduleTask( vtkKWRemoteIOTask *task )
{
  //std::cout << "Scheduling a task ";

  // only schedule a task if the processing task is up
  this->ProcessingThreadActiveLock->Lock();
  int active = this->ProcessingThreadActive;
  this->ProcessingThreadActiveLock->Unlock();

  if (!active)
    {
    // We might not have created the processing thread yet. Let's try to
    // create it.
    this->CreateProcessingThread();
    }

  // only schedule a task if the processing task is up
  this->ProcessingThreadActiveLock->Lock();
  active = this->ProcessingThreadActive;
  this->ProcessingThreadActiveLock->Unlock();

  // If active is still false, we failed to create the processing thread.
  // Let's return false.

  if (active)
    {
    this->ProcessingTaskQueueLock->Lock();
    (*this->InternalTaskQueue).push( task );
    //std::cout << (*this->InternalTaskQueue).size() << std::endl;
    this->ProcessingTaskQueueLock->Unlock();
    
    return true;
    }

  // could not schedule the task
  return false;
}

//----------------------------------------------------------------------------
// The actual work of transferring data is done here.
void vtkKWRemoteIOManager::ApplyTransfer( void *clientdata )
{
  //--- get the DataTransfer from the clientdata
  vtkKWDataTransfer *dt = reinterpret_cast < vtkKWDataTransfer*> (clientdata);
  if ( dt == NULL )
    {
    vtkErrorMacro("ApplyTransfer: data transfer is null");
    return;
    }

  int asynchIO = dt->GetAsynchronous();

  const char *source = dt->GetSourceURI();
  vtkstd::string dest = dt->GetDestinationURI();
  if ( dt->GetTransferType() == vtkKWDataTransfer::RemoteDownload  )
    {
    //---
    //--- Download data
    //---
    vtkURIHandler *handler = dt->GetHandler();
    if ( handler != NULL && source != NULL )
      {
      int async_pending = 
        asynchIO && dt->GetTransferStatus() == vtkKWDataTransfer::Pending;
      if (async_pending)
        {
        dt->SetTransferStatusNoModify ( vtkKWDataTransfer::Running );
        }
      else
        {
        vtkDebugMacro(
          "ApplyTransfer: stage file read on the handler..., source = " 
            << source << ", dest = " << dest.c_str());
        }
      
      handler->StageFileRead( source, dest.c_str() );

      // If the downloaded files are .tar files, untar them.
      //
      size_t posTar = dest.rfind(".tar");
      size_t posTarGz = dest.rfind(".tar.gz");
      const bool isTar = 
        (posTar != vtkstd::string::npos && posTar == (dest.length()-4));
      const bool isTarGz = 
        (posTarGz != vtkstd::string::npos && posTarGz == (dest.length()-7));
      if ((isTar || isTarGz) &&
          !dt->GetDisableTar() && 
          vtksys::SystemTools::FileExists(dest.c_str()))
        {
        vtkstd::string destPath = 
          vtksys::SystemTools::GetFilenamePath(dest.c_str());

        // Extract the tar / tar.gz archive
        if (vtkKWRemoteIOUtilities::ExtractTar( 
              dest.c_str(), destPath.c_str(), isTarGz ))
          {
          // Remove the tar archive after untarring
          vtksys::SystemTools::RemoveFile(dest.c_str());
          }
        }

      if (async_pending)
        {
        dt->SetTransferStatusNoModify ( vtkKWDataTransfer::Completed );
        }
      }
    }

  else if ( dt->GetTransferType() == vtkKWDataTransfer::RemoteUpload  )
    {
    }
  else
    {
    vtkErrorMacro(
      "ApplyTransfer: unknonw transfer type " <<  dt->GetTransferType() );
    }
}

//----------------------------------------------------------------------------
void vtkKWRemoteIOManager::TimerCallback()
{
  if (this->TimerToken)
    {
    Tcl_DeleteTimerHandler( this->TimerToken );
    this->TimerToken = NULL;
    }

  const int n = this->DataTransferCollection->GetNumberOfItems();
  for ( int i=0; i < n; i++ )
    {
    vtkKWDataTransfer *dt = static_cast< vtkKWDataTransfer *>(
            this->DataTransferCollection->GetItemAsObject(i) );
    
   // Check if the transfer status changed.
    if (dt->CheckAndUpdateTransferStatus())
      {
      this->InvokeEvent( 
          vtkKWRemoteIOManager::TransferStatusChangedEvent, dt );
      }

    else if (dt->GetTransferStatus() == vtkKWDataTransfer::Running)
      {
      // Report some progress event
      this->InvokeEvent( 
          vtkKWRemoteIOManager::TransferUpdateEvent, dt );
      }
    }

  if (!this->TimerToken)
    {
    this->TimerToken = Tcl_CreateTimerHandler( 
                this->TimerDelay,
                vtkKWRemoteIOManagerTimerCallback,
                (ClientData)this);
    }
}

//----------------------------------------------------------------------------
void vtkKWRemoteIOManager::SetTransferStatusChangedCallback( 
      void (*f)(vtkObject *caller, unsigned long eid,
      void *clientdata, void *calldata), vtkObject *obj)
{
  this->TransferStatusChangedCommand->SetCallback(f);
  this->TransferStatusChangedCommand->SetClientData(obj);
}

//----------------------------------------------------------------------------
void vtkKWRemoteIOManager::SetTransferUpdateCallback( 
      void (*f)(vtkObject *caller, unsigned long eid,
      void *clientdata, void *calldata), vtkObject *obj)
{
  this->TransferUpdateCommand->SetCallback(f);
  this->TransferUpdateCommand->SetClientData(obj);
}