File size: 27,147 Bytes
68ce2fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-    */
/* ex: set filetype=cpp softtabstop=4 shiftwidth=4 tabstop=4 cindent expandtab: */

/*
  Author(s):  Anton Deguet
  Created on: 2010-02-12

  (C) Copyright 2010-2026 Johns Hopkins University (JHU), All Rights Reserved.

--- begin cisst license - do not edit ---

This software is provided "as is" under an open source license, with
no warranty.  The complete license can be found in license.txt and
http://www.cisst.org/cisst/license.txt.

--- end cisst license ---

*/

#include <cisstMultiTask/mtsCollectorEvent.h>

#include <cisstCommon/cmnGenericObjectProxy.h>
#include <cisstCommon/cmnThrow.h>
#include <cisstOSAbstraction/osaGetTime.h>
#include <cisstMultiTask/mtsManagerLocal.h>
#include <cisstMultiTask/mtsInterfaceRequired.h>
#include <cisstMultiTask/mtsInterfaceProvided.h>
#include <iostream>
#include <fstream>


mtsCollectorEvent::CollectorEventVoid::CollectorEventVoid(const std::string & componentName,
                                                          const std::string & interfaceName,
                                                          const std::string & eventName,
                                                          size_t eventId,
                                                          mtsCollectorEvent * collector):
    ComponentName(componentName),
    InterfaceName(interfaceName),
    EventName(eventName),
    EventId(eventId),
    Collector(collector)
{
}


void mtsCollectorEvent::CollectorEventVoid::EventHandler(void)
{
    this->Collector->SaveEventVoid(this);
}


void mtsCollectorEvent::CollectorEventVoid::PrintHeader(std::ostream & outputStream, const CollectorFileFormat CMN_UNUSED(fileFormat))
{
    outputStream << "# TimeEventReceived" << this->Collector->Delimiter
                 << " Id: " << this->EventId
                 << " Event void: \"" << this->ComponentName << "::" << this->InterfaceName << "::" << this->EventName << "\""
                 << std::endl;
}


mtsCollectorEvent::CollectorEventWrite::CollectorEventWrite(const std::string & componentName,
                                                            const std::string & interfaceName,
                                                            const std::string & eventName,
                                                            size_t eventId,
                                                            mtsCollectorEvent * collector):
    ComponentName(componentName),
    InterfaceName(interfaceName),
    EventName(eventName),
    InterfaceRequired(0),
    ArgumentPrototype(0),
    EventId(eventId),
    Collector(collector)
{
}


void mtsCollectorEvent::CollectorEventWrite::EventHandler(const mtsGenericObject & payload)
{
    this->Collector->SaveEventWrite(this, payload);
}


void mtsCollectorEvent::CollectorEventWrite::PrintHeader(std::ostream & outputStream, const CollectorFileFormat CMN_UNUSED(fileFormat))
{
    CMN_ASSERT(this->ArgumentPrototype);
    outputStream << "# TimeEventReceived" << this->Collector->Delimiter
                 << " Id: " << this->EventId
                 << " Event write: \"" << this->ComponentName << "::" << this->InterfaceName << "::" << this->EventName
                 << "(" << this->ArgumentPrototype->Services()->GetName() << ")\"" << this->Collector->Delimiter;
    this->ArgumentPrototype->ToStreamRaw(outputStream, this->Collector->Delimiter, true);
    outputStream << std::endl;
}


mtsCollectorEvent::mtsCollectorEvent(const std::string & collectorName,
                                     const CollectorFileFormat fileFormat):
    mtsCollectorBase(collectorName, fileFormat),
    ObservedComponents("ObservedComponents"),
    EventCounter(1),
    Collecting(false),
    ScheduledStartTime(0.0),
    ScheduledStopTime(0.0),
    TimeServer(0)
{
    this->SetOutputToDefault(fileFormat);
    this->ObservedComponents.SetOwner(*this);
    this->TimeServer = &(mtsManagerLocal::GetInstance()->GetTimeServer());
}


mtsCollectorEvent::~mtsCollectorEvent()
{
}


