CentralManagerService.cpp 6.92 KB
Newer Older
1
//    Copyright (C) 2017, ETH Zurich, D-ITET, Cyrill Burgener, Marco Mueller, Philipp Friedli
bucyril's avatar
bucyril committed
2
//
3
4
5
//    This file is part of D-FaLL-System.
//    
//    D-FaLL-System is free software: you can redistribute it and/or modify
bucyril's avatar
bucyril committed
6
7
8
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
9
10
//    
//    D-FaLL-System is distributed in the hope that it will be useful,
bucyril's avatar
bucyril committed
11
12
13
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
14
//    
bucyril's avatar
bucyril committed
15
//    You should have received a copy of the GNU General Public License
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
//    along with D-FaLL-System.  If not, see <http://www.gnu.org/licenses/>.
//    
//
//    ----------------------------------------------------------------------------------
//    DDDD        FFFFF        L     L           SSSS  Y   Y   SSSS  TTTTT  EEEEE  M   M
//    D   D       F      aaa   L     L          S       Y Y   S        T    E      MM MM
//    D   D  ---  FFFF  a   a  L     L     ---   SSS     Y     SSS     T    EEE    M M M
//    D   D       F     a  aa  L     L              S    Y        S    T    E      M   M
//    DDDD        F      aa a  LLLL  LLLL       SSSS     Y    SSSS     T    EEEEE  M   M
//
//
//    DESCRIPTION:
//    The service that manages the context of the student groups.
//
//    ----------------------------------------------------------------------------------

bucyril's avatar
bucyril committed
32

33
34
35



Paul Beuchat's avatar
Paul Beuchat committed
36
// INCLUDE THE HEADER
37
#include "nodes/CentralManagerService.h"
bucyril's avatar
bucyril committed
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




//    ----------------------------------------------------------------------------------
//    FFFFF  U   U  N   N   CCCC  TTTTT  III   OOO   N   N
//    F      U   U  NN  N  C        T     I   O   O  NN  N
//    FFF    U   U  N N N  C        T     I   O   O  N N N
//    F      U   U  N  NN  C        T     I   O   O  N  NN
//    F       UUU   N   N   CCCC    T    III   OOO   N   N
//
//    III M   M PPPP  L     EEEEE M   M EEEEE N   N TTTTT   A   TTTTT III  OOO  N   N
//     I  MM MM P   P L     E     MM MM E     NN  N   T    A A    T    I  O   O NN  N
//     I  M M M PPPP  L     EEE   M M M EEE   N N N   T   A   A   T    I  O   O N N N
//     I  M   M P     L     E     M   M E     N  NN   T   AAAAA   T    I  O   O N  NN
//    III M   M P     LLLLL EEEEE M   M EEEEE N   N   T   A   A   T   III  OOO  N   N
//    ----------------------------------------------------------------------------------





//    ----------------------------------------------------------------------------------
//    DDDD     A    TTTTT    A    BBBB     A     SSSS  EEEEE
//    D   D   A A     T     A A   B   B   A A   S      E
//    D   D  A   A    T    A   A  BBBB   A   A   SSS   EEE
//    D   D  AAAAA    T    AAAAA  B   B  AAAAA      S  E
//    DDDD   A   A    T    A   A  BBBB   A   A  SSSS   EEEEE
//    ----------------------------------------------------------------------------------

bool cmRead(CMRead::Request &request, CMRead::Response &response)
{
71
    response.crazyflieDB = crazyflieDB;
phfriedl's avatar
phfriedl committed
72
73
	return true;
}
bucyril's avatar
bucyril committed
74

75
76
77
78
int findEntryByStudID(unsigned int studID)
{
    for(int i = 0; i < crazyflieDB.crazyflieEntries.size(); i++)
    {
79
        CrazyflieEntry entry = crazyflieDB.crazyflieEntries[i];
80
81
        if(entry.studentID == studID)
        {
82
83
84
85
86
87
            return i;
        }
    }
    return -1;
}

88
89
bool cmQuery(CMQuery::Request &request, CMQuery::Response &response)
{
90
    int cfIndex = findEntryByStudID(request.studentID);
91
92
    if(cfIndex != -1)
    {
93
94
        response.crazyflieContext = crazyflieDB.crazyflieEntries[cfIndex].crazyflieContext;
        return true;
95
96
97
    }
    else
    {
98
99
100
        return false;
    }
}
bucyril's avatar
bucyril committed
101

