File: Xfer.C

package info (click to toggle)
paraview 4.1.0%2Bdfsg%2B1-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 278,136 kB
  • ctags: 340,527
  • sloc: cpp: 2,314,053; ansic: 817,139; python: 245,770; xml: 68,996; tcl: 48,285; fortran: 39,116; yacc: 5,713; java: 3,827; perl: 3,108; sh: 2,045; lex: 1,809; makefile: 935; asm: 471; pascal: 228
file content (784 lines) | stat: -rw-r--r-- 21,414 bytes parent folder | download | duplicates (4)
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
/*****************************************************************************
*
* Copyright (c) 2000 - 2010, Lawrence Livermore National Security, LLC
* Produced at the Lawrence Livermore National Laboratory
* LLNL-CODE-400124
* All rights reserved.
*
* This file is  part of VisIt. For  details, see https://visit.llnl.gov/.  The
* full copyright notice is contained in the file COPYRIGHT located at the root
* of the VisIt distribution or at http://www.llnl.gov/visit/copyright.html.
*
* Redistribution  and  use  in  source  and  binary  forms,  with  or  without
* modification, are permitted provided that the following conditions are met:
*
*  - Redistributions of  source code must  retain the above  copyright notice,
*    this list of conditions and the disclaimer below.
*  - Redistributions in binary form must reproduce the above copyright notice,
*    this  list of  conditions  and  the  disclaimer (as noted below)  in  the
*    documentation and/or other materials provided with the distribution.
*  - Neither the name of  the LLNS/LLNL nor the names of  its contributors may
*    be used to endorse or promote products derived from this software without
*    specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT  HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR  IMPLIED WARRANTIES, INCLUDING,  BUT NOT  LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND  FITNESS FOR A PARTICULAR  PURPOSE
* ARE  DISCLAIMED. IN  NO EVENT  SHALL LAWRENCE  LIVERMORE NATIONAL  SECURITY,
* LLC, THE  U.S.  DEPARTMENT OF  ENERGY  OR  CONTRIBUTORS BE  LIABLE  FOR  ANY
* DIRECT,  INDIRECT,   INCIDENTAL,   SPECIAL,   EXEMPLARY,  OR   CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT  LIMITED TO, PROCUREMENT OF  SUBSTITUTE GOODS OR
* SERVICES; LOSS OF  USE, DATA, OR PROFITS; OR  BUSINESS INTERRUPTION) HOWEVER
* CAUSED  AND  ON  ANY  THEORY  OF  LIABILITY,  WHETHER  IN  CONTRACT,  STRICT
* LIABILITY, OR TORT  (INCLUDING NEGLIGENCE OR OTHERWISE)  ARISING IN ANY  WAY
* OUT OF THE  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*
*****************************************************************************/

#include <Xfer.h>
#include <AttributeSubject.h>
#include <VisItRPC.h>
#include <Connection.h>
#include <visitstream.h>
#include <DebugStream.h>

// ****************************************************************************
// Method: Xfer::Xfer
//
// Purpose: 
//   Constructor for the Xfer class.
//
// Programmer: Brad Whitlock
// Creation:   Thu Jul 13 11:16:23 PDT 2000
//
// Modifications:
//   Brad Whitlock, Thu Feb 27 11:07:00 PDT 2003
//   I initialized some new members related to special opcodes.
//
// ****************************************************************************

Xfer::Xfer() : SimpleObserver(), subjectList()
{
    input = output = NULL;

    opcode = 0; length = -1;
    haveStoredHeader = false;

    nextGuido = 0;
    nextSpecialOpcode = -2;
    specialOpcodeCallback = 0;
    specialOpcodeCallbackData = 0;
}

// ****************************************************************************
// Method: Xfer::~Xfer
//
// Purpose: 
//   Destructor for the Xfer class.
//
// Programmer: Brad Whitlock
// Creation:   Thu Jul 13 11:17:24 PDT 2000
//
// Modifications:
//   
// ****************************************************************************

Xfer::~Xfer()
{
    std::vector<AttributeSubject*>::iterator pos;

    // Detach this observer from all of the subjects it's observing.
    for(pos = subjectList.begin(); pos != subjectList.end(); ++pos)
    {
        (*pos)->Detach(this);
    }
}