bool mtsCollectorEvent::CheckCollectingStatus(void)
{
    // get the current time and check agains scheduled start/stop time
    const double currentTime = this->TimeServer->GetRelativeTime();
    if (this->Collecting) {
        // check if we have been collecting long enough to send a progress info (number of samples)
        if ((currentTime - this->TimeOfLastProgressEvent) >= this->TimeIntervalForProgressEvent) {
            this->ProgressEventTrigger(size_t(this->SampleCounterForEvent));
            this->SampleCounterForEvent = 0;
            this->TimeOfLastProgressEvent = currentTime;
        }
        // now check if we are supposed to stop
        if ((this->ScheduledStopTime != 0.0) // stop time is set
            && (currentTime >= this->ScheduledStopTime)) {
            this->SetCollecting(false, currentTime);
            CMN_LOG_CLASS_RUN_DEBUG << "CheckCollectingStatus: stopping collection at: " << currentTime << std::endl;
        }
    } else {
        // we are not collecting yet, check if we should start
        if ((this->ScheduledStartTime != 0.0)
            && (currentTime >= this->ScheduledStartTime)) {
            this->SetCollecting(true, currentTime);
            CMN_LOG_CLASS_RUN_DEBUG << "CheckCollectingStatus: starting collection at: " << currentTime << std::endl;
        }
    }
    return this->Collecting;
}


mtsInterfaceRequired * mtsCollectorEvent::GetInterfaceRequiredFor(const mtsComponent * componentPointer,
                                                                  const mtsInterfaceProvided * interfacePointer)
{
    CMN_ASSERT(componentPointer);
    CMN_ASSERT(interfacePointer);
    // check if this component is already "registered"
    InterfacesRequiredMap * interfacesRequired = this->ObservedComponents.GetItem(componentPointer->GetName(), CMN_LOG_LEVEL_INIT_DEBUG);
    if (!interfacesRequired) {
        CMN_LOG_CLASS_INIT_DEBUG << "GetInterfaceRequiredFor: create required interfaces for component \""
                                 << componentPointer->GetName() << "\"" << std::endl;
        interfacesRequired = new InterfacesRequiredMap(componentPointer->GetName());
        interfacesRequired->SetOwner(*this);
        this->ObservedComponents.AddItem(componentPointer->GetName(), interfacesRequired);
    } else {
        CMN_LOG_CLASS_INIT_DEBUG << "GetInterfaceRequiredFor: found required interfaces for component \""
                                 << componentPointer->GetName() << "\"" << std::endl;
    }
    // check if the component already has the required interface needed
    mtsInterfaceRequired * interfaceRequired = interfacesRequired->GetItem(interfacePointer->GetName(), CMN_LOG_LEVEL_INIT_DEBUG);
    if (!interfaceRequired) {
        CMN_LOG_CLASS_INIT_DEBUG << "GetInterfaceRequiredFor: create required interface to collect events from interface \""
                                 << interfacePointer->GetName() << "\"" << std::endl;
        std::string interfaceRequiredName = componentPointer->GetName() + "::" + interfacePointer->GetName();
        interfaceRequired = this->AddInterfaceRequiredWithoutSystemEventHandlers(interfaceRequiredName);
        if (interfaceRequired) {
            interfacesRequired->AddItem(interfacePointer->GetName(), interfaceRequired);
            CMN_LOG_CLASS_INIT_DEBUG << "GetInterfaceRequiredFor: created required interface \""
                                     << interfaceRequiredName << "\"" << std::endl;
        } else {
            CMN_LOG_CLASS_INIT_ERROR << "GetInterfaceRequiredFor: failed to create required interface \""
                                     << interfaceRequiredName << "\"" << std::endl;
        }
    } else {
        CMN_LOG_CLASS_INIT_DEBUG << "GetInterfaceRequiredFor: found required interface for interface \""
                                 << interfacePointer->GetName() << "\"" << std::endl;
    }
    return interfaceRequired;
}


void mtsCollectorEvent::Startup(void)
{
    CMN_LOG_CLASS_INIT_DEBUG << "Startup() for collector \"" << this->GetName() << "\"" << std::endl;
    size_t index;
    CollectorEventWrite * collectorEvent;
    mtsCommandWriteBase * eventHandler;
    const mtsGenericObject * argumentPrototype;
    for (index = 0; index < this->EventsWrite.size(); index++) {
        collectorEvent = this->EventsWrite[index];
        CMN_ASSERT(collectorEvent->InterfaceRequired);
        eventHandler = collectorEvent->InterfaceRequired->GetEventHandlerWrite(collectorEvent->EventName);
        CMN_ASSERT(eventHandler);
        argumentPrototype = eventHandler->GetArgumentPrototype();
        CMN_ASSERT(argumentPrototype);
        collectorEvent->ArgumentPrototype = dynamic_cast<const mtsGenericObject *>(argumentPrototype->Services()->Create());
        CMN_ASSERT(collectorEvent->ArgumentPrototype);
    }
}


