diff options
author | Kozoriz <kozorizandriy@gmail.com> | 2016-03-10 10:59:30 +0200 |
---|---|---|
committer | Kozoriz <kozorizandriy@gmail.com> | 2016-03-10 13:28:45 +0200 |
commit | 253f9a224e8e78182b3e5f26c422002a3b581c2c (patch) | |
tree | 8cc3351651158b71eb374ec734171df947b131fb /src/components/telemetry_monitor | |
parent | 358ca6459aa2e9b2fe9ace737cc396937311c014 (diff) | |
download | sdl_core-253f9a224e8e78182b3e5f26c422002a3b581c2c.tar.gz |
TimeTester refatctoring.
Removed singletone using from TimeTester.
TimeTester renamed -> TelemetryMonitor.
All files and directories corrected.
Tests corrected, all works.
Disabled 2 tests(task created).
Related to : APPLINK-21915
Diffstat (limited to 'src/components/telemetry_monitor')
29 files changed, 2207 insertions, 0 deletions
diff --git a/src/components/telemetry_monitor/CMakeLists.txt b/src/components/telemetry_monitor/CMakeLists.txt new file mode 100644 index 0000000000..8ce8d19f85 --- /dev/null +++ b/src/components/telemetry_monitor/CMakeLists.txt @@ -0,0 +1,69 @@ +# Copyright (c) 2016, 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. + + +set(TELEMETRY_MONITOR_SRC_DIR ${COMPONENTS_DIR}/telemetry_monitor/src) + +include_directories ( + include + ${COMPONENTS_DIR}/utils/include/ + ${COMPONENTS_DIR}/protocol_handler/include/ + ${COMPONENTS_DIR}/connection_handler/include/ + ${COMPONENTS_DIR}/transport_manager/include/ + ${COMPONENTS_DIR}/application_manager/include/ + ${COMPONENTS_DIR}/policy/src/policy/include/ + ${COMPONENTS_DIR}/hmi_message_handler/include/ + ${COMPONENTS_DIR}/formatters/include/ + ${COMPONENTS_DIR}/media_manager/include/ + ${COMPONENTS_DIR}/smart_objects/include/ + ${COMPONENTS_DIR}/config_profile/include/ + ${JSONCPP_INCLUDE_DIRECTORY} + ${CMAKE_BINARY_DIR}/src/components/ + ${LOG4CXX_INCLUDE_DIRECTORY} +) + +set(SOURCES + ${TELEMETRY_MONITOR_SRC_DIR}/metric_wrapper.cc + ${TELEMETRY_MONITOR_SRC_DIR}/telemetry_monitor.cc + ${TELEMETRY_MONITOR_SRC_DIR}/application_manager_observer.cc + ${TELEMETRY_MONITOR_SRC_DIR}/transport_manager_observer.cc + ${TELEMETRY_MONITOR_SRC_DIR}/protocol_handler_observer.cc + ${TELEMETRY_MONITOR_SRC_DIR}/application_manager_metric.cc + ${TELEMETRY_MONITOR_SRC_DIR}/transport_manager_metric.cc + ${TELEMETRY_MONITOR_SRC_DIR}/protocol_handler_metric.cc +) + +add_library("TelemetryMonitor" ${SOURCES}) +target_link_libraries("TelemetryMonitor" ${LIBRARIES}) +add_dependencies("TelemetryMonitor" HMI_API MOBILE_API) + +if(BUILD_TESTS) + add_subdirectory(test) +endif() diff --git a/src/components/telemetry_monitor/include/telemetry_monitor/application_manager_metric.h b/src/components/telemetry_monitor/include/telemetry_monitor/application_manager_metric.h new file mode 100644 index 0000000000..df568e7ebd --- /dev/null +++ b/src/components/telemetry_monitor/include/telemetry_monitor/application_manager_metric.h @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2014, 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. + */ + +#ifndef SRC_COMPONENTS_TELEMETRY_MONITOR_INCLUDE_TELEMETRY_MONITOR_APPLICATION_MANAGER_MECTRIC_WRAPPER_H_ +#define SRC_COMPONENTS_TELEMETRY_MONITOR_INCLUDE_TELEMETRY_MONITOR_APPLICATION_MANAGER_MECTRIC_WRAPPER_H_ + +#include <string> + +#include "telemetry_monitor/metric_wrapper.h" +#include "telemetry_monitor/application_manager_observer.h" + + +namespace telemetry_monitor { + +class ApplicationManagerObserver; + +class ApplicationManagerMetricWrapper: public MetricWrapper { + + public: + utils::SharedPtr<application_manager::AMTelemetryObserver::MessageMetric> message_metric; + virtual Json::Value GetJsonMetric(); +}; + +} +#endif // SRC_COMPONENTS_TELEMETRY_MONITOR_INCLUDE_TELEMETRY_MONITOR_APPLICATION_MANAGER_MECTRIC_WRAPPER_H_ diff --git a/src/components/telemetry_monitor/include/telemetry_monitor/application_manager_observer.h b/src/components/telemetry_monitor/include/telemetry_monitor/application_manager_observer.h new file mode 100644 index 0000000000..995aad1085 --- /dev/null +++ b/src/components/telemetry_monitor/include/telemetry_monitor/application_manager_observer.h @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2014, 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. + */ + +#ifndef SRC_COMPONENTS_TELEMETRY_MONITOR_INCLUDE_TELEMETRY_MONITOR_APPLICATION_MANAGER_OBSERVER_H_ +#define SRC_COMPONENTS_TELEMETRY_MONITOR_INCLUDE_TELEMETRY_MONITOR_APPLICATION_MANAGER_OBSERVER_H_ + +#include "utils/message_queue.h" +#include "application_manager/telemetry_observer.h" +#include "telemetry_monitor/application_manager_metric.h" + + +namespace telemetry_monitor { + +class TelemetryMonitor; + +class ApplicationManagerObserver: public application_manager::AMTelemetryObserver { + public: + explicit ApplicationManagerObserver(TelemetryMonitor* telemetry_monitor); + virtual void OnMessage(utils::SharedPtr<MessageMetric> metric); + + private: + TelemetryMonitor* telemetry_monitor_; +}; + +} +#endif // SRC_COMPONENTS_TELEMETRY_MONITOR_INCLUDE_TELEMETRY_MONITOR_APPLICATION_MANAGER_OBSERVER_H_ diff --git a/src/components/telemetry_monitor/include/telemetry_monitor/json_keys.h b/src/components/telemetry_monitor/include/telemetry_monitor/json_keys.h new file mode 100644 index 0000000000..f5aba07721 --- /dev/null +++ b/src/components/telemetry_monitor/include/telemetry_monitor/json_keys.h @@ -0,0 +1,52 @@ +/* + * + * Copyright (c) 2014, 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. + */ + +#ifndef SRC_COMPONENTS_TELEMETRY_MONITOR_INCLUDE_TELEMETRY_MONITOR_JSON_KEYS_H_ +#define SRC_COMPONENTS_TELEMETRY_MONITOR_INCLUDE_TELEMETRY_MONITOR_JSON_KEYS_H_ + +namespace telemetry_monitor { + namespace strings { + const char logger[] = "logger"; + const char begin[] = "begin"; + const char end[] = "end"; + const char data_size[] = "data_size"; + const char message_id[] = "message_id"; + const char session_id[] = "session_id"; + const char correlation_id[] = "correlation_id"; + const char connection_key[] = "connection_key"; + const char stime[] = "stime"; + const char utime[] = "utime"; + const char memory[] = "RAM"; + } +} +#endif // SRC_COMPONENTS_TELEMETRY_MONITOR_INCLUDE_TELEMETRY_MONITOR_JSON_KEYS_H_ diff --git a/src/components/telemetry_monitor/include/telemetry_monitor/metric_wrapper.h b/src/components/telemetry_monitor/include/telemetry_monitor/metric_wrapper.h new file mode 100644 index 0000000000..66a0dc74f3 --- /dev/null +++ b/src/components/telemetry_monitor/include/telemetry_monitor/metric_wrapper.h @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2014, 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. + */ + +#ifndef SRC_COMPONENTS_TELEMETRY_MONITOR_INCLUDE_TELEMETRY_MONITOR_MECTRIC_H_ +#define SRC_COMPONENTS_TELEMETRY_MONITOR_INCLUDE_TELEMETRY_MONITOR_MECTRIC_H_ + +#include <string> +#include "utils/resource_usage.h" +#include "json/json.h" + +namespace telemetry_monitor { + +class MetricWrapper { + utils::ResourseUsage* resources; + public: + MetricWrapper(); + /* + * @brief get current cpu and memory info and store it as member + */ + bool grabResources(); + virtual std::string GetStyledString(); + virtual Json::Value GetJsonMetric(); + virtual ~MetricWrapper(); + protected: + void Clear(); +}; + + +} // namespace telemetry_monitor +#endif // SRC_COMPONENTS_TELEMETRY_MONITOR_INCLUDE_TELEMETRY_MONITOR_MECTRIC_H_ diff --git a/src/components/telemetry_monitor/include/telemetry_monitor/protocol_handler_metric.h b/src/components/telemetry_monitor/include/telemetry_monitor/protocol_handler_metric.h new file mode 100644 index 0000000000..d3c27ba55b --- /dev/null +++ b/src/components/telemetry_monitor/include/telemetry_monitor/protocol_handler_metric.h @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2014, 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. + */ + +#ifndef SRC_COMPONENTS_TELEMETRY_MONITOR_INCLUDE_TELEMETRY_MONITOR_PROTOCOL_HANDLER_MECTRIC_H_ +#define SRC_COMPONENTS_TELEMETRY_MONITOR_INCLUDE_TELEMETRY_MONITOR_PROTOCOL_HANDLER_MECTRIC_H_ + +#include <string> +#include "utils/shared_ptr.h" +#include "telemetry_monitor/metric_wrapper.h" +#include "protocol_handler_observer.h" + +namespace telemetry_monitor { + +class ProtocolHandlerMecticWrapper: public MetricWrapper { + + public: + utils::SharedPtr<protocol_handler::PHTelemetryObserver::MessageMetric> message_metric; + virtual Json::Value GetJsonMetric(); +}; +} // namespace telemetry_monitor +#endif // SRC_COMPONENTS_TELEMETRY_MONITOR_INCLUDE_TELEMETRY_MONITOR_PROTOCOL_HANDLER_MECTRIC_H_ diff --git a/src/components/telemetry_monitor/include/telemetry_monitor/protocol_handler_observer.h b/src/components/telemetry_monitor/include/telemetry_monitor/protocol_handler_observer.h new file mode 100644 index 0000000000..c9f6a7f07f --- /dev/null +++ b/src/components/telemetry_monitor/include/telemetry_monitor/protocol_handler_observer.h @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2014, 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. + */ + +#ifndef SRC_COMPONENTS_TELEMETRY_MONITOR_INCLUDE_TELEMETRY_MONITOR_PROTOCOL_HANDLER_OBSERVER_H_ +#define SRC_COMPONENTS_TELEMETRY_MONITOR_INCLUDE_TELEMETRY_MONITOR_PROTOCOL_HANDLER_OBSERVER_H_ + +#include "protocol_handler/telemetry_observer.h" +#include "utils/message_queue.h" + +namespace telemetry_monitor { + +class TelemetryMonitor; + +class ProtocolHandlerObserver: public protocol_handler::PHTelemetryObserver { + public: + explicit ProtocolHandlerObserver(TelemetryMonitor* telemetry_monitor); + + virtual void StartMessageProcess(uint32_t message_id, const TimevalStruct& start_time); + + virtual void EndMessageProcess(utils::SharedPtr<MessageMetric> m); + + private: + TelemetryMonitor* telemetry_monitor_; + std::map<uint32_t, TimevalStruct> time_starts; +}; +} // namespace telemetry_monitor +#endif // SRC_COMPONENTS_TELEMETRY_MONITOR_INCLUDE_TELEMETRY_MONITOR_PROTOCOL_HANDLER_OBSERVER_H_ diff --git a/src/components/telemetry_monitor/include/telemetry_monitor/telemetry_monitor.h b/src/components/telemetry_monitor/include/telemetry_monitor/telemetry_monitor.h new file mode 100644 index 0000000000..316f687b46 --- /dev/null +++ b/src/components/telemetry_monitor/include/telemetry_monitor/telemetry_monitor.h @@ -0,0 +1,105 @@ +/* + * Copyright (c) 2014, 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. + */ + +#ifndef SRC_COMPONENTS_TELEMETRY_MONITOR_INCLUDE_TELEMETRY_MONITOR_H_ +#define SRC_COMPONENTS_TELEMETRY_MONITOR_INCLUDE_TELEMETRY_MONITOR_H_ + +#include <string> + +#include "utils/shared_ptr.h" +#include "utils/message_queue.h" +#include "utils/threads/thread.h" +#include "utils/threads/thread_delegate.h" +#include "telemetry_monitor/metric_wrapper.h" +#include "telemetry_monitor/application_manager_observer.h" +#include "application_manager/application_manager_impl.h" +#include "telemetry_monitor/transport_manager_observer.h" +#include "transport_manager/transport_manager_impl.h" +#include "protocol_handler_observer.h" +#include "protocol_handler/protocol_handler_impl.h" + +namespace telemetry_monitor { + +using ::utils::MessageQueue; + +class Streamer : public threads::ThreadDelegate { + public: + explicit Streamer(TelemetryMonitor* const server); + ~Streamer(); + void threadMain() OVERRIDE; + void exitThreadMain() OVERRIDE; + + virtual void PushMessage(utils::SharedPtr<MetricWrapper> metric); + volatile bool is_client_connected_; + private: + void Start(); + void Stop(); + bool IsReady() const; + bool Send(const std::string &msg); + void ShutDownAndCloseSocket(int32_t socket_fd); + TelemetryMonitor* const kserver_; + int32_t server_socket_fd_; + int32_t client_socket_fd_; + volatile bool stop_flag_; + MessageQueue<utils::SharedPtr<MetricWrapper> > messages_; + DISALLOW_COPY_AND_ASSIGN(Streamer); +}; + +class TelemetryMonitor { + public: + TelemetryMonitor(const std::string& server_address, uint16_t port); + virtual ~TelemetryMonitor(); + virtual void Init( + TelemetryObservable<protocol_handler::PHTelemetryObserver>* protocol_handler, + TelemetryObservable<application_manager::AMTelemetryObserver>* app_manager, + TelemetryObservable<transport_manager::TMTelemetryObserver>* transport_manager); + virtual void Stop(); + virtual void Start(); + virtual void SendMetric(utils::SharedPtr<MetricWrapper> metric); + void set_streamer(Streamer* streamer); + const std::string& ip() const; + int16_t port() const; + + private: + std::string server_address_; + int16_t port_; + bool is_ready_; + threads::Thread* thread_; + Streamer* streamer_; + ApplicationManagerObserver app_observer; + TransportManagerObserver tm_observer; + ProtocolHandlerObserver ph_observer; + + DISALLOW_COPY_AND_ASSIGN(TelemetryMonitor); +}; +} // namespace telemetry_monitor +#endif // SRC_COMPONENTS_TELEMETRY_MONITOR_INCLUDE_TELEMETRY_MONITOR_H_ diff --git a/src/components/telemetry_monitor/include/telemetry_monitor/transport_manager_metric.h b/src/components/telemetry_monitor/include/telemetry_monitor/transport_manager_metric.h new file mode 100644 index 0000000000..1e6cb501bc --- /dev/null +++ b/src/components/telemetry_monitor/include/telemetry_monitor/transport_manager_metric.h @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2014, 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. + */ + +#ifndef SRC_COMPONENTS_TELEMETRY_MONITOR_INCLUDE_TELEMETRY_MONITOR_TRANSPORT_MANAGER_MECTRIC_H_ +#define SRC_COMPONENTS_TELEMETRY_MONITOR_INCLUDE_TELEMETRY_MONITOR_TRANSPORT_MANAGER_MECTRIC_H_ + +#include <string> +#include "telemetry_monitor/metric_wrapper.h" +#include "telemetry_monitor/transport_manager_observer.h" + +namespace telemetry_monitor { + +class TransportManagerMecticWrapper: public MetricWrapper { + public: + utils::SharedPtr<transport_manager::TMTelemetryObserver::MessageMetric> message_metric; + virtual Json::Value GetJsonMetric(); +}; +} // namespace telemetry_monitor +#endif // SRC_COMPONENTS_TELEMETRY_MONITOR_INCLUDE_TELEMETRY_MONITOR_TRANSPORT_MANAGER_MECTRIC_H_ diff --git a/src/components/telemetry_monitor/include/telemetry_monitor/transport_manager_observer.h b/src/components/telemetry_monitor/include/telemetry_monitor/transport_manager_observer.h new file mode 100644 index 0000000000..9390ecd209 --- /dev/null +++ b/src/components/telemetry_monitor/include/telemetry_monitor/transport_manager_observer.h @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2014, 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. + */ + +#ifndef SRC_COMPONENTS_TELEMETRY_MONITOR_INCLUDE_TELEMETRY_MONITOR_TRANSPORT_MANAGER_OBSERVER_H_ +#define SRC_COMPONENTS_TELEMETRY_MONITOR_INCLUDE_TELEMETRY_MONITOR_TRANSPORT_MANAGER_OBSERVER_H_ + +#include "transport_manager/telemetry_observer.h" +#include "utils/message_queue.h" +#include "utils/date_time.h" + +namespace telemetry_monitor { + +class TelemetryMonitor; + +class TransportManagerObserver: public transport_manager::TMTelemetryObserver { + public: + explicit TransportManagerObserver(TelemetryMonitor* telemetry_monitor); + virtual void StartRawMsg(const protocol_handler::RawMessage* ptr); + virtual void StopRawMsg(const protocol_handler::RawMessage* ptr); + private: + TelemetryMonitor* telemetry_monitor_; + std::map<const protocol_handler::RawMessage*, TimevalStruct> time_starts; +}; +} // namespace telemetry_monitor +#endif // SRC_COMPONENTS_TELEMETRY_MONITOR_INCLUDE_TELEMETRY_MONITOR_TRANSPORT_MANAGER_OBSERVER_H_ diff --git a/src/components/telemetry_monitor/src/application_manager_metric.cc b/src/components/telemetry_monitor/src/application_manager_metric.cc new file mode 100644 index 0000000000..0af9570c41 --- /dev/null +++ b/src/components/telemetry_monitor/src/application_manager_metric.cc @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2014, 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 "telemetry_monitor/application_manager_metric.h" +#include "telemetry_monitor/json_keys.h" +#include "application_manager/smart_object_keys.h" +#include "utils/convert_utils.h" + +namespace telemetry_monitor { + +Json::Value ApplicationManagerMetricWrapper::GetJsonMetric() { + Json::Value result = MetricWrapper::GetJsonMetric(); + result[strings::logger] = "ApplicationManager"; + result[strings::begin] = + Json::Int64(date_time::DateTime::getuSecs(message_metric->begin)); + result[strings::end] = + Json::Int64(date_time::DateTime::getuSecs(message_metric->end)); + const NsSmartDeviceLink::NsSmartObjects::SmartObject& params = + message_metric->message->getElement(application_manager::strings::params); + result[strings::correlation_id] = utils::ConvertInt64ToLongLongInt( + params[application_manager::strings::correlation_id].asInt()); + result[strings::connection_key] = utils::ConvertInt64ToLongLongInt( + params[application_manager::strings::connection_key].asInt()); + return result; +} +} // namespace telemetry_monitor diff --git a/src/components/telemetry_monitor/src/application_manager_observer.cc b/src/components/telemetry_monitor/src/application_manager_observer.cc new file mode 100644 index 0000000000..47570497cf --- /dev/null +++ b/src/components/telemetry_monitor/src/application_manager_observer.cc @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2014, 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 "telemetry_monitor/application_manager_observer.h" +#include "utils/shared_ptr.h" +#include "telemetry_monitor/telemetry_monitor.h" +#include "telemetry_monitor/application_manager_metric.h" + +namespace telemetry_monitor { + +ApplicationManagerObserver::ApplicationManagerObserver(TelemetryMonitor* telemetry_monitor): + telemetry_monitor_(telemetry_monitor) { +} + +void ApplicationManagerObserver::OnMessage(utils::SharedPtr<MessageMetric> metric) { + ApplicationManagerMetricWrapper* m = new ApplicationManagerMetricWrapper(); + m->message_metric = metric; + m->grabResources(); + telemetry_monitor_->SendMetric(m); +} +} // namespace telemetry_monitor diff --git a/src/components/telemetry_monitor/src/metric_wrapper.cc b/src/components/telemetry_monitor/src/metric_wrapper.cc new file mode 100644 index 0000000000..2803921501 --- /dev/null +++ b/src/components/telemetry_monitor/src/metric_wrapper.cc @@ -0,0 +1,78 @@ +/* + * Copyright (c) 2015, 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 "telemetry_monitor/metric_wrapper.h" +#include "telemetry_monitor/json_keys.h" + +namespace telemetry_monitor { + + MetricWrapper::MetricWrapper(): + resources(NULL) { + } + +bool MetricWrapper::grabResources() { + Clear(); + resources = utils::Resources::getCurrentResourseUsage(); + if (NULL != resources) { + return true; + } else { + return false; + } + flush(std::cout); +} + +std::string MetricWrapper::GetStyledString() { + return GetJsonMetric().toStyledString(); +} + +Json::Value MetricWrapper::GetJsonMetric() { + Json::Value result; + if (resources) { + result[strings::stime] = resources->stime; + result[strings::utime] = resources->utime; + result[strings::memory] = resources->memory; + } + return result; +} + +void MetricWrapper::Clear() { + if (NULL != resources) { + delete resources; + resources = NULL; + } +} + +MetricWrapper::~MetricWrapper() { + Clear(); +} + +} diff --git a/src/components/telemetry_monitor/src/protocol_handler_metric.cc b/src/components/telemetry_monitor/src/protocol_handler_metric.cc new file mode 100644 index 0000000000..35beb3e1f7 --- /dev/null +++ b/src/components/telemetry_monitor/src/protocol_handler_metric.cc @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2014, 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 "telemetry_monitor/protocol_handler_metric.h" +#include "json/json.h" +#include "telemetry_monitor/json_keys.h" + +namespace telemetry_monitor { + +Json::Value ProtocolHandlerMecticWrapper::GetJsonMetric() { + Json::Value result = MetricWrapper::GetJsonMetric(); + result[strings::logger] = "ProtocolHandler"; + result[strings::begin] = + Json::Int64(date_time::DateTime::getuSecs(message_metric->begin)); + result[strings::end] = + Json::Int64(date_time::DateTime::getuSecs(message_metric->end)); + result[strings::message_id] = message_metric->message_id; + result[strings::connection_key] = message_metric->connection_key; + return result; +} + +} // namespace telemetry_monitor diff --git a/src/components/telemetry_monitor/src/protocol_handler_observer.cc b/src/components/telemetry_monitor/src/protocol_handler_observer.cc new file mode 100644 index 0000000000..c65e20121f --- /dev/null +++ b/src/components/telemetry_monitor/src/protocol_handler_observer.cc @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2014, 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 "utils/date_time.h" +#include "telemetry_monitor/protocol_handler_observer.h" +#include "telemetry_monitor/protocol_handler_metric.h" +#include "telemetry_monitor/telemetry_monitor.h" + +namespace telemetry_monitor { + +CREATE_LOGGERPTR_GLOBAL(logger_, "TelemetryMonitor") + +ProtocolHandlerObserver::ProtocolHandlerObserver(TelemetryMonitor *telemetry_monitor): + telemetry_monitor_(telemetry_monitor) { +} + +void ProtocolHandlerObserver::StartMessageProcess(uint32_t message_id, + const TimevalStruct &start_time) { + if (message_id == 0) { + return; + } + if (time_starts.find(message_id) != time_starts.end()) { + LOG4CXX_DEBUG(logger_, "Already waiting for stop processing for Message ID: " + << message_id); + return; + } + time_starts[message_id] = start_time; +} + +void ProtocolHandlerObserver::EndMessageProcess(utils::SharedPtr<MessageMetric> m) { + uint32_t message_id = m->message_id; + std::map<uint32_t, TimevalStruct>::const_iterator it = time_starts.find(message_id); + if (it == time_starts.end()) { + LOG4CXX_WARN(logger_, "Cant find start time for message" << message_id); + return; + } + m->begin = time_starts[message_id]; + m->end = date_time::DateTime::getCurrentTime(); + ProtocolHandlerMecticWrapper* metric = new ProtocolHandlerMecticWrapper(); + metric->message_metric = m; + metric->grabResources(); + telemetry_monitor_->SendMetric(metric); +} +} // namespace telemetry_monitor diff --git a/src/components/telemetry_monitor/src/telemetry_monitor.cc b/src/components/telemetry_monitor/src/telemetry_monitor.cc new file mode 100644 index 0000000000..7a697cb309 --- /dev/null +++ b/src/components/telemetry_monitor/src/telemetry_monitor.cc @@ -0,0 +1,288 @@ +/* + * Copyright (c) 2014, 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 "telemetry_monitor/telemetry_monitor.h" + +#include <sys/socket.h> +#include <sys/types.h> +#include <sys/select.h> +#include <sys/time.h> +#include <netinet/in.h> +#include <arpa/inet.h> +#include <unistd.h> + +#include "transport_manager/transport_manager_default.h" +#include "utils/resource_usage.h" +#include "telemetry_monitor/telemetry_observable.h" + +namespace telemetry_monitor { + +CREATE_LOGGERPTR_GLOBAL(logger_, "TelemetryMonitor") + +TelemetryMonitor::TelemetryMonitor(const std::string& server_address, + uint16_t port): + server_address_(server_address), + port_(port), + thread_(NULL), + streamer_(NULL), + app_observer(this), + tm_observer(this), + ph_observer(this) { + +} + +void TelemetryMonitor::Start() { + streamer_ = new Streamer(this); + thread_ = threads::CreateThread("TelemetryMonitor", streamer_ ); +} + +void TelemetryMonitor::set_streamer(Streamer* streamer) { + LOG4CXX_AUTO_TRACE(logger_); + if (thread_ && !thread_->is_running()) { + thread_->set_delegate(streamer); + if (streamer_) { + delete streamer_; + } + streamer_ = streamer; + } else { + LOG4CXX_ERROR(logger_, "Unable to replace streamer if it is active"); + } +} + +const std::string& TelemetryMonitor::ip() const { + return server_address_; +} + +int16_t TelemetryMonitor::port() const { + return port_; +} + +TelemetryMonitor::~TelemetryMonitor() { + Stop(); +} + +void TelemetryMonitor::Init( + TelemetryObservable<protocol_handler::PHTelemetryObserver>* + protocol_handler, + TelemetryObservable<application_manager::AMTelemetryObserver>* + app_manager, + TelemetryObservable<transport_manager::TMTelemetryObserver>* + transport_manager) { + LOG4CXX_AUTO_TRACE(logger_); + DCHECK_OR_RETURN_VOID(streamer_); + + app_manager->SetTelemetryObserver(&app_observer); + transport_manager->SetTelemetryObserver(&tm_observer); + protocol_handler->SetTelemetryObserver(&ph_observer); + + DCHECK_OR_RETURN_VOID(thread_); + thread_->start(threads::ThreadOptions()); +} + +void TelemetryMonitor::Stop() { + LOG4CXX_AUTO_TRACE(logger_); + if (thread_) { + thread_->stop(); + thread_->join(); + threads::DeleteThread(thread_); + } + thread_ = NULL; +} + +void TelemetryMonitor::SendMetric(utils::SharedPtr<MetricWrapper> metric) { + if ((NULL != streamer_ )&& streamer_->is_client_connected_) { + streamer_->PushMessage(metric); + } +} + +Streamer::Streamer(TelemetryMonitor* const server) + : is_client_connected_(false), + kserver_(server), + server_socket_fd_(0), + client_socket_fd_(0), + stop_flag_(false) { +} + +Streamer::~Streamer() { + Stop(); +} + +void Streamer::threadMain() { + LOG4CXX_AUTO_TRACE(logger_); + + Start(); + while (!stop_flag_) { + LOG4CXX_INFO(logger_, "Server socket is listening "); + client_socket_fd_ = accept(server_socket_fd_, NULL, NULL); + if (0 > client_socket_fd_) { + LOG4CXX_ERROR(logger_, "Cant open socket . Socket is busy "); + Stop(); + break; + } + LOG4CXX_INFO(logger_, "Client connected"); + + is_client_connected_ = true; + while (is_client_connected_) { + while (!messages_.empty()) { + utils::SharedPtr<MetricWrapper> metric; + if (!messages_.pop(metric)) { + continue; + } + is_client_connected_ = Send(metric->GetStyledString()); + } + + if (!IsReady()) { + LOG4CXX_INFO(logger_, "Client disconnected."); + break; + } + + messages_.wait(); + } + } +} + +void Streamer::exitThreadMain() { + LOG4CXX_AUTO_TRACE(logger_); + Stop(); + messages_.Shutdown(); +} + +void Streamer::Start() { + LOG4CXX_AUTO_TRACE(logger_); + server_socket_fd_ = socket(AF_INET, SOCK_STREAM, 0); + + if (0 >= server_socket_fd_) { + LOG4CXX_ERROR(logger_, "Server open error"); + return; + } else { + LOG4CXX_DEBUG(logger_, "Server socket : " << server_socket_fd_); + } + + int32_t optval = 1; + if (-1 == setsockopt(server_socket_fd_, SOL_SOCKET, SO_REUSEADDR, + &optval, sizeof optval)) { + LOG4CXX_ERROR(logger_, "Unable to set sockopt"); + return; + } + + sockaddr_in serv_addr_ = { 0 }; + serv_addr_.sin_addr.s_addr = inet_addr(kserver_->ip().c_str()); + serv_addr_.sin_family = AF_INET; + serv_addr_.sin_port = htons(kserver_->port()); + + if (-1 == bind(server_socket_fd_, + reinterpret_cast<struct sockaddr*>(&serv_addr_), + sizeof(serv_addr_))) { + LOG4CXX_ERROR(logger_, "Unable to bind server " + << kserver_->ip().c_str() << ':' << kserver_->port()); + return; + } + if (-1 == listen(server_socket_fd_, 1)) { + LOG4CXX_ERROR(logger_, "Streamer listen error " << strerror(errno) ); + return; + } +} + +void Streamer::ShutDownAndCloseSocket(int32_t socket_fd) { + LOG4CXX_AUTO_TRACE(logger_); + if (0 < socket_fd){ + LOG4CXX_INFO(logger_, "Shutdown socket"); + if (-1 == ::shutdown(socket_fd, SHUT_RDWR)) { + LOG4CXX_ERROR(logger_, "Unable to shutdown socket"); + } + if (-1 == close(socket_fd)) { + LOG4CXX_ERROR(logger_, "Unable to close socket"); + } + } else { + LOG4CXX_WARN(logger_, "Socket in not connected: " << socket_fd); + } +} + +void Streamer::Stop() { + LOG4CXX_AUTO_TRACE(logger_); + if (stop_flag_) { + LOG4CXX_WARN(logger_, "Already Stopped"); + return; + } + stop_flag_ = true; + messages_.Reset(); + LOG4CXX_WARN(logger_, "Stop server_socket_fd_"); + ShutDownAndCloseSocket(server_socket_fd_); + server_socket_fd_ = -1; + + LOG4CXX_WARN(logger_, "Stop client_socket_fd_"); + ShutDownAndCloseSocket(client_socket_fd_); + client_socket_fd_ = -1; + is_client_connected_ = false; +} + +bool Streamer::IsReady() const { + bool result = true; + fd_set fds; + FD_ZERO(&fds); + FD_SET(client_socket_fd_, &fds); + TimevalStruct tv = {0, 0}; + tv.tv_sec = 5; // set a 5 second timeout + tv.tv_usec = 0; + + const int retval = select(client_socket_fd_ + 1, 0, &fds, 0, &tv); + + if (-1 == retval) { + LOG4CXX_ERROR(logger_, "An error occurred"); + result = false; + } else if (0 == retval) { + LOG4CXX_ERROR(logger_, "The timeout expired"); + result = false; + } + + return result; +} + +bool Streamer::Send(const std::string& msg) { + LOG4CXX_AUTO_TRACE(logger_); + if (!IsReady()) { + LOG4CXX_ERROR(logger_, " Socket is not ready"); + return false; + } + + if (-1 == ::send(client_socket_fd_, msg.c_str(), + msg.size(), MSG_NOSIGNAL)) { + LOG4CXX_ERROR(logger_, " Unable to send"); + return false; + } + return true; +} + +void Streamer::PushMessage(utils::SharedPtr<MetricWrapper> metric) { + messages_.push(metric); +} +} // namespace telemetry_monitor diff --git a/src/components/telemetry_monitor/src/transport_manager_metric.cc b/src/components/telemetry_monitor/src/transport_manager_metric.cc new file mode 100644 index 0000000000..2e62a63797 --- /dev/null +++ b/src/components/telemetry_monitor/src/transport_manager_metric.cc @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2014, 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 "telemetry_monitor/transport_manager_metric.h" +#include "json/json.h" +#include "telemetry_monitor/json_keys.h" +#include "application_manager/smart_object_keys.h" + +namespace telemetry_monitor { + +Json::Value TransportManagerMecticWrapper::GetJsonMetric() { + Json::Value result = MetricWrapper::GetJsonMetric(); + result[strings::logger] = "TransportManager"; + result[strings::begin] = + Json::Int64(date_time::DateTime::getuSecs(message_metric->begin)); + result[strings::end] = + Json::Int64(date_time::DateTime::getuSecs(message_metric->end)); + result[strings::data_size] = static_cast<uint32_t>(message_metric->data_size); + return result; +} + +} // namespace telemetry_monitor diff --git a/src/components/telemetry_monitor/src/transport_manager_observer.cc b/src/components/telemetry_monitor/src/transport_manager_observer.cc new file mode 100644 index 0000000000..b74bc55b55 --- /dev/null +++ b/src/components/telemetry_monitor/src/transport_manager_observer.cc @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2014, 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 "telemetry_monitor/transport_manager_observer.h" + +#include <time.h> + +#include "telemetry_monitor/transport_manager_metric.h" +#include "telemetry_monitor/telemetry_monitor.h" + +namespace telemetry_monitor { + +TransportManagerObserver::TransportManagerObserver(TelemetryMonitor* telemetry_monitor): + telemetry_monitor_ (telemetry_monitor) { +} + +void TransportManagerObserver::StartRawMsg(const protocol_handler::RawMessage* ptr) { + time_starts[ptr] = date_time::DateTime::getCurrentTime(); +} + +void TransportManagerObserver::StopRawMsg(const protocol_handler::RawMessage* ptr) { + std::map<const protocol_handler::RawMessage*, TimevalStruct>::const_iterator it; + it = time_starts.find(ptr); + if (it != time_starts.end()) { + TransportManagerMecticWrapper* m = new TransportManagerMecticWrapper(); + m->message_metric = new transport_manager::TMTelemetryObserver::MessageMetric(); + m->message_metric->begin = it->second; + m->message_metric->end = date_time::DateTime::getCurrentTime(); + m->message_metric->data_size = ptr->data_size(); + m->grabResources(); + telemetry_monitor_->SendMetric(m); + } +} + +} //namespace telemetry_monitor diff --git a/src/components/telemetry_monitor/test/CMakeLists.txt b/src/components/telemetry_monitor/test/CMakeLists.txt new file mode 100644 index 0000000000..f574621828 --- /dev/null +++ b/src/components/telemetry_monitor/test/CMakeLists.txt @@ -0,0 +1,87 @@ +# Copyright (c) 2015, 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. + +if(BUILD_TESTS) + +include_directories( + include + ${GMOCK_INCLUDE_DIRECTORY} + ${COMPONENTS_DIR}/telemetry_monitor/include + ${COMPONENTS_DIR}/utils/include + ${COMPONENTS_DIR}/protocol_handler/include + ${COMPONENTS_DIR}/application_manager/include +) + + +set(testSources + metric_wrapper_test.cc + telemetry_monitor_test.cc + protocol_handler_metric_test.cc + protocol_handler_observer_test.cc + transport_manager_metric_test.cc + transport_manager_observer_test.cc + application_manager_metric_test.cc + application_manager_observer_test.cc +) + +set(testLibraries + gmock + TelemetryMonitor + jsoncpp + Utils + MediaManager + ProtocolHandler + v4_protocol_v1_2_no_extra + SmartObjects + ProtocolLibrary + ConfigProfile + ApplicationManager + MessageHelper + Resumption + jsoncpp + transport_manager + MediaManager + ProtocolHandler + connectionHandler +) + +if (BUILD_USB_SUPPORT) + list(APPEND testLibraries Libusb-1.0.16) +endif() + +if (BUILD_BT_SUPPORT) + list(APPEND testLibraries bluetooth) +endif() + +file(COPY log4cxx.properties DESTINATION ${CMAKE_CURRENT_BINARY_DIR}) + +create_test("telemetry_monitor_test" "${testSources}" "${testLibraries}") + +endif() diff --git a/src/components/telemetry_monitor/test/application_manager_metric_test.cc b/src/components/telemetry_monitor/test/application_manager_metric_test.cc new file mode 100644 index 0000000000..0ec23ccda0 --- /dev/null +++ b/src/components/telemetry_monitor/test/application_manager_metric_test.cc @@ -0,0 +1,118 @@ +/* + * Copyright (c) 2015, 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 "gtest/gtest.h" +#include "telemetry_monitor/json_keys.h" +#include "utils/resource_usage.h" +#include "application_manager/smart_object_keys.h" +#include "telemetry_monitor/application_manager_metric.h" + +namespace test { +namespace components { +namespace telemetry_monitor_test { + +using namespace ::telemetry_monitor; + +TEST(ApplicationManagerMetricWrapper, grabResources) { + ApplicationManagerMetricWrapper metric_test; + EXPECT_TRUE(metric_test.grabResources()); +} + +TEST(ApplicationManagerMetricWrapper, GetJsonMetric) { + ApplicationManagerMetricWrapper metric_test; + + TimevalStruct start_time; + start_time.tv_sec = 1; + start_time.tv_usec = 0; + + TimevalStruct end_time; + end_time.tv_sec = 10; + end_time.tv_usec = 0; + metric_test.message_metric = new application_manager::AMTelemetryObserver::MessageMetric(); + metric_test.message_metric->begin = start_time; + metric_test.message_metric->end = end_time; + NsSmartDeviceLink::NsSmartObjects::SmartObject obj; + obj["params"][application_manager::strings::correlation_id] = 11; + obj["params"][application_manager::strings::connection_key] = 12; + metric_test.message_metric->message = new NsSmartDeviceLink::NsSmartObjects::SmartObject(obj); + Json::Value jvalue = metric_test.GetJsonMetric(); + + EXPECT_EQ("null\n", jvalue[telemetry_monitor::strings::stime].toStyledString()); + EXPECT_EQ("null\n", jvalue[telemetry_monitor::strings::utime].toStyledString()); + EXPECT_EQ("null\n", jvalue[telemetry_monitor::strings::memory].toStyledString()); + + EXPECT_EQ(date_time::DateTime::getuSecs(start_time), jvalue[telemetry_monitor::strings::begin].asInt64()); + EXPECT_EQ(date_time::DateTime::getuSecs(end_time), jvalue[telemetry_monitor::strings::end].asInt64()); + EXPECT_EQ(obj["params"][application_manager::strings::correlation_id].asInt(), jvalue[telemetry_monitor::strings::correlation_id].asInt64()); + EXPECT_EQ(obj["params"][application_manager::strings::connection_key].asInt(), jvalue[telemetry_monitor::strings::connection_key].asInt()); +} + +TEST(ApplicationManagerMetricWrapper, GetJsonMetricWithGrabResources) { + ApplicationManagerMetricWrapper metric_test; + utils::ResourseUsage* resources = utils::Resources::getCurrentResourseUsage(); + EXPECT_TRUE(metric_test.grabResources()); + + TimevalStruct start_time; + start_time.tv_sec = 1; + start_time.tv_usec = 0; + + TimevalStruct end_time; + end_time.tv_sec = 10; + end_time.tv_usec = 0; + + metric_test.message_metric = new application_manager::AMTelemetryObserver::MessageMetric(); + metric_test.message_metric->begin = start_time; + metric_test.message_metric->end = end_time; + NsSmartDeviceLink::NsSmartObjects::SmartObject obj; + obj["params"][application_manager::strings::correlation_id] = 11; + obj["params"][application_manager::strings::connection_key] = 12; + metric_test.message_metric->message = new NsSmartDeviceLink::NsSmartObjects::SmartObject(obj); + Json::Value jvalue = metric_test.GetJsonMetric(); + + EXPECT_EQ(date_time::DateTime::getuSecs(start_time), jvalue[telemetry_monitor::strings::begin].asInt64()); + EXPECT_EQ(date_time::DateTime::getuSecs(end_time), jvalue[telemetry_monitor::strings::end].asInt64()); + EXPECT_EQ(obj["params"][application_manager::strings::correlation_id].asInt(), jvalue[telemetry_monitor::strings::correlation_id].asInt64()); + EXPECT_EQ(obj["params"][application_manager::strings::connection_key].asInt(), jvalue[telemetry_monitor::strings::connection_key].asInt()); + + EXPECT_EQ(date_time::DateTime::getuSecs(start_time), jvalue[telemetry_monitor::strings::begin].asInt64()); + EXPECT_EQ(date_time::DateTime::getuSecs(end_time), jvalue[telemetry_monitor::strings::end].asInt64()); + + EXPECT_NEAR(resources->stime, jvalue[telemetry_monitor::strings::stime].asInt(),1); + EXPECT_NEAR(resources->utime, jvalue[telemetry_monitor::strings::utime].asInt(),1); + EXPECT_EQ(resources->memory, jvalue[telemetry_monitor::strings::memory].asInt()); + + delete resources; +} + +} // namespace telemetry_monitor +} // namespace components +} // namespace test diff --git a/src/components/telemetry_monitor/test/application_manager_observer_test.cc b/src/components/telemetry_monitor/test/application_manager_observer_test.cc new file mode 100644 index 0000000000..9ca0850c15 --- /dev/null +++ b/src/components/telemetry_monitor/test/application_manager_observer_test.cc @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2015, 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 "gtest/gtest.h" +#include "telemetry_monitor/mock_telemetry_monitor.h" +#include "application_manager/telemetry_observer.h" +#include "telemetry_monitor/application_manager_metric.h" +#include "telemetry_monitor/application_manager_observer.h" +#include "utils/shared_ptr.h" +#include "telemetry_monitor/telemetry_monitor.h" + +namespace test { +namespace components { +namespace telemetry_monitor_test { + +using namespace telemetry_monitor; +using ::testing::_; + +TEST(ApplicationManagerObserver, CallOnMessage) { + MockTelemetryMonitor mock_telemetry_monitor; + ApplicationManagerObserver app_observer(&mock_telemetry_monitor); + typedef application_manager::AMTelemetryObserver::MessageMetric AMMetric; + utils::SharedPtr<AMMetric> ptr = application_manager::AMTelemetryObserver::MessageMetricSharedPtr(); + EXPECT_CALL(mock_telemetry_monitor, SendMetric(_)); + app_observer.OnMessage(ptr); +} + +} // namespace telemetry_monitor +} // namespace components +} // namespace test diff --git a/src/components/telemetry_monitor/test/include/telemetry_monitor/mock_telemetry_monitor.h b/src/components/telemetry_monitor/test/include/telemetry_monitor/mock_telemetry_monitor.h new file mode 100644 index 0000000000..2d849a634c --- /dev/null +++ b/src/components/telemetry_monitor/test/include/telemetry_monitor/mock_telemetry_monitor.h @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2016, 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. + */ + +#ifndef SRC_COMPONENTS_TRANSPORT_MANAGER_TEST_INCLUDE_MOCK_TELEMETRY_MONITOR_H_ +#define SRC_COMPONENTS_TRANSPORT_MANAGER_TEST_INCLUDE_MOCK_TELEMETRY_MONITOR_H_ + +#include <string> +#include "gmock/gmock.h" +#include "telemetry_monitor/telemetry_monitor.h" +#include "telemetry_monitor/metric_wrapper.h" +#include "utils/shared_ptr.h" + +namespace test { +namespace components { +namespace telemetry_monitor_test { + +class MockTelemetryMonitor: public telemetry_monitor::TelemetryMonitor { + public: + MockTelemetryMonitor():telemetry_monitor::TelemetryMonitor("",0u){} + MOCK_METHOD1(Init, void(protocol_handler::ProtocolHandlerImpl* ph)); + MOCK_METHOD0(Stop, void()); + MOCK_METHOD0(Start, void()); + MOCK_METHOD1(SendMetric, + void(utils::SharedPtr<telemetry_monitor::MetricWrapper> metric)); +}; +} // namespace transport_manager_test +} // namespace components +} // namespace test +#endif // SRC_COMPONENTS_TRANSPORT_MANAGER_TEST_INCLUDE_MOCK_TELEMETRY_MONITOR_H_ diff --git a/src/components/telemetry_monitor/test/log4cxx.properties b/src/components/telemetry_monitor/test/log4cxx.properties new file mode 100644 index 0000000000..68adbfa7f3 --- /dev/null +++ b/src/components/telemetry_monitor/test/log4cxx.properties @@ -0,0 +1,19 @@ +# Only ERROR and FATAL messages are logged to console +log4j.appender.Console=org.apache.log4j.ConsoleAppender +log4j.appender.Console.ImmediateFlush=true +log4j.appender.Console.layout=org.apache.log4j.PatternLayout +log4j.appender.Console.layout.ConversionPattern=%-5p [%d{dd MMM yyyy HH:mm:ss,SSS}][%c] %m%n +log4j.appender.Console.Threshold=DEBUG + +# Log for all SmartDeviceLinkCore messages +log4j.appender.SmartDeviceLinkCoreLogFile=org.apache.log4j.FileAppender +log4j.appender.SmartDeviceLinkCoreLogFile.File=SmartDeviceLinkCore.log +log4j.appender.SmartDeviceLinkCoreLogFile.append=true +log4j.appender.SmartDeviceLinkCoreLogFile.DatePattern='.' yyyy-MM-dd HH-mm +log4j.appender.SmartDeviceLinkCoreLogFile.ImmediateFlush=true +log4j.appender.SmartDeviceLinkCoreLogFile.layout=org.apache.log4j.PatternLayout +log4j.appender.SmartDeviceLinkCoreLogFile.layout.ConversionPattern=%-5p [%d{dd MMM yyyy HH:mm:ss,SSS}][%c] %F:%L %M: %m%n + + +# All SmartDeviceLinkCore logs +log4j.rootLogger=ALL, Console, SmartDeviceLinkCoreLogFile diff --git a/src/components/telemetry_monitor/test/metric_wrapper_test.cc b/src/components/telemetry_monitor/test/metric_wrapper_test.cc new file mode 100644 index 0000000000..08b9747742 --- /dev/null +++ b/src/components/telemetry_monitor/test/metric_wrapper_test.cc @@ -0,0 +1,82 @@ +/* + * Copyright (c) 2015, 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 "gtest/gtest.h" +#include "telemetry_monitor/metric_wrapper.h" +#include "telemetry_monitor/json_keys.h" +#include "json/json.h" +#include "utils/resource_usage.h" + +namespace test { +namespace components { +namespace telemetry_monitor_test { + +using namespace ::telemetry_monitor; + +TEST(MetricWrapper, grabResources) { + MetricWrapper metric_test; + EXPECT_TRUE(metric_test.grabResources()); +} + +TEST(MetricWrapper, GetJsonMetricWithoutGrab) { + MetricWrapper metric_test; + Json::Value jvalue = metric_test.GetJsonMetric(); + + EXPECT_EQ("null\n", jvalue[strings::stime].toStyledString()); + EXPECT_EQ("null\n", jvalue[strings::utime].toStyledString()); + EXPECT_EQ("null\n", jvalue[strings::memory].toStyledString()); +} + +TEST(MetricWrapper, GetJsonMetricWithGrabResources) { + MetricWrapper metric_test; + utils::ResourseUsage* resources = utils::Resources::getCurrentResourseUsage(); + EXPECT_TRUE(resources != NULL); + EXPECT_TRUE(metric_test.grabResources()); + Json::Value jvalue = metric_test.GetJsonMetric(); + + EXPECT_TRUE(jvalue[strings::stime].isInt()); + EXPECT_TRUE(jvalue[strings::utime].isInt()); + EXPECT_TRUE(jvalue[strings::memory].isInt()); + EXPECT_NE("null/n", jvalue[strings::stime].toStyledString()); + EXPECT_NE("null/n", jvalue[strings::utime].toStyledString()); + EXPECT_NE("null/n", jvalue[strings::memory].toStyledString()); + + EXPECT_NEAR(resources->stime, jvalue[strings::stime].asInt(),1); + EXPECT_NEAR(resources->utime, jvalue[strings::utime].asInt(),1); + EXPECT_EQ(resources->memory, jvalue[strings::memory].asInt()); + + delete resources; +} + +} // namespace telemetry_monitor +} // namespace components +} // namespace test diff --git a/src/components/telemetry_monitor/test/protocol_handler_metric_test.cc b/src/components/telemetry_monitor/test/protocol_handler_metric_test.cc new file mode 100644 index 0000000000..d74244a97f --- /dev/null +++ b/src/components/telemetry_monitor/test/protocol_handler_metric_test.cc @@ -0,0 +1,119 @@ +/* + * Copyright (c) 2015, 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 "gtest/gtest.h" +#include "telemetry_monitor/json_keys.h" +#include "json/json.h" +#include "utils/resource_usage.h" +#include "telemetry_monitor/protocol_handler_metric.h" +#include "protocol_handler/telemetry_observer.h" + +namespace test { +namespace components { +namespace telemetry_monitor_test { + +using namespace ::telemetry_monitor; + +TEST(ProtocolHandlerMetricTest, grabResources) { + ProtocolHandlerMecticWrapper metric_test; + EXPECT_TRUE(metric_test.grabResources()); +} + +TEST(ProtocolHandlerMetricTest, GetJsonMetric) { + ProtocolHandlerMecticWrapper metric_test; + + TimevalStruct start_time; + start_time.tv_sec = 1; + start_time.tv_usec = 0; + + TimevalStruct end_time; + end_time.tv_sec = 10; + end_time.tv_usec = 0; + metric_test.message_metric = new protocol_handler::PHTelemetryObserver::MessageMetric(); + metric_test.message_metric->begin = start_time; + metric_test.message_metric->end = end_time; + metric_test.message_metric->message_id = 5; + metric_test.message_metric->connection_key = 2; + Json::Value jvalue = metric_test.GetJsonMetric(); + + EXPECT_EQ("\"ProtocolHandler\"\n", jvalue[strings::logger].toStyledString()); + EXPECT_EQ("null\n", jvalue[strings::stime].toStyledString()); + EXPECT_EQ("null\n", jvalue[strings::utime].toStyledString()); + EXPECT_EQ("null\n", jvalue[strings::memory].toStyledString()); + + EXPECT_EQ(date_time::DateTime::getuSecs(start_time), jvalue[strings::begin].asInt64()); + EXPECT_EQ(date_time::DateTime::getuSecs(end_time), jvalue[strings::end].asInt64()); + EXPECT_EQ(5, jvalue[strings::message_id].asInt64()); + EXPECT_EQ(2, jvalue[strings::connection_key].asInt()); +} + +TEST(ProtocolHandlerMetricTest, GetJsonMetricWithGrabResources) { + ProtocolHandlerMecticWrapper metric_test; + utils::ResourseUsage* resources = utils::Resources::getCurrentResourseUsage(); + EXPECT_TRUE(resources != NULL); + EXPECT_TRUE(metric_test.grabResources()); + + TimevalStruct start_time; + start_time.tv_sec = 1; + start_time.tv_usec = 0; + + TimevalStruct end_time; + end_time.tv_sec = 10; + end_time.tv_usec = 0; + metric_test.message_metric = new protocol_handler::PHTelemetryObserver::MessageMetric(); + metric_test.message_metric->begin = start_time; + metric_test.message_metric->end = end_time; + metric_test.message_metric->message_id = 5; + metric_test.message_metric->connection_key = 2; + Json::Value jvalue = metric_test.GetJsonMetric(); + + EXPECT_TRUE(jvalue[strings::stime].isInt()); + EXPECT_TRUE(jvalue[strings::utime].isInt()); + EXPECT_TRUE(jvalue[strings::memory].isInt()); + EXPECT_NE("null/n", jvalue[strings::stime].toStyledString()); + EXPECT_NE("null/n", jvalue[strings::utime].toStyledString()); + EXPECT_NE("null/n", jvalue[strings::memory].toStyledString()); + + EXPECT_EQ(date_time::DateTime::getuSecs(start_time), jvalue[strings::begin].asInt64()); + EXPECT_EQ(date_time::DateTime::getuSecs(end_time), jvalue[strings::end].asInt64()); + EXPECT_EQ(5, jvalue[strings::message_id].asInt64()); + EXPECT_EQ(2, jvalue[strings::connection_key].asInt()); + + EXPECT_NEAR(resources->stime, jvalue[strings::stime].asInt(),1); + EXPECT_NEAR(resources->utime, jvalue[strings::utime].asInt(),1); + EXPECT_EQ(resources->memory, jvalue[strings::memory].asInt()); + delete resources; +} + +} // namespace telemetry_monitor +} // namespace components +} // namespace test diff --git a/src/components/telemetry_monitor/test/protocol_handler_observer_test.cc b/src/components/telemetry_monitor/test/protocol_handler_observer_test.cc new file mode 100644 index 0000000000..784e809da6 --- /dev/null +++ b/src/components/telemetry_monitor/test/protocol_handler_observer_test.cc @@ -0,0 +1,81 @@ +/* + * Copyright (c) 2015, 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 "gtest/gtest.h" +#include "telemetry_monitor/telemetry_monitor.h" +#include "telemetry_monitor/mock_telemetry_monitor.h" +#include "protocol_handler/telemetry_observer.h" + +namespace test { +namespace components { +namespace telemetry_monitor_test { + +using namespace telemetry_monitor; +using ::testing::_; + +TEST(ProtocolHandlerObserverTest, MessageProcess) { + MockTelemetryMonitor mock_telemetry_monitor; + + ProtocolHandlerObserver pr_handler(&mock_telemetry_monitor); + uint32_t message_id = 1; + TimevalStruct start_time; + start_time.tv_sec = 1; + start_time.tv_usec = 0; + pr_handler.StartMessageProcess(message_id, start_time); + + typedef protocol_handler::PHTelemetryObserver::MessageMetric MetricType; + utils::SharedPtr<MetricType> message_metric = new MetricType(); + message_metric->message_id = 1; + EXPECT_CALL(mock_telemetry_monitor, SendMetric(_)); + pr_handler.EndMessageProcess(message_metric); +} + +TEST(ProtocolHandlerObserverTest, MessageProcessWithZeroMessageId) { + MockTelemetryMonitor mock_telemetry_monitor; + + ProtocolHandlerObserver pr_handler(&mock_telemetry_monitor); + uint32_t message_id = 0; + TimevalStruct start_time; + start_time.tv_sec = 1; + start_time.tv_usec = 0; + pr_handler.StartMessageProcess(message_id, start_time); + + typedef protocol_handler::PHTelemetryObserver::MessageMetric MetricType; + utils::SharedPtr<MetricType> message_metric = new MetricType(); + message_metric->message_id = 0; + EXPECT_CALL(mock_telemetry_monitor, SendMetric(_)).Times(0); + pr_handler.EndMessageProcess(message_metric); +} + +} // namespace telemetry_monitor_test +} // namespace components +} // namespace test diff --git a/src/components/telemetry_monitor/test/telemetry_monitor_test.cc b/src/components/telemetry_monitor/test/telemetry_monitor_test.cc new file mode 100644 index 0000000000..14c7fe4cd5 --- /dev/null +++ b/src/components/telemetry_monitor/test/telemetry_monitor_test.cc @@ -0,0 +1,106 @@ +/* + * Copyright (c) 2015, 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 "gtest/gtest.h" +#include "telemetry_monitor/telemetry_monitor.h" +#include "protocol_handler/telemetry_observer.h" +#include "protocol_handler/protocol_handler.h" +#include "protocol_handler//mock_protocol_handler.h" +#include "protocol_handler/mock_session_observer.h" +#include "protocol_handler/mock_protocol_handler_settings.h" +#include "connection_handler/mock_connection_handler.h" +#include "transport_manager/transport_manager_mock.h" +#include "telemetry_monitor/mock_telemetry_observable.h" + + +using testing::Return; +using testing::_; + +namespace test { +namespace components { +namespace telemetry_monitor_test { + +using namespace telemetry_monitor; + +class StreamerMock : public Streamer { + public: + StreamerMock(TelemetryMonitor* const server) + : Streamer(server) { + is_client_connected_ = true; + } + MOCK_METHOD1(PushMessage,void(utils::SharedPtr<MetricWrapper> metric)); +}; + +TEST(TelemetryMonitorTest, MessageProcess) { + const std::string& server_address = "server_address"; + const uint16_t port = 12345; + MockTelemetryObservable am_observeble; + + transport_manager_test::MockTransportManager transport_manager_mock; + testing::NiceMock<connection_handler_test::MockConnectionHandler> + connection_handler_mock; + testing::NiceMock< + ::test::components::protocol_handler_test::MockProtocolHandlerSettings> + protocol_handler_settings_mock; + ::test::components::protocol_handler_test::MockSessionObserver + session_observer_mock; + + const size_t init_value = 1000u; + ON_CALL(protocol_handler_settings_mock, maximum_payload_size()) + .WillByDefault(Return(init_value)); + ON_CALL(protocol_handler_settings_mock, message_frequency_time()) + .WillByDefault(Return(init_value)); + ON_CALL(protocol_handler_settings_mock, malformed_message_filtering()) + .WillByDefault(Return(init_value)); + ON_CALL(protocol_handler_settings_mock, multiframe_waiting_timeout()) + .WillByDefault(Return(init_value)); + protocol_handler::ProtocolHandlerImpl protocol_handler_mock( + protocol_handler_settings_mock, session_observer_mock, + connection_handler_mock, transport_manager_mock); + + EXPECT_CALL(am_observeble, SetTelemetryObserver(_)); + EXPECT_CALL(transport_manager_mock, SetTelemetryObserver(_)); + telemetry_monitor::TelemetryMonitor telemetry_monitor(server_address, port); + StreamerMock streamer_mock(&telemetry_monitor); + telemetry_monitor.Start(); + + telemetry_monitor.set_streamer(&streamer_mock); + telemetry_monitor.Init(&protocol_handler_mock, &am_observeble, + &transport_manager_mock); + utils::SharedPtr<telemetry_monitor::MetricWrapper> test_metric; + EXPECT_CALL(streamer_mock, PushMessage(test_metric)); + telemetry_monitor.SendMetric(test_metric); +} + +} // namespace telemetry_monitor +} // namespace components +} // namespace test diff --git a/src/components/telemetry_monitor/test/transport_manager_metric_test.cc b/src/components/telemetry_monitor/test/transport_manager_metric_test.cc new file mode 100644 index 0000000000..53d4ed1ba4 --- /dev/null +++ b/src/components/telemetry_monitor/test/transport_manager_metric_test.cc @@ -0,0 +1,110 @@ +/* + * Copyright (c) 2015, 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 "gtest/gtest.h" +#include "telemetry_monitor/json_keys.h" +#include "json/json.h" +#include "utils/resource_usage.h" +#include "telemetry_monitor/transport_manager_metric.h" +#include "protocol_handler/telemetry_observer.h" + +namespace test { +namespace components { +namespace telemetry_monitor_test { + +using namespace ::telemetry_monitor; + +TEST(TransportManagerMetricWrapper, grabResources) { + TransportManagerMecticWrapper metric_test; + EXPECT_TRUE(metric_test.grabResources()); +} + +TEST(TransportManagerMetricWrapper, GetJsonMetric) { + TransportManagerMecticWrapper metric_test; + + TimevalStruct start_time; + start_time.tv_sec = 1; + start_time.tv_usec = 0; + + TimevalStruct end_time; + end_time.tv_sec = 10; + end_time.tv_usec = 0; + metric_test.message_metric = new transport_manager::TMTelemetryObserver::MessageMetric(); + metric_test.message_metric->begin = start_time; + metric_test.message_metric->end = end_time; + metric_test.message_metric->data_size = 1000; + Json::Value jvalue = metric_test.GetJsonMetric(); + + EXPECT_EQ("null\n", jvalue[strings::stime].toStyledString()); + EXPECT_EQ("null\n", jvalue[strings::utime].toStyledString()); + EXPECT_EQ("null\n", jvalue[strings::memory].toStyledString()); + + EXPECT_EQ(date_time::DateTime::getuSecs(start_time), jvalue[strings::begin].asInt64()); + EXPECT_EQ(date_time::DateTime::getuSecs(end_time), jvalue[strings::end].asInt64()); + EXPECT_EQ(1000, jvalue[strings::data_size].asInt()); +} + +TEST(TransportManagerMetricWrapper, GetJsonMetricWithGrabResources) { + TransportManagerMecticWrapper metric_test; + utils::ResourseUsage* resources = utils::Resources::getCurrentResourseUsage(); + EXPECT_TRUE(resources != NULL); + EXPECT_TRUE(metric_test.grabResources()); + + TimevalStruct start_time; + start_time.tv_sec = 1; + start_time.tv_usec = 0; + + TimevalStruct end_time; + end_time.tv_sec = 10; + end_time.tv_usec = 0; + metric_test.message_metric = new transport_manager::TMTelemetryObserver::MessageMetric(); + metric_test.message_metric->begin = start_time; + metric_test.message_metric->end = end_time; + + metric_test.message_metric->data_size = 1000; + Json::Value jvalue = metric_test.GetJsonMetric(); + + EXPECT_EQ("\"TransportManager\"\n", jvalue[strings::logger].toStyledString()); + EXPECT_EQ(date_time::DateTime::getuSecs(start_time), jvalue[strings::begin].asInt64()); + EXPECT_EQ(date_time::DateTime::getuSecs(end_time), jvalue[strings::end].asInt64()); + EXPECT_EQ(1000, jvalue[strings::data_size].asInt()); + + EXPECT_NEAR(resources->stime, jvalue[strings::stime].asInt(),1); + EXPECT_NEAR(resources->utime, jvalue[strings::utime].asInt(),1); + EXPECT_EQ(resources->memory, jvalue[strings::memory].asInt()); + + delete resources; +} + +} // namespace telemetry_monitor +} // namespace components +} // namespace test diff --git a/src/components/telemetry_monitor/test/transport_manager_observer_test.cc b/src/components/telemetry_monitor/test/transport_manager_observer_test.cc new file mode 100644 index 0000000000..eba786ef70 --- /dev/null +++ b/src/components/telemetry_monitor/test/transport_manager_observer_test.cc @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2015, 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 "gtest/gtest.h" +#include "telemetry_monitor/mock_telemetry_monitor.h" +#include "protocol_handler/telemetry_observer.h" +#include "telemetry_monitor/transport_manager_metric.h" +#include "telemetry_monitor/transport_manager_observer.h" + +namespace test { +namespace components { +namespace telemetry_monitor_test { + +using namespace telemetry_monitor; +using ::testing::_; + +TEST(TransportManagerObserverTest, MessageProcess) { + MockTelemetryMonitor mock_telemetry_monitor; + TransportManagerObserver tr_observer(&mock_telemetry_monitor); + protocol_handler::RawMessage* ptr = new ::protocol_handler::RawMessage(0, 0, NULL, 0); + tr_observer.StartRawMsg(ptr); + EXPECT_CALL(mock_telemetry_monitor, SendMetric(_)); + tr_observer.StopRawMsg(ptr); + delete ptr; +} + +} // namespace telemetry_monitor +} // namespace components +} // namespace test |