// ****************************************************************************
// Method: Xfer::Add
//
// Purpose: 
//   Adds a new AttributeSubject to the list of subjects it is
//   observing.
//
// Arguments:
//   subject: The subject that will be added to the list.
//
// Returns:    
//
// Note:       
//
// Programmer: Brad Whitlock
// Creation:   Thu Jul 13 11:19:07 PDT 2000
//
// Modifications:
//   
// ****************************************************************************

void
Xfer::Add(AttributeSubject *subject)
{
    // Subscribe to the subject
    subject->Attach(this);
    subjectList.push_back(subject);

    // Set the subject's Guido
    subject->SetGuido(GetNextGuido());
}

// ****************************************************************************
// Method: Xfer::Add
//
// Purpose: 
//   Adds a new RPC to the list of subjects it is observing.
//
// Arguments:
//   rpc: The rpc that will be added to the list.
//
// Returns:    
//
// Note:       
//
// Programmer: Jeremy Meredith
// Creation:   Fri Aug 11 13:47:56 PDT 2000
//
// Modifications:
//
// ****************************************************************************

void
Xfer::Add(VisItRPC *rpc)
{
    Add((AttributeSubject *)rpc);
    rpc->SetXfer(this);

    Add(rpc->GetReply());
}

// ****************************************************************************
// Method: Xfer::Remove
//
// Purpose: 
//   Removes a subject from the list of subjects the Xfer object is
//   observing.
//
// Arguments:
//   subject : The subject that is being removed from the list.
//
// Returns:    
//
// Note:       
//
// Programmer: Brad Whitlock
// Creation:   Thu Jul 13 11:20:12 PDT 2000
//
// Modifications:
//   
// ****************************************************************************

void
Xfer::Remove(AttributeSubject *subject)
{
    std::vector<AttributeSubject*>::iterator pos;

    // Erase all references to the subject.
    for(pos = subjectList.begin(); pos != subjectList.end(); )
    {
       if(*pos == subject)
           pos = subjectList.erase(pos);
       else
           ++pos;
    }

    // Detach from the subject
    subject->Detach(this);
}

// ****************************************************************************
// Method: Xfer::Update
//
// Purpose: 
//   Writes a subject to the Xfer object's output connection.
//   Depending on the type of output connection, this is mainly
//   used to send subjects to other processes.
//
// Arguments:
//    TheChangedSubject : The subject to write on the output connection.
//
// Returns:    
//
// Note:       
//
// Programmer: Brad Whitlock
// Creation:   Thu Jul 13 11:21:40 PDT 2000
//
// Modifications:
//   Brad Whitlock, Fri Jul 25 12:16:17 PDT 2003
//   Added debug coding.
//
// ****************************************************************************

void
Xfer::Update(Subject *TheChangedSubject)
{
    if (output == NULL)
        return;

    AttributeSubject *subject = (AttributeSubject *)TheChangedSubject;

    // Write out the subject's guido and message size.
    output->WriteInt(subject->GetGuido());
    int sz = subject->CalculateMessageSize(*output);
    output->WriteInt(sz);

    debug5 << "Xfer::Update: Sending: opcode=" << subject->GetGuido()
           << ", len=" << sz
           << ", name=" << subject->TypeName().c_str() << endl;

    // Write the things about the subject that have changed onto the
    // output connection and flush it out to make sure it's sent.
    subject->Write(*output);
    output->Flush();
}

// ****************************************************************************
//  Method:  Xfer::SendInterruption
//
//  Purpose:
//    Send an interruption message to the remote connection.
//
//  Programmer:  Jeremy Meredith
//  Creation:    July  2, 2001
//
// ****************************************************************************

void
Xfer::SendInterruption()
{
    output->WriteInt(-1);
    output->WriteInt(0);
    output->Flush();
}