void mtsCollectorEvent::Run(void)
{
    CMN_LOG_CLASS_RUN_DEBUG << "Run for collector \"" << this->GetName() << "\"" << std::endl;
    ProcessQueuedCommands();
    ProcessQueuedEvents();
}


std::string mtsCollectorEvent::GetDefaultOutputName(void)
{
    std::string currentDateTime;
    osaGetDateTimeString(currentDateTime);
    std::string fileName =
        "EventCollection-" + this->GetName() + "-" + currentDateTime;
    return fileName;
}


mtsComponent * mtsCollectorEvent::CheckComponent(const std::string & componentName) const
{
    // todo, replace with GetComponent when available
    mtsComponent * componentPointer = GetManagerComponentServices()->ComponentGet(componentName);
    if (!componentPointer) {
        CMN_LOG_CLASS_INIT_ERROR << "component \"" << componentName
                                 << "\" not found in component manager for collector \""
                                 << this->GetName() << "\". Available components "
                                 << cmnDataVectorHumanReadable(GetManagerComponentServices()->GetNamesOfComponents()) << std::endl;
    }
    return componentPointer;
}


mtsInterfaceProvided * mtsCollectorEvent::CheckInterface(const mtsComponent * componentPointer,
                                                         const std::string & interfaceName) const
{
    CMN_ASSERT(componentPointer);
    mtsInterfaceProvided * interfacePointer = componentPointer->GetInterfaceProvided(interfaceName);
    if (!interfacePointer) {
        CMN_LOG_CLASS_INIT_ERROR << "interface \"" << interfaceName
                                 << "\" not found in component \"" << componentPointer->GetName()
                                 << "\" for collector \"" << this->GetName() << "\".  Available interfaces "
                                 << cmnDataVectorHumanReadable(componentPointer->GetNamesOfInterfacesProvided()) << std::endl;
    }
    return interfacePointer;
}


bool mtsCollectorEvent::AddObservedComponent(const std::string & componentName)
{
    // check if the component exists
    mtsComponent * componentPointer = this->CheckComponent(componentName);
    if (!componentPointer) {
        return false;
    }
    return this->AddObservedComponent(componentPointer);
}


bool mtsCollectorEvent::AddObservedComponent(const mtsComponent * componentPointer)
{
    CMN_ASSERT(componentPointer);
    // check if this task has already been connected
    if (this->ConnectedFlag) {
        CMN_LOG_CLASS_INIT_ERROR << "AddObservedComponent: collector \"" << this->GetName()
                                 << "\" is already connected, you cannot add an observed component" << std::endl;
        return false;
    }
    CMN_LOG_CLASS_INIT_DEBUG << "AddObservedComponent: adding component \""
                             << componentPointer->GetName() << "\"" << std::endl;
    // get all provided interface names
    typedef std::vector<std::string> VectorOfNames;
    typedef VectorOfNames::const_iterator NameIterator;
    VectorOfNames providedInterfaces = componentPointer->GetNamesOfInterfacesProvided();
    const NameIterator end = providedInterfaces.end();
    mtsInterfaceProvided * interfacePointer;
    NameIterator iterator;
    for (iterator = providedInterfaces.begin();
         iterator != end;
         ++iterator) {
        interfacePointer = componentPointer->GetInterfaceProvided(*iterator);
        this->AddObservedInterface(componentPointer, interfacePointer);
    }
    return true;
}


bool mtsCollectorEvent::AddObservedInterface(const std::string & componentName,
                                             const std::string & interfaceName)
{
    // check if the component exists
    mtsComponent * componentPointer = this->CheckComponent(componentName);
    if (!componentPointer) {
        return false;
    }
    // check if the interface exists
    mtsInterfaceProvided * interfacePointer = this->CheckInterface(componentPointer, interfaceName);
    if (!interfacePointer) {
        return false;
    }
    return this->AddObservedInterface(componentPointer, interfacePointer);
}


