1 /*
2  * This file is part of yacas.
3  * Yacas is free software: you can redistribute it and/or modify
4  * it under the terms of the GNU Lesset General Public License as
5  * published by the Free Software Foundation, either version 2.1
6  * of the License, or (at your option) any later version.
7  *
8  * Yacas is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public
14  * License along with yacas. If not, see <http://www.gnu.org/licenses/>.
15  *
16  */
17 
18 /*
19  * File:   yacas_kernel.hpp
20  * Author: mazur
21  *
22  * Created on November 6, 2015, 3:10 PM
23  */
24 
25 #ifndef YACAS_KERNEL_HPP
26 #define YACAS_KERNEL_HPP
27 
28 #include "hmac_sha256.hpp"
29 #include "yacas_engine.hpp"
30 
31 #include <boost/uuid/random_generator.hpp>
32 #include <jsoncpp/json/json.h>
33 #include <zmqpp/zmqpp.hpp>
34 
35 #include <map>
36 #include <sstream>
37 
38 class YacasKernel : NonCopyable {
39 public:
40     YacasKernel(const std::string& scripts_path, const Json::Value&);
41 
42     void run();
43 
44 private:
45     class Session : NonCopyable {
46     public:
47         explicit Session(const std::string& key);
48 
auth() const49         const HMAC_SHA256& auth() const { return _auth; };
uuid() const50         const boost::uuids::uuid& uuid() const { return _uuid; };
51 
generate_msg_uuid() const52         boost::uuids::uuid generate_msg_uuid() const { return _uuid_gen(); }
53 
54     private:
55         HMAC_SHA256 _auth;
56 
57         mutable boost::uuids::random_generator _uuid_gen;
58         boost::uuids::uuid _uuid;
59     };
60 
61     class Request : NonCopyable {
62     public:
63         Request(const Session& session, const zmqpp::message& msg);
64 
header() const65         const Json::Value& header() const { return _header; }
content() const66         const Json::Value& content() const { return _content; }
67 
68         void reply(zmqpp::socket&,
69                    const std::string& type,
70                    const Json::Value& content) const;
71 
72     private:
73         const Session& _session;
74         Json::Value _header;
75         Json::Value _content;
76         Json::Value _metadata;
77         std::string _identities_buf;
78     };
79 
80     void _handle_shell(const std::shared_ptr<Request>& request);
81     void _handle_engine(const zmqpp::message& msg);
82 
83     Session _session;
84 
85     zmqpp::context _ctx;
86 
87     zmqpp::socket _hb_socket;
88     zmqpp::socket _iopub_socket;
89     zmqpp::socket _control_socket;
90     zmqpp::socket _stdin_socket;
91     zmqpp::socket _shell_socket;
92 
93     zmqpp::socket _engine_socket;
94 
95     unsigned long _execution_count;
96 
97     YacasEngine _engine;
98 
99     bool _tex_output;
100     std::stringstream _side_effects;
101     CYacas _yacas;
102 
103     std::map<unsigned long, std::shared_ptr<Request>> _execute_requests;
104 
105     bool _shutdown;
106 };
107 
108 #endif
109