// ****************************************************************************
// Method: Xfer::SubjectRemoved
//
// Purpose: 
//   Removes a subject from the list of subjects that the Xfer object
//   is watching. This is called when the subject is destroyed. It
//   is a precaution against the Xfer trying to detach from a subject
//   that has been destroyed.
//
// Arguments:
//   TheRemovedSubject : The subject that was destroyed.
//
// Programmer: Brad Whitlock
// Creation:   Thu Aug 31 15:28:41 PST 2000
//
// Modifications:
//   
// ****************************************************************************

void
Xfer::SubjectRemoved(Subject *TheRemovedSubject)
{
    std::vector<AttributeSubject*>::iterator pos;

    // Erase all references to the subject that was removed.
    for(pos = subjectList.begin(); pos != subjectList.end(); )
    {
       if(*pos == TheRemovedSubject)
           pos = subjectList.erase(pos);
       else
           ++pos;
    }
}

// ****************************************************************************
// Method: Xfer::ReadPendingMessages
//
// Purpose: 
//   This method reads messages encoded on the Xfer object's
//   input connection and copies them to another buffer.  It also
//   checks for pending Abort commands.
//
// Notes:      This function returns whether we've processed an interruption.
//             If we processed an interruption, we cleared pending commands
//             from the buffered input and returned true. Otherwise we return
//             false.
//
// Programmer: Jeremy Meredith
// Creation:   July  3, 2001
//
// Modifications:
//   Brad Whitlock, Thu Feb 27 11:00:34 PDT 2003
//   I added support for special opcodes.
//
//   Jeremy Meredith, Tue Mar  4 13:10:25 PST 2003
//   Added length to the new buffer because MPIXfer needs it.
//
//   Brad Whitlock, Thu May 5 16:59:28 PST 2005
//   I changed the code so interrupt can be handled as a special opcode.
//
// ****************************************************************************

bool
Xfer::ReadPendingMessages()
{
    // While there are complete messages, read them.
    while(ReadHeader())
    {
        if (opcode > nextSpecialOpcode && opcode < 0)
        {
            if (opcode == -1)
                bufferedInput.Flush();

            // If the callback and the data were provided, call the callback
            // so it can process the user-defined opcode.
            if(specialOpcodeCallback != 0)
                specialOpcodeCallback(opcode, specialOpcodeCallbackData);

            if (opcode == -1)
                return true;

            continue;
        }

        bufferedInput.WriteInt(opcode);
        bufferedInput.WriteInt(length);
        for (int i=0; i<length; i++)
        {
            unsigned char tmp;
            input->Read(&tmp);
            bufferedInput.Write(tmp);
        }
    }

    return false;
}

// ****************************************************************************
// Method: Xfer::Process
//
// Purpose: 
//   This method processes messages encoded on the Xfer object's
//   input connection. It keeps going while there are complete
//   messages.
//
// Arguments:
//
// Returns:    
//
// Note:       
//
// Programmer: Brad Whitlock
// Creation:   Thu Jul 13 11:23:57 PDT 2000
//
// Modifications:
//    Jeremy Meredith, Tue Jul  3 15:02:12 PDT 2001
//    Made it read from the new buffer instead of the input connection.
//   
//    Jeremy Meredith, Tue Mar  4 13:10:25 PST 2003
//    Added length to the new buffer because MPIXfer needs it.
//
//    Brad Whitlock, Fri Jul 25 12:14:56 PDT 2003
//    Added debug messages and added code to skip unknown opcodes so we
//    don't hang as easily if the protocol gets messed up somehow.
//
// ****************************************************************************

void
Xfer::Process()
{
    ReadPendingMessages();

    // While there are complete messages, read and process them.
    while (bufferedInput.Size() > 0)
    {
        int     curOpcode;
        int     curLength;
        bufferedInput.ReadInt(&curOpcode);
        bufferedInput.ReadInt(&curLength);

        bool    bytesNeedToBeSkipped = true;
        if (curOpcode < subjectList.size())
        {
            if (subjectList[curOpcode])
            {
                debug5 << "Xfer::Process: Opcode=" << curOpcode
                    << ", len=" << curLength
                    << ", type="
                    << subjectList[curOpcode]->TypeName().c_str() << endl;

                // Read the object into its local copy.
                subjectList[curOpcode]->Read(bufferedInput);

                // Indicate that we want Xfer to ignore update messages if
                // it gets them while processing the Notify.
                SetUpdate(false);
                subjectList[curOpcode]->Notify();
                bytesNeedToBeSkipped = false;
            }
        }

        if (bytesNeedToBeSkipped)
        {
            debug1 << "Xfer::Process: Opcode " << curOpcode
                   << " is unknown! Skipping " << curLength << " bytes." << endl;
            unsigned char uchar;
            for (int i = 0; i < curLength; ++i)
                bufferedInput.Read(&uchar);
        }
    }
}