bool mtsCollectorEvent::AddObservedInterface(const mtsComponent * componentPointer,
                                             const mtsInterfaceProvided * interfacePointer)
{
    // check if this task has already been connected
    if (this->ConnectedFlag) {
        CMN_LOG_CLASS_INIT_ERROR << "AddObservedInterface: collector \"" << this->GetName()
                                 << "\" is already connected, you cannot add an observed interface" << std::endl;
        return false;
    }
    CMN_ASSERT(componentPointer);
    CMN_ASSERT(interfacePointer);
    CMN_LOG_CLASS_INIT_DEBUG << "AddObservedInterface: adding interface \""
                             << componentPointer->GetName() << "."
                             << interfacePointer->GetName() << "\"" << std::endl;
    // get all event names
    typedef std::vector<std::string> VectorOfNames;
    typedef VectorOfNames::const_iterator NameIterator;
    // void events
    VectorOfNames eventNames = interfacePointer->GetNamesOfEventsVoid();
    const NameIterator endVoid = eventNames.end();
    NameIterator iterator;
    for (iterator = eventNames.begin();
         iterator != endVoid;
         ++iterator) {
        this->AddObservedEventVoid(componentPointer, interfacePointer, *iterator);
    }
    // write events
    eventNames = interfacePointer->GetNamesOfEventsWrite();
    const NameIterator endWrite = eventNames.end();
    for (iterator = eventNames.begin();
         iterator != endWrite;
         ++iterator) {
        this->AddObservedEventWrite(componentPointer, interfacePointer, *iterator);
    }
    return true;
}


bool mtsCollectorEvent::AddObservedEventVoid(const std::string & componentName,
                                             const std::string & interfaceName,
                                             const std::string & eventName)
{
    // check if the component exists
    mtsComponent * componentPointer = this->CheckComponent(componentName);
    if (!componentPointer) {
        return false;
    }
    // check if the interface exists
    mtsInterfaceProvided * interfacePointer = this->CheckInterface(componentPointer, interfaceName);
    if (!interfacePointer) {
        return false;
    }
    return this->AddObservedEventVoid(componentPointer, interfacePointer, eventName);
}


bool mtsCollectorEvent::AddObservedEventWrite(const std::string & componentName,
                                              const std::string & interfaceName,
                                              const std::string & eventName)
{
    // check if the component exists
    mtsComponent * componentPointer = this->CheckComponent(componentName);
    if (!componentPointer) {
        return false;
    }
    // check if the interface exists
    mtsInterfaceProvided * interfacePointer = this->CheckInterface(componentPointer, interfaceName);
    if (!interfacePointer) {
        return false;
    }
    return this->AddObservedEventWrite(componentPointer, interfacePointer, eventName);
}


bool mtsCollectorEvent::AddObservedEventVoid(const mtsComponent * componentPointer,
                                             const mtsInterfaceProvided * interfacePointer,
                                             const std::string & eventName)
{
    // check if this task has already been connected
    if (this->ConnectedFlag) {
        CMN_LOG_CLASS_INIT_ERROR << "AddObservedEventVoid: collector \"" << this->GetName()
                                 << "\" is already connected, you cannot add an observed event" << std::endl;
        return false;
    }
    CMN_ASSERT(componentPointer);
    CMN_ASSERT(interfacePointer);
    const auto events = interfacePointer->GetNamesOfEventsVoid();
    const auto found = std::find(events.begin(), events.end(), eventName);
    if (found == events.end()) {
        CMN_LOG_CLASS_INIT_ERROR << "AddObservedEventVoid: can't find event " << eventName
                                 << " for component " << componentPointer->GetName() << ", interface "
                                 << interfacePointer->GetName() << ".  Available events "
                                 << cmnDataVectorHumanReadable(events) << std::endl;
        return false;
    }
    CMN_LOG_CLASS_INIT_DEBUG << "AddObservedEventVoid: adding event     \"" // extra spaces added for alignment
                             << componentPointer->GetName() << "."
                             << interfacePointer->GetName() << "."
                             << eventName << "\"" << std::endl;
    // create a CollectorEventvoid object
    CollectorEventVoid * collector = new CollectorEventVoid(componentPointer->GetName(), interfacePointer->GetName(), eventName,
                                                            this->EventCounter, this);
    this->EventsVoid.push_back(collector);
    this->EventCounter++;
    // get the required interface to add an observer
    mtsInterfaceRequired * interfaceRequired = GetInterfaceRequiredFor(componentPointer, interfacePointer);
    interfaceRequired->AddEventHandlerVoid(&CollectorEventVoid::EventHandler, collector, eventName);
    return true;
}


