summaryrefslogtreecommitdiff
path: root/SDL_Core/src/components/AppMgr/src/CommandMapping.cpp
blob: 25e00ebf2a9fcc536878abfdfa676bf24b838a16 (plain)
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
//
// Copyright (c) 2013, Ford Motor Company
// All rights reserved.
//
// 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 following disclaimer.
//
// Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following
// disclaimer in the documentation and/or other materials provided with the
// distribution.
//
// Neither the name of the Ford Motor Company 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 THE COPYRIGHT HOLDER 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 "AppMgr/CommandMapping.h"
#include "AppMgr/RegistryItem.h"
#include "LoggerHelper.hpp"

namespace NsAppManager
{

    log4cplus::Logger CommandMapping::mLogger = log4cplus::Logger::getInstance(LOG4CPLUS_TEXT("CommandMapping"));

    /**
     * \brief Default class constructor
     */
    CommandMapping::CommandMapping()
    {
    }

    /**
     * \brief Default class destructor
     */
    CommandMapping::~CommandMapping()
    {
        clear();
    }

    /**
     * \brief add a command to a mapping
     * \param commandId command id
     * \param type command type
     * \param params VR or UI params supplied with the AddCommand request
     */
    void CommandMapping::addCommand(unsigned int commandId, const CommandType& type, CommandParams params)
    {
        LOG4CPLUS_INFO_EXT(mLogger, "Subscribed to a command " << commandId << " type " << type.getType() );
        mCommands.insert(Command(CommandBase( commandId, type ), params ));
    }

    /**
     * \brief remove a command from a mapping
     * \param commandId command id
     * \param type a type of a command
     */
    void CommandMapping::removeCommand(unsigned int commandId, const CommandType& type)
    {
        LOG4CPLUS_INFO_EXT(mLogger, "Deleting a command " << commandId << " type " << type.getType() );
        mCommands.erase(CommandBase(commandId, type));
    }

    /**
     * \brief finds commands in mapping
     * \param commandId command id
     * \return true if found, false if not
     */
    bool CommandMapping::findCommand(unsigned int commandId, const CommandType& type) const
    {
        return ( mCommands.find(CommandBase(commandId, type)) != mCommands.end() );
    }

    /**
     * \brief finds commands in mapping
     * \param commandId command id
     * \return commands list
     */
    Commands CommandMapping::findCommands(unsigned int commandId) const
    {
        Commands cmds;
        LOG4CPLUS_INFO_EXT(mLogger, "Searching for commands by id " << commandId );
        for(Commands::const_iterator it = mCommands.begin(); it != mCommands.end(); it++)
        {
            const Command& cmd = *it;
            const CommandBase& base = cmd.first;
            const unsigned int& cmdId = std::get<0>(base);
            const CommandType& cmdType = std::get<1>(base);
            if(cmdId == commandId)
            {
                LOG4CPLUS_INFO_EXT(mLogger, "Found a command " << cmdId << " type " << cmdType.getType() );
                cmds.insert(cmd);
            }
        }
        return cmds;
    }

    /**
     * \brief gets all commands
     * \return commands
     */
    Commands CommandMapping::getAllCommands() const
    {
        return mCommands;
    }

    /**
     * \brief retrieve types associated with command id in current mapping
     * \param commandId command id to search for types
     * \return input container of command types to be filled with result
     */
    CommandTypes CommandMapping::getTypes( unsigned int commandId ) const
    {
        CommandTypes types;
        LOG4CPLUS_INFO_EXT(mLogger, "Searching for command types by command id " << commandId );
        for(CommandType type = CommandType::FIRST; type != CommandType::LAST; type++)
        {
            Commands::const_iterator it = mCommands.find( CommandBase(commandId, type) );
            if ( it != mCommands.end() )
            {
                LOG4CPLUS_INFO_EXT(mLogger, "Found a type " << type.getType() );
                types.push_back(type);
            }
        }
        return types;
    }

    /**
     * \brief get count of commands
     * \return commands count
     */
    size_t CommandMapping::size() const
    {
       return  mCommands.size();
    }

    /**
     * \brief get count of unresponsed requests associated with the given command id
     * \param cmdId id of command we need to count unresponded requests for
     * \return unresponded requests count
     */
    unsigned int CommandMapping::getUnrespondedRequestCount(const unsigned int &cmdId) const
    {
        LOG4CPLUS_INFO_EXT(mLogger, "Searching for unresponded requests for command " << cmdId );
        RequestsAwaitingResponse::const_iterator it = mRequestsPerCommand.find(cmdId);
        if(it != mRequestsPerCommand.end())
        {
            LOG4CPLUS_INFO_EXT(mLogger, "Unresponded requests for command " << cmdId << " is " << it->second );
            return it->second;
        }
        LOG4CPLUS_INFO_EXT(mLogger, "No unresponded requests for command " << cmdId << " found! " );
        return 0;
    }