102
103
104
105
int findEntryByCF(string name)
{
    for(int i = 0; i < crazyflieDB.crazyflieEntries.size(); i++)
    {
106
107
        CrazyflieEntry entry = crazyflieDB.crazyflieEntries[i];
        string cfName = entry.crazyflieContext.crazyflieName;
108
109
        if(cfName == name)
        {
110
111
112
113
            return i;
        }
    }
    return -1;
114
}
bucyril's avatar
bucyril committed
115

116
117
118
119
120
121
bool cmUpdate(CMUpdate::Request &request, CMUpdate::Response &response)
{
    switch(request.mode)
    {
        case ENTRY_INSERT_OR_UPDATE:
        {
122
123
            string cfName = request.crazyflieEntry.crazyflieContext.crazyflieName;
            int cfIndex = findEntryByCF(cfName);
124
125
            if(cfIndex == -1)
            {
126
                crazyflieDB.crazyflieEntries.push_back(request.crazyflieEntry);
127
128
129
            }
            else
            {
130
131
132
133
134
135
136
137
                crazyflieDB.crazyflieEntries[cfIndex] = request.crazyflieEntry;
            }
            return true;
        }

        case ENTRY_REMOVE: {
            string cfName = request.crazyflieEntry.crazyflieContext.crazyflieName;
            int cfIndex = findEntryByCF(cfName);
138
139
            if(cfIndex == -1)
            {
140
                return false;
141
142
143
            }
            else
            {
144
145
146
147
148
149
150
                crazyflieDB.crazyflieEntries.erase(crazyflieDB.crazyflieEntries.begin() +cfIndex);
                return true;
            }
        }

        default: return false;
    }
151
152
}

153
154
155
156
157
158
bool cmCommand(CMCommand::Request &request, CMCommand::Response &response)
{
    switch(request.command)
    {
        case CMD_SAVE:
        {
bucyril's avatar
bucyril committed
159
            writeCrazyflieDB(crazyflieDB);
160
161
162
            return true;
        }

163
164
        case CMD_RELOAD:
        {
bucyril's avatar
bucyril committed
165
166
            crazyflieDB.crazyflieEntries.clear();
            readCrazyflieDB(crazyflieDB);
167
168
169
170
171
            return true;
        }

        default: return false;
    }
172
}
bucyril's avatar
bucyril committed
173

174
175
176
177
178
179
180
181
182
183
184
185
186



//    ----------------------------------------------------------------------------------
//    M   M    A    III  N   N
//    MM MM   A A    I   NN  N
//    M M M  A   A   I   N N N
//    M   M  AAAAA   I   N  NN
//    M   M  A   A  III  N   N
//    ----------------------------------------------------------------------------------

int main(int argc, char* argv[])
{
bucyril's avatar
bucyril committed
187
188
189
    ros::init(argc, argv, "CentralManagerService");

    ros::NodeHandle nodeHandle("~");
bucyril's avatar
bucyril committed
190
191
    
    readCrazyflieDB(crazyflieDB);
bucyril's avatar
bucyril committed
192

193
194
195
196
197
    ros::ServiceServer readService = nodeHandle.advertiseService("Read", cmRead);
    ros::ServiceServer queryService = nodeHandle.advertiseService("Query", cmQuery);
    ros::ServiceServer updateService = nodeHandle.advertiseService("Update", cmUpdate);
    ros::ServiceServer commandService = nodeHandle.advertiseService("Command", cmCommand);

198
199
200
201
202
203
204
205
206
207
    // Get the handle to the namespace in which this coordinator is launched
    //ros::NodeHandle namespaceNodeHandle = ros::NodeHandle();

    // Subscriber for requests that the controller parameters should be re-loaded from
    // the .YAML files on the coordinators machine, and then all the agents should be
    // request to fetch the parameters from itself, i.e., fetch parameters from the
    // coordinator.
    //ros::Subscriber controllerYamlReadyForFetchSubscriber = namespaceNodeHandle.subscribe("/ParameterService/controllerYamlReadyForFetch", 1, controllerYamlReadyForFetchCallback);


bucyril's avatar
bucyril committed
208
209
210
211
212
    ROS_INFO("CentralManagerService ready");
    ros::spin();

    return 0;
}