bool mtsCollectorEvent::AddObservedEventWrite(const mtsComponent * componentPointer,
                                              const mtsInterfaceProvided * interfacePointer,
                                              const std::string & eventName)
{
    // check if this task has already been connected
    if (this->ConnectedFlag) {
        CMN_LOG_CLASS_INIT_ERROR << "AddObservedEventWrite: collector \"" << this->GetName()
                                 << "\" is already connected, you cannot add an observed event" << std::endl;
        return false;
    }
    CMN_ASSERT(componentPointer);
    CMN_ASSERT(interfacePointer);
    const auto events = interfacePointer->GetNamesOfEventsWrite();
    const auto found = std::find(events.begin(), events.end(), eventName);
    if (found == events.end()) {
        CMN_LOG_CLASS_INIT_ERROR << "AddObservedEventWrite: can't find event " << eventName
                                 << " for component " << componentPointer->GetName() << ", interface "
                                 << interfacePointer->GetName() << ".  Available events "
                                 << cmnDataVectorHumanReadable(events) << std::endl;
        return false;
    }
    CMN_LOG_CLASS_INIT_DEBUG << "AddObservedEventWrite: adding event    \"" // extra spaces added for alignment
                             << componentPointer->GetName() << "."
                             << interfacePointer->GetName() << "."
                             << eventName << "\"" << std::endl;
    // create a CollectorEventWrite object
    CollectorEventWrite * collector = new CollectorEventWrite(componentPointer->GetName(), interfacePointer->GetName(), eventName,
                                                              this->EventCounter, this);
    this->EventsWrite.push_back(collector);
    this->EventCounter++;
    // get the required interface to add an observer
    mtsInterfaceRequired * interfaceRequired = GetInterfaceRequiredFor(componentPointer, interfacePointer);
    interfaceRequired->AddEventHandlerWriteGeneric(&CollectorEventWrite::EventHandler, collector, eventName);
    // collector needs to have the required interface to find out the argument prototype at Startup
    collector->InterfaceRequired = interfaceRequired;
    return true;
}


bool mtsCollectorEvent::Connect(void)
{
    // iterate all components and interfaces to connect all
    const ComponentsMap::const_iterator endComponents = this->ObservedComponents.end();
    ComponentsMap::const_iterator iterComponents;
    InterfacesRequiredMap::const_iterator endInterfaces, iterInterfaces;
    for (iterComponents = this->ObservedComponents.begin();
         iterComponents != endComponents;
         iterComponents++) {
        // iterComponents->first is the component name
        // iterComponents->second is a map of interfaces
        endInterfaces = iterComponents->second->end();
        for (iterInterfaces = iterComponents->second->begin();
             iterInterfaces != endInterfaces;
             iterInterfaces++) {
            CMN_LOG_CLASS_INIT_DEBUG << "Connect: connecting required interface \""
                                     << this->GetName() << "::" << iterInterfaces->second->GetName()
                                     << "\" to \""
                                     << iterComponents->first << "::" << iterInterfaces->first
                                     << "\"" << std::endl;
            if (GetManagerComponentServices()->Connect(this->GetName(), iterInterfaces->second->GetName(),
                                                       iterComponents->first, iterInterfaces->first)) {
                CMN_LOG_CLASS_INIT_ERROR << "Connect: connect failed for required interface \""
                                         << this->GetName() << "::" << iterInterfaces->second->GetName()
                                         << "\" to \""
                                         << iterComponents->first << "::" << iterInterfaces->first
                                         << "\"" << std::endl;
                return false;
            }
        }
    }
    this->ConnectedFlag = true;
    return true;
}


void mtsCollectorEvent::SaveEventVoid(const CollectorEventVoid * event)
{
    // check if collection is turned on
    if (this->CheckCollectingStatus()) {
        // if this method is called for the first time, print out some information.
        if (this->FirstRunningFlag) {
            this->OpenFileIfNeeded();
            this->PrintHeader(this->FileFormat);
        }
        *(this->OutputStream) << mtsManagerLocal::GetInstance()->GetTimeServer().GetRelativeTime()
                              << this->Delimiter << event->EventId << std::endl;
        this->SampleCounter++;
        this->SampleCounterForEvent++;
    }
}