    /**
     * \brief increment count of unresponsed requests associated with the given command id
     * \param cmdId id of command we need to increment unresponded request count for
     * \return unresponded requests count after the operation
     */
    unsigned int CommandMapping::incrementUnrespondedRequestCount(const unsigned int &cmdId)
    {
        LOG4CPLUS_INFO_EXT(mLogger, "Incrementing for unresponded requests for command " << cmdId );
        RequestsAwaitingResponse::iterator it = mRequestsPerCommand.find(cmdId);
        unsigned int reqsCount = it != mRequestsPerCommand.end() ? it->second : 0;
        LOG4CPLUS_INFO_EXT(mLogger, "Unresponded requests for command " << cmdId << " was " << reqsCount );
        reqsCount++;
        if(it != mRequestsPerCommand.end())
        {
            mRequestsPerCommand.erase(it);
        }
        mRequestsPerCommand.insert(RequestAwaitingResponse(cmdId, reqsCount));
        LOG4CPLUS_INFO_EXT(mLogger, "Unresponded requests for command " << cmdId << " became " << reqsCount );
        return reqsCount;
    }

    /**
     * \brief decrement count of unresponsed requests associated with the given command id
     * \param cmdId id of command we need to decrement unresponded request count for
     * \return unresponded requests count after the operation
     */
    unsigned int CommandMapping::decrementUnrespondedRequestCount(const unsigned int &cmdId)
    {
        LOG4CPLUS_INFO_EXT(mLogger, "Decrementing for unresponded requests for command " << cmdId );
        RequestsAwaitingResponse::iterator it = mRequestsPerCommand.find(cmdId);
        if(it != mRequestsPerCommand.end())
        {
            LOG4CPLUS_INFO_EXT(mLogger, "Unresponded requests for command " << cmdId << " was " << it->second );
            if(it->second <= 0)
            {
                LOG4CPLUS_ERROR_EXT(mLogger, "Trying to decrement already null value!" );
                return 0;
            }
            unsigned int reqsCount = it->second - 1;
            mRequestsPerCommand.erase(it);
            mRequestsPerCommand.insert(RequestAwaitingResponse(cmdId, reqsCount));
            LOG4CPLUS_INFO_EXT(mLogger, "Unresponded requests for command " << cmdId << " became " << reqsCount );
            return reqsCount;
        }
        LOG4CPLUS_INFO_EXT(mLogger, "No unresponded requests for command " << cmdId << " found! " );
        return 0;
    }

    /**
     * \brief cleans all the items
     */
    void CommandMapping::clear()
    {
        mCommands.clear();
    }

    /**
     * \brief cleans all the requests awaiting response
     */
    void CommandMapping::clearUnrespondedRequests()
    {
        mRequestsPerCommand.clear();
    }

    /**
     * \brief Copy constructor
     */
    CommandMapping::CommandMapping(const CommandMapping &)
    {
    }

    /**
     * \brief Default constructor
     */
    CommandType::CommandType()
        :mType(CommandType::UNDEFINED)
    {
    }

    /**
     * \brief Copy constructor
     */
    CommandType::CommandType(const CommandType& src)
        :mType(src.getType())
    {
    }

    /**
     * \brief Class constructor
     * \param type command type to create a class with
     */
    CommandType::CommandType(const CommandType::Type& type)
        :mType(type)
    {
    }

    /**
     * \brief comparison operator
     * \param type of a command to compare with
     * \return comparison result
     */
    bool CommandType::operator ==(const CommandType::Type &type) const
    {
        return mType == type;
    }

    /**
     * \brief comparison operator
     * \param type of a command to compare with
     * \return comparison result
     */
    bool CommandType::operator ==(const CommandType &type) const
    {
        return mType == type.getType();
    }

    /**
     * \brief comparison operator
     * \param type of a command to compare with
     * \return comparison result
     */
    bool CommandType::operator <(const CommandType::Type &type) const
    {
        return mType < type;
    }

    /**
     * \brief comparison operator
     * \param type of a command to compare with
     * \return comparison result
     */
    bool CommandType::operator <(const CommandType &type) const
    {
        return mType < type.getType();
    }

    /**
     * \brief comparison operator
     * \param type of a command to compare with
     * \return comparison result
     */
    bool CommandType::operator >(const CommandType::Type &type) const
    {
        return mType > type;
    }

    /**
     * \brief comparison operator
     * \param type of a command to compare with
     * \return comparison result
     */
    bool CommandType::operator >(const CommandType &type) const
    {
        return mType > type.getType();
    }

    /**
     * \brief comparison operator
     * \param type of a command to compare with
     * \return comparison result
     */
    bool CommandType::operator !=(const CommandType::Type &type) const
    {
        return mType != type;
    }

    /**
     * \brief comparison operator
     * \param type of a command to compare with
     * \return comparison result
     */
    bool CommandType::operator !=(const CommandType &type) const
    {
        return mType != type.getType();
    }

    /**
     * \brief pre-increment operator
     * \return incremented value
     */
    CommandType& CommandType::operator ++()
    {
        if(mType != CommandType::LAST)
        {
            int type = mType + 1;
            mType = (CommandType::Type)type;
        }
        return *this;
    }

    /**
     * \brief post-increment operator
     * \return incremented value
     */
    CommandType CommandType::operator++ (int)
    {
        CommandType result(*this);
        ++(*this);
        return result;
    }

    /**
     * \brief get command type
     * \return command type
     */
    const CommandType::Type& CommandType::getType() const
    {
        return mType;
    }

}