// *************************************************************************************
// Method: Xfer::ListObjects
//
// Purpose: 
//   Lists the names of the objects that are hooked up.
//
// Programmer: Brad Whitlock
// Creation:   Fri Mar 22 16:48:28 PST 2002
//
// Modifications:
//   
// *************************************************************************************

void
Xfer::ListObjects()
{
    debug2 << "Xfer: Number of objects: " << subjectList.size() << endl;
    for(size_t i = 0; i < subjectList.size(); ++i)
        debug2 << "Object[" << i << "] = " << subjectList[i]->TypeName().c_str()
               << endl;
}

// ****************************************************************************
// Method: Xfer::ReadHeader
//
// Purpose: 
//   Checks the input connection to see if there is an entire message
//   waiting to be read. If there is, it strips the header and returns
//   true. Otherwise, don't strip the header and return false.
//
// Arguments:
//
// Returns:    
//   true if the message can be read
//   false if the message cannot be read.
//
// Programmer: Brad Whitlock
// Creation:   Mon Jun 5 15:11:49 PST 2000
//
// Modifications:
//    Jeremy Meredith, Tue Jul  3 15:17:24 PDT 2001
//    Changed check to allow empty messages.
//   
// ****************************************************************************

bool
Xfer::ReadHeader()
{
    if(input == NULL)
        return false;

    bool retval = false;

    // If the connection has more than the header in its buffer, try
    // reading the header. Also try if there is a stored header. That
    // means that we've tried to read for this operation before. Maybe
    // the message is all there this time.

    if((input->Size() >= (2 * sizeof(int))) || haveStoredHeader)
    {
        if(!haveStoredHeader)
        {
            input->ReadInt(&opcode);
            input->ReadInt(&length);
        }

        // Check the message size against the size of what's actually
        // arrived in the buffer.
        retval = (length <= input->Size());
        haveStoredHeader = !retval;
    }

    return retval;
}

// ****************************************************************************
// Method: Xfer::SetInputConnection
//
// Purpose: 
//   Sets the object's input connection. This is the connection used
//   to read messages for the Process method.
//
// Arguments:
//   conn : The connection to use for the input connection.
//
// Returns:    
//
// Note:       
//
// Programmer: Brad Whitlock
// Creation:   Thu Jul 13 11:26:01 PDT 2000
//
// Modifications:
//   
// ****************************************************************************

void
Xfer::SetInputConnection(Connection *conn)
{
    input = conn;
}

// ****************************************************************************
// Method: Xfer::SetOutputConnection
//
// Purpose: 
//   Sets the object's output connection. This is the connection used
//   by the Update method that writes an object onto the connection.
//
// Arguments:
//   conn : The connection to use for the output connection.
//
// Returns:    
//
// Note:       
//
// Programmer: Brad Whitlock
// Creation:   Thu Jul 13 11:26:53 PDT 2000
//
// Modifications:
//   
// ****************************************************************************

void
Xfer::SetOutputConnection(Connection *conn)
{
    output = conn;
}

// ****************************************************************************
// Method: Xfer::GetInputConnection
//
// Purpose: 
//   Gets the object's input connection. This is the connection used
//   to read messages for the Process method.
//
// Arguments:
//
// Returns:    Connection*
//
// Note:       
//
// Programmer: Jeremy Meredith
// Creation:   Fri Aug 11 14:55:38 PDT 2000
//
// Modifications:
//   
// ****************************************************************************

Connection *
Xfer::GetInputConnection() const
{
    return input;
}