void mtsCollectorEvent::SaveEventWrite(const CollectorEventWrite * event, const mtsGenericObject & payload)
{
    // check if collection is turned on
    if (this->CheckCollectingStatus()) {
        // if this method is called for the first time, print out some information.
        if (this->FirstRunningFlag) {
            this->OpenFileIfNeeded();
            this->PrintHeader(this->FileFormat);
        }
        *(this->OutputStream) << mtsManagerLocal::GetInstance()->GetTimeServer().GetRelativeTime()
                              << this->Delimiter << event->EventId << this->Delimiter;
        payload.ToStreamRaw(*(this->OutputStream), this->Delimiter);
        *(this->OutputStream) << std::endl;
        this->SampleCounter++;
        this->SampleCounterForEvent++;
    }
}


void mtsCollectorEvent::PrintHeader(const CollectorFileFormat & fileFormat)
{
    std::string currentDateTime;
    osaGetDateTimeString(currentDateTime);

    if (this->OutputStream) {
        // Print out some information on the state table.

        // All lines in the header should be preceded by '#' which represents
        // the line contains header information rather than collected data.
        *(this->OutputStream) << "# Date & time        : " << currentDateTime << std::endl;
        *(this->OutputStream) << "# Total event count : " << (this->EventCounter - 1) << std::endl;
        *(this->OutputStream) << "# Data format        : ";
        if (fileFormat == COLLECTOR_FILE_FORMAT_PLAIN_TEXT) {
            *(this->OutputStream) << "Text";
        } else if (fileFormat == COLLECTOR_FILE_FORMAT_CSV) {
            *(this->OutputStream) << "Text (CSV)";
        } else {
            *(this->OutputStream) << "Binary";
        }
        *(this->OutputStream) << std::endl;
        *(this->OutputStream) << "#" << std::endl;

        size_t index;
        for (index = 0; index < this->EventsVoid.size(); index++) {
            (this->EventsVoid[index])->PrintHeader(*(this->OutputStream), fileFormat);
        }
        for (index = 0; index < this->EventsWrite.size(); index++) {
            (this->EventsWrite[index])->PrintHeader(*(this->OutputStream), fileFormat);
        }

        // In case of using binary format
        if (fileFormat == COLLECTOR_FILE_FORMAT_BINARY) {
            CMN_LOG_CLASS_INIT_ERROR << "PrintHeader: binary format not supported yet" << std::endl;
        }
    } else {
        CMN_LOG_CLASS_RUN_ERROR << "PrintHeader: output stream for collector \""
                                << this->GetName() << "\" is not available." << std::endl;
    }
    this->FirstRunningFlag = false;
}


void mtsCollectorEvent::StartCollection(const double & delay)
{
    const double currentTime = this->TimeServer->GetRelativeTime();
    if (delay == 0.0) {
        this->SetCollecting(true, currentTime);
        CMN_LOG_CLASS_RUN_DEBUG << "StartCollection: starting collection now (" << currentTime << ")" << std::endl;
    } else {
        this->ScheduledStartTime = currentTime + delay;
        CMN_LOG_CLASS_RUN_DEBUG << "StartCollection: collection scheduled to start at " << this->ScheduledStartTime << std::endl;
    }
}


void mtsCollectorEvent::StopCollection(const double & delay)
{
    const double currentTime = this->TimeServer->GetRelativeTime();
    if (delay == 0.0) {
        this->SetCollecting(false, currentTime);
        CMN_LOG_CLASS_RUN_DEBUG << "StopCollection: stopping collection now (" << currentTime << ")" << std::endl;
    } else {
        this->ScheduledStopTime = currentTime + delay;
        CMN_LOG_CLASS_RUN_DEBUG << "StopCollection: collection scheduled to stop at " << this->ScheduledStopTime << std::endl;
    }
}


void mtsCollectorEvent::SetCollecting(bool collecting, double currentTime)
{
    this->Collecting = collecting;
    if (this->Collecting) {
        // start collecting
        this->CollectionStartedEventTrigger(true);
        this->ScheduledStartTime = 0.0;
        this->TimeOfLastProgressEvent = currentTime;
    } else {
        // stop collecting
        this->ProgressEventTrigger(size_t(this->SampleCounterForEvent));
        this->CollectionStartedEventTrigger(false);
        this->ScheduledStopTime = 0.0;
        this->SampleCounterForEvent = 0;
    }
}