File size: 4,363 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
/* -*- 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: 2009-08-10

  (C) Copyright 2009-2019 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 <cisstCommon/cmnConstants.h>
#include <cisstMultiTask/mtsInterfaceProvided.h>
#include <cisstMultiTask/mtsManagerLocal.h>

#include "serverTask.h"

// required to implement the class services, see cisstCommon
CMN_IMPLEMENT_SERVICES(serverTask);

serverTask::serverTask(const std::string & taskName, double period):
    // base constructor, same task name and period.  Set the length of
    // state table to 5000
    mtsTaskPeriodic(taskName, period, false, 5000),
    BenchmarkDoneMember(false),
    NumberOfskippedElement(0),
    SamplesCollected(0)
{
    // add ServerData to the StateTable defined in mtsTask
    this->StateTable.AddData(ReadValue, "ReadValue");
    // add one interface, this will create an mtsTaskInterface
    mtsInterfaceProvided * provided = AddInterfaceProvided("Provided1");
    if (provided) {
        provided->AddCommandWrite(&serverTask::Write, this, "Write");
        provided->AddCommandReadState(this->StateTable, this->ReadValue, "Read");
    }
    provided = AddInterfaceProvided("Provided2");
    if (provided) {
        provided->AddCommandWrite(&serverTask::TriggerEvent, this, "TriggerEvent");
        provided->AddEventWrite(this->EventWrite, "EventWrite", value_type());
    }

    // Get a pointer to the time server
    this->TimeServer = &mtsManagerLocal::GetInstance()->GetTimeServer();

    // Make sure we timestamp ourselves
    this->ReadValue.SetAutomaticTimestamp(false);

    // Allocates space for samples
    this->Samples.SetSize(confNumberOfSamples);
}


void serverTask::Write(const value_type & data)
{
    CMN_LOG_CLASS_RUN_VERBOSE << "Write" << std::endl;
    if (this->BenchmarkDoneMember == false) {
        if (this->NumberOfskippedElement < confNumberOfSamplesToSkip) {
            this->NumberOfskippedElement++;
        } else {
            double originalTime = data.Timestamp();
            double currentTime = TimeServer->GetRelativeTime();
            this->Samples.Element(this->SamplesCollected) = (currentTime - originalTime);
            this->SamplesCollected++;
        }
    }
    if (this->SamplesCollected == confNumberOfSamples) {
        this->BenchmarkDoneMember = true;
        double average = Samples.SumOfElements() / Samples.size();
        double min = 0.0;
        double max = 0.0;
        Samples.MinAndMaxElement(min, max);
        std::cout << "scs: server->client->server: client read (previous cycle data) and writes back (queued)" << std::endl
                  << "scs: Client period (ms): " << cmnInternalTo_ms(confClientPeriod) << std::endl
                  << "scs: Server period (ms): " << cmnInternalTo_ms(confServerPeriod) << std::endl
                  << "scs: Size of elements used (in bytes): " << sizeof(value_type) << std::endl
                  << "scs: Number of samples: " << this->SamplesCollected << std::endl
                  << "scs: Average (ms): " << cmnInternalTo_ms(average) << std::endl
                  << "scs: Standard deviation (ms): " << cmnInternalTo_ms(StandardDeviation(this->Samples)) << std::endl
                  << "scs: Min (ms): " << cmnInternalTo_ms(min) << std::endl
                  << "scs: Max (ms): " << cmnInternalTo_ms(max) << std::endl;
        this->SamplesCollected++; // just to avoid printing results again
    }
}


void serverTask::TriggerEvent(const value_type & data)
{
    CMN_LOG_CLASS_RUN_VERBOSE << "TriggerEvent" << std::endl;
    // send data back to measure loop time
    this->EventWrite(data);
}


void serverTask::Startup(void)
{
}

void serverTask::Run(void)
{
    ProcessQueuedCommands();

    // at the end, timestamp the ReadValue
    double time;
    time = this->TimeServer->GetRelativeTime();
    this->ReadValue.SetTimestamp(time);
}


bool serverTask::BenchmarkDone(void) const
{
    return this->BenchmarkDoneMember;
}