// ****************************************************************************
// Method: Xfer::GetOutputConnection
//
// Purpose: 
//   Gets the object's output connection. This is the connection used
//   by the Update method that writes an object onto the connection.
//
// Arguments:
//
// Returns:    Connection*
//
// Note:       
//
// Programmer: Jeremy Meredith
// Creation:   Fri Aug 11 14:55:26 PDT 2000
//
// Modifications:
//   
// ****************************************************************************

Connection *
Xfer::GetOutputConnection() const
{
    return output;
}

// ****************************************************************************
// Method: Xfer::GetBufferedInputConnection
//
// Purpose: 
//   Returns a pointer to the buffered input connection.
//
// Returns:    Connection*
//
// Programmer: Brad Whitlock
// Creation:   Thu Apr 14 16:39:09 PST 2005
//
// Modifications:
//   
// ****************************************************************************

Connection *
Xfer::GetBufferedInputConnection()
{
    return &bufferedInput;
}

// ****************************************************************************
// Method: Xfer::GetNextGuido
//
// Purpose: 
//   Returns the next unique object identifier.
//
// Returns:    The index of the next object.
//
// Programmer: Brad Whitlock
// Creation:   Thu Jul 13 11:16:23 PDT 2000
//
// Modifications:
//   
// ****************************************************************************

int
Xfer::GetNextGuido()
{
    return nextGuido++;
}

// ****************************************************************************
// Method: Xfer::CreateNewSpecialOpcode
//
// Purpose: 
//   Returns a new special opcode.
//
// Returns:    A new special opcode.
//
// Programmer: Brad Whitlock
// Creation:   Thu Feb 27 11:13:28 PDT 2003
//
// Modifications:
//   
// ****************************************************************************

int
Xfer::CreateNewSpecialOpcode()
{
    return nextSpecialOpcode--;
}

// ****************************************************************************
// Method: Xfer::SendSpecialOpcode
//
// Purpose: 
//   Sends a special opcode to the remote process.
//
// Arguments:
//   opcode : The opcode that will be sent.
//
// Programmer: Brad Whitlock
// Creation:   Thu Feb 27 11:14:01 PDT 2003
//
// Modifications:
//   
// ****************************************************************************

void
Xfer::SendSpecialOpcode(int opcode)
{
    if(opcode < -1 && opcode > nextSpecialOpcode)
    {
        output->WriteInt(opcode);
        output->WriteInt(0);
        output->Flush();
    }
    else
    {
        debug1 << "Special opcode is out of range!";
    }
}

// ****************************************************************************
// Method: Xfer::SetupSpecialOpcodeHandler
//
// Purpose: 
//   Sets up a callback function to handle special opcodes.
//
// Arguments:
//   cb   : The address of the callback function.
//   data : user-defined data that will be passed to the callback.
//
// Programmer: Brad Whitlock
// Creation:   Thu Feb 27 11:14:54 PDT 2003
//
// Modifications:
//   
// ****************************************************************************

void
Xfer::SetupSpecialOpcodeHandler(void (*cb)(int, void *), void *data)
{
    specialOpcodeCallback = cb;
    specialOpcodeCallbackData = data;
}

// ****************************************************************************
// Method: Xfer::GetSubject
//
// Purpose: 
//   Return the i'th subject in the Xfer.
//
// Arguments:
//   index : The index of the object to return.
//
// Returns:    The pointer to the i'th subject or 0.
//
// Note:       This method should almost never need to be used.
//
// Programmer: Brad Whitlock
// Creation:   Fri Dec  7 17:03:55 PST 2007
//
// Modifications:
//   
// ****************************************************************************

const AttributeSubject *
Xfer::GetSubject(int index) const
{
    return (index >= 0 && index < subjectList.size()) ? subjectList[index] : 0;
}

// ****************************************************************************
// Method: Xfer::GetNumSubjects
//
// Purpose: 
//   Return the number of subjects in the Xfer.
//
// Arguments:
//
// Returns:    The number of subjects.
//
// Note:       This method should almost never need to be used.
//
// Programmer: Brad Whitlock
// Creation:   Fri Dec  7 17:03:55 PST 2007
//
// Modifications:
//   
// ****************************************************************************

int
Xfer::GetNumSubjects() const
{
    return subjectList.size();
}