File: xmessage.cpp

package info (click to toggle)
xeus 5.2.8-1
  • links: PTS
  • area: main
  • in suites: forky
  • size: 9,764 kB
  • sloc: cpp: 7,406; makefile: 157; python: 25
file content (166 lines) | stat: -rw-r--r-- 5,051 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/***************************************************************************
* Copyright (c) 2016, Johan Mabille, Sylvain Corlay, Martin Renou          *
* Copyright (c) 2016, QuantStack                                           *
*                                                                          *
* Distributed under the terms of the BSD 3-Clause License.                 *
*                                                                          *
* The full license is in the file LICENSE, distributed with this software. *
****************************************************************************/

#include <chrono>
#include <cstddef>
#include <iomanip>
#include <stdexcept>
#include <sstream>
#include <string>
#include <utility>

#include "nlohmann/json.hpp"

#include "xeus/xguid.hpp"
#include "xeus/xmessage.hpp"

namespace nl = nlohmann;

namespace xeus
{
    xmessage_base::xmessage_base(
        nl::json header, nl::json parent_header, nl::json metadata, nl::json content, buffer_sequence buffers)
        : m_header(std::move(header))
        , m_parent_header(std::move(parent_header))
        , m_metadata(std::move(metadata))
        , m_content(std::move(content))
        , m_buffers(std::move(buffers))
    {
    }

    const nl::json& xmessage_base::header() const
    {
        return m_header;
    }

    const nl::json& xmessage_base::parent_header() const
    {
        return m_parent_header;
    }

    const nl::json& xmessage_base::metadata() const
    {
        return m_metadata;
    }

    const nl::json& xmessage_base::content() const
    {
        return m_content;
    }

    const buffer_sequence& xmessage_base::buffers() const &
    {
        return m_buffers;
    }
    
    buffer_sequence&& xmessage_base::buffers() &&
    {
        return std::move(m_buffers);
    }
    
    xmessage::xmessage(const guid_list& zmq_id,
                       nl::json header,
                       nl::json parent_header,
                       nl::json metadata,
                       nl::json content,
                       buffer_sequence buffers)
        : xmessage_base(std::move(header),
                        std::move(parent_header),
                        std::move(metadata),
                        std::move(content),
                        std::move(buffers))
        , m_zmq_id(zmq_id)
    {
    }

    xmessage::xmessage(const guid_list& zmq_id,
                       xmessage_base_data&& data)
        : xmessage_base(std::move(data.m_header),
                        std::move(data.m_parent_header),
                        std::move(data.m_metadata),
                        std::move(data.m_content),
                        std::move(data.m_buffers))
        , m_zmq_id(zmq_id)
    {
    }

    auto xmessage::identities() const -> const guid_list&
    {
        return m_zmq_id;
    }

    xpub_message::xpub_message(const std::string& topic,
                               nl::json header,
                               nl::json parent_header,
                               nl::json metadata,
                               nl::json content,
                               buffer_sequence buffers)
        : xmessage_base(std::move(header),
                        std::move(parent_header),
                        std::move(metadata),
                        std::move(content),
                        std::move(buffers))
        , m_topic(topic)
    {
    }

    xpub_message::xpub_message(const std::string& topic,
                               xmessage_base_data&& data)
        : xmessage_base(std::move(data.m_header),
                        std::move(data.m_parent_header),
                        std::move(data.m_metadata),
                        std::move(data.m_content),
                        std::move(data.m_buffers))
        , m_topic(topic)
    {
    }

    const std::string& xpub_message::topic() const
    {
        return m_topic;
    }

    std::string iso8601_now()
    {
        std::ostringstream ss;

        // now
        auto now = std::chrono::system_clock::now();

        // down to seconds
        auto itt = std::chrono::system_clock::to_time_t(now);
        ss << std::put_time(std::gmtime(&itt), "%FT%T");

        // down to microseconds
        auto micros = std::chrono::duration_cast<std::chrono::microseconds>(now.time_since_epoch());
        auto fractionals = micros.count() % 1000000;
        ss << "." << fractionals << "Z";

        return ss.str();
    }

    std::string get_protocol_version()
    {
        return XEUS_KERNEL_PROTOCOL_VERSION;
    }

    nl::json make_header(const std::string& msg_type,
                      const std::string& user_name,
                      const std::string& session_id)
    {
        nl::json header;
        header["msg_id"] = new_xguid();
        header["username"] = user_name;
        header["session"] = session_id;
        header["date"] = iso8601_now();
        header["msg_type"] = msg_type;
        header["version"] = get_protocol_version();
        return header;
    }
}