├── .gitignore ├── .gitmodules ├── .travis.yml ├── AUTHORS ├── CMakeLists.txt ├── Dockerfile ├── LICENSE ├── README.md ├── cmake └── locate_library.cmake ├── cocaine-bf.spec ├── config.hpp.in ├── debian ├── changelog ├── cocaine-runtime.conf ├── cocaine-runtime.default ├── cocaine-runtime.dirs ├── cocaine-runtime.init ├── cocaine-runtime.install ├── cocaine-runtime.postinst ├── cocaine-runtime.postrm ├── compat ├── control ├── copyright ├── libcocaine-core3.install ├── libcocaine-dev.install ├── libcocaine-dev.links ├── libcocaine-io-util3.install ├── rules └── source │ └── format ├── docker ├── .dockerignore └── Dockerfile ├── fedora ├── cocaine-runtime.service └── cocaine-runtime.tmpfiles ├── include └── cocaine │ ├── api │ ├── authentication.hpp │ ├── authorization │ │ ├── event.hpp │ │ ├── storage.hpp │ │ └── unicorn.hpp │ ├── cluster.hpp │ ├── executor.hpp │ ├── gateway.hpp │ ├── service.hpp │ ├── storage.hpp │ └── unicorn.hpp │ ├── auth │ └── uid.hpp │ ├── common.hpp │ ├── context.hpp │ ├── context │ ├── config.hpp │ ├── filter.hpp │ ├── mapper.hpp │ ├── quote.hpp │ └── signal.hpp │ ├── defaults.hpp │ ├── detail │ ├── cluster │ │ ├── multicast.hpp │ │ └── predefine.hpp │ ├── crypto.hpp │ ├── essentials.hpp │ ├── gateway │ │ └── adhoc.hpp │ ├── runtime │ │ ├── logging.hpp │ │ └── pid_file.hpp │ ├── service │ │ ├── locator.hpp │ │ ├── locator │ │ │ └── routing.hpp │ │ ├── logging.hpp │ │ └── storage.hpp │ └── storage │ │ └── files.hpp │ ├── dynamic.hpp │ ├── dynamic │ ├── constructors.hpp │ ├── constructors │ │ ├── endpoint.hpp │ │ ├── graph.hpp │ │ ├── optional.hpp │ │ ├── set.hpp │ │ └── shared_ptr.hpp │ ├── converters.hpp │ ├── converters │ │ └── endpoint.hpp │ ├── detail.hpp │ ├── dynamic.hpp │ └── object.hpp │ ├── engine.hpp │ ├── errors.hpp │ ├── executor │ └── asio.hpp │ ├── format.hpp │ ├── format │ ├── base.hpp │ ├── deque.hpp │ ├── dynamic.hpp │ ├── endpoint.hpp │ ├── error_code.hpp │ ├── exception.hpp │ ├── graph.hpp │ ├── header.hpp │ ├── kv_container.hpp │ ├── linear_container.hpp │ ├── map.hpp │ ├── optional.hpp │ ├── ptr.hpp │ ├── set.hpp │ ├── tuple.hpp │ └── vector.hpp │ ├── forwards.hpp │ ├── hpack │ ├── header.hpp │ ├── header_definitions.hpp │ ├── mpl.hpp │ ├── msgpack_traits.hpp │ └── static_table.hpp │ ├── idl │ ├── context.hpp │ ├── control.hpp │ ├── locator.hpp │ ├── logging.hpp │ ├── primitive.hpp │ ├── storage.hpp │ └── streaming.hpp │ ├── locked_ptr.hpp │ ├── logging.hpp │ ├── memory.hpp │ ├── middleware │ ├── auth.hpp │ └── headers.hpp │ ├── platform.hpp │ ├── repository.hpp │ ├── repository │ ├── authentication.hpp │ ├── authorization.hpp │ ├── cluster.hpp │ ├── gateway.hpp │ ├── service.hpp │ ├── storage.hpp │ └── unicorn.hpp │ ├── rpc │ ├── actor.hpp │ ├── asio │ │ ├── decoder.hpp │ │ ├── encoder.hpp │ │ ├── readable_stream.hpp │ │ ├── transport.hpp │ │ └── writable_stream.hpp │ ├── basic_dispatch.hpp │ ├── dispatch.hpp │ ├── frozen.hpp │ ├── graph.hpp │ ├── protocol.hpp │ ├── queue.hpp │ ├── result_of.hpp │ ├── sanitize.hpp │ ├── session.hpp │ ├── slot.hpp │ ├── slot │ │ ├── blocking.hpp │ │ ├── deferred.hpp │ │ ├── function.hpp │ │ ├── generic.hpp │ │ └── streamed.hpp │ ├── tags.hpp │ ├── traversal.hpp │ └── upstream.hpp │ ├── signal.hpp │ ├── trace │ ├── logger.hpp │ └── trace.hpp │ ├── traits.hpp │ ├── traits │ ├── attributes.hpp │ ├── dynamic.hpp │ ├── endpoint.hpp │ ├── enum.hpp │ ├── error_code.hpp │ ├── frozen.hpp │ ├── graph.hpp │ ├── literal.hpp │ ├── map.hpp │ ├── optional.hpp │ ├── siginfo.hpp │ ├── tuple.hpp │ └── vector.hpp │ ├── tuple.hpp │ ├── unicorn │ ├── path.hpp │ └── value.hpp │ ├── unique_id.hpp │ ├── utility.hpp │ └── utility │ ├── exchange.hpp │ └── future.hpp ├── libcocaine-core3.version ├── src ├── actor.cpp ├── api │ ├── authentication.cpp │ ├── authorization.cpp │ ├── storage.cpp │ └── unicorn.cpp ├── auth │ └── uid.cpp ├── authentication │ └── promiscuous.hpp ├── authorization │ ├── event.cpp │ ├── event.hpp │ ├── storage.cpp │ ├── storage.hpp │ ├── storage │ │ └── metainfo.hpp │ ├── unicorn.cpp │ └── unicorn.hpp ├── chamber.cpp ├── chamber.hpp ├── cluster │ ├── multicast.cpp │ └── predefine.cpp ├── context.cpp ├── context │ ├── config.cpp │ ├── distributor.hpp │ └── mapper.cpp ├── crypto.cpp ├── decoder.cpp ├── defaults.cpp ├── dispatch.cpp ├── dynamic.cpp ├── encoder.cpp ├── engine.cpp ├── errors.cpp ├── essentials.cpp ├── executor │ └── asio.cpp ├── gateway │ └── adhoc.cpp ├── header.cpp ├── logging.cpp ├── repository.cpp ├── runtime │ ├── logging.cpp │ ├── pid_file.cpp │ ├── runtime.cpp │ └── signal.hpp ├── service │ ├── locator.cpp │ ├── locator │ │ └── routing.cpp │ ├── logging.cpp │ └── storage.cpp ├── session.cpp ├── signal.cpp ├── storage │ └── files.cpp ├── trace.cpp ├── trace │ └── logger.cpp ├── unicorn │ └── value.cpp └── unique_id.cpp └── tests ├── CMakeLists.txt ├── benchmark.cpp ├── ci └── travis.sh ├── test_storage.rb └── unit ├── context.cpp ├── format.cpp ├── header.cpp ├── header_table.cpp ├── lexical_cast.cpp ├── protocol.cpp ├── test_idl.hpp └── uuid.cpp /.gitignore: -------------------------------------------------------------------------------- 1 | # Generated files 2 | include/cocaine/config.hpp 3 | 4 | # Build directory 5 | build/* 6 | tests/build/* 7 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "foreign/backward-cpp"] 2 | path = foreign/backward-cpp 3 | url = git://github.com/bombela/backward-cpp.git 4 | [submodule "foreign/rapidjson"] 5 | path = foreign/rapidjson 6 | url = git://github.com/miloyip/rapidjson.git 7 | [submodule "tests/foreign/celero"] 8 | path = tests/foreign/celero 9 | url = git://github.com/DigitalInBlue/Celero.git 10 | [submodule "foreign/asio"] 11 | path = foreign/asio 12 | url = git://github.com/chriskohlhoff/asio.git 13 | [submodule "foreign/modules"] 14 | path = foreign/modules 15 | url = git://github.com/3Hren/modules.git 16 | [submodule "foreign/metrics"] 17 | path = foreign/metrics 18 | url = git://github.com/3Hren/metrics.git 19 | [submodule "tests/foreign/.googletest"] 20 | path = tests/foreign/.googletest 21 | url = https://github.com/google/googletest.git 22 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: required 2 | services: 3 | - docker 4 | script: 5 | - docker build --build-arg BUILD_BRANCH=$TRAVIS_BRANCH . 6 | -------------------------------------------------------------------------------- /AUTHORS: -------------------------------------------------------------------------------- 1 | Contributors 2 | ============ 3 | 4 | Andrey Sibiryov 5 | Rim Zaidullin 6 | Vickenty Fesunov 7 | Alexander Eliseev 8 | Alexey Ivanov 9 | Anton Kortunov 10 | Eugeny Polyakov 11 | Andrey Goryachev 12 | Evgeny Safronov 13 | Ruslan Nigmatullin 14 | Anton Tyurin 15 | Andrey Larionov 16 | Denis Zaitsev 17 | 18 | Yandex LLC <*@yandex-team.ru> 19 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM ubuntu:trusty 2 | 3 | # Pass build branch from travis 4 | ARG BUILD_BRANCH 5 | ENV BUILD_BRANCH=$BUILD_BRANCH 6 | 7 | RUN apt-get -y -qq update 8 | RUN apt-get -y -qq install build-essential devscripts equivs git-core 9 | 10 | # Yep. It's a bit hackish to install our internal dependencies through git. 11 | ## Build and install Metrics. 12 | RUN git clone https://github.com/3Hren/metrics --recursive -b master /build/metrics 13 | RUN cd /build/metrics && \ 14 | DEBIAN_FRONTEND=noninteractive mk-build-deps -ir -t "apt-get -qq --no-install-recommends" 15 | RUN cd /build/metrics && \ 16 | yes | debuild -e CC -e CXX -uc -us -j$(cat /proc/cpuinfo | fgrep -c processor) && \ 17 | debi 18 | 19 | ## Build and install Blackhole. 20 | RUN git clone https://github.com/3Hren/blackhole --recursive -b master /build/blackhole 21 | RUN cd /build/blackhole && \ 22 | DEBIAN_FRONTEND=noninteractive mk-build-deps -ir -t "apt-get -qq --no-install-recommends" 23 | RUN cd /build/blackhole && \ 24 | yes | debuild -e CC -e CXX -uc -us -j$(cat /proc/cpuinfo | fgrep -c processor) && \ 25 | debi 26 | 27 | # Hack to cache plugins dependencies. 28 | RUN apt-get -qq install libarchive-dev uuid-dev libcgroup-dev libboost-filesystem-dev libboost-thread-dev libnl-3-dev libnl-genl-3-dev libzookeeper-mt-dev libpqxx-dev 29 | 30 | # Build and install cocaine-core. 31 | COPY . /build/cocaine-core 32 | RUN cd /build/cocaine-core && \ 33 | DEBIAN_FRONTEND=noninteractive mk-build-deps -ir -t "apt-get -qq --no-install-recommends" 34 | RUN cd /build/cocaine-core && \ 35 | yes | debuild -e CC -e CXX -uc -us -j$(cat /proc/cpuinfo | fgrep -c processor) && \ 36 | debi 37 | 38 | # Build and install cocaine-plugins. 39 | RUN git clone https://github.com/3Hren/cocaine-plugins --recursive -b $BUILD_BRANCH /build/cocaine-plugins 40 | RUN cd /build/cocaine-plugins && \ 41 | cmake -DELASTICSEARCH=OFF -DMONGO=OFF -DURLFETCH=OFF -DDOCKER=OFF -DELLIPTICS=OFF . && \ 42 | make 43 | 44 | # Cleanup. 45 | RUN apt-get -y -qq purge cocaine-core-build-deps && \ 46 | apt-get -y -qq purge blackhole-build-deps && \ 47 | apt-get -y -qq purge metrics-build-deps && \ 48 | apt-get -y -qq purge build-essential devscripts equivs git-core && \ 49 | apt-get -y -qq autoremove --purge 50 | -------------------------------------------------------------------------------- /cmake/locate_library.cmake: -------------------------------------------------------------------------------- 1 | FUNCTION(LOCATE_LIBRARY VARIABLE HEADER LIBRARY) 2 | IF(${VARIABLE}_INCLUDE_DIRS AND ${VARIABLE}_LIBRARY_DIRS) 3 | RETURN() 4 | ENDIF() 5 | 6 | FIND_PATH(${VARIABLE}_INCLUDE_DIRS NAMES ${HEADER} PATH_SUFFIXES ${ARGN}) 7 | FIND_LIBRARY(${VARIABLE}_LIBRARIES NAMES ${LIBRARY} PATH_SUFFIXES ${ARGN}) 8 | GET_FILENAME_COMPONENT(${VARIABLE}_LIBRARY_DIRS ${${VARIABLE}_LIBRARIES} PATH CACHE) 9 | 10 | STRING(TOLOWER ${VARIABLE} LIBRARY_NAME) 11 | 12 | IF(NOT ${VARIABLE}_INCLUDE_DIRS OR NOT ${VARIABLE}_LIBRARY_DIRS) 13 | MESSAGE(FATAL_ERROR "${LIBRARY_NAME} development files are required to build.") 14 | ELSE() 15 | MESSAGE(STATUS "Found ${LIBRARY_NAME}: ${${VARIABLE}_LIBRARIES}") 16 | ENDIF() 17 | ENDFUNCTION() 18 | 19 | -------------------------------------------------------------------------------- /config.hpp.in: -------------------------------------------------------------------------------- 1 | #define COCAINE_VERSION_MAJOR ${COCAINE_VERSION_MAJOR} 2 | #define COCAINE_VERSION_MINOR ${COCAINE_VERSION_MINOR} 3 | #define COCAINE_VERSION_RELEASE ${COCAINE_VERSION_RELEASE} 4 | 5 | #define COCAINE_MAKE_VERSION(major, minor, release) \ 6 | ((major) * 10000 + (minor) * 100 + (release)) 7 | 8 | #define COCAINE_VERSION \ 9 | COCAINE_MAKE_VERSION(COCAINE_VERSION_MAJOR, COCAINE_VERSION_MINOR, COCAINE_VERSION_RELEASE) 10 | 11 | #define PACKAGE "cocaine-core" 12 | #define PACKAGE_VERSION COCAINE_VERSION 13 | -------------------------------------------------------------------------------- /debian/cocaine-runtime.conf: -------------------------------------------------------------------------------- 1 | { 2 | "version": 4, 3 | "logging": { 4 | "loggers" : { 5 | "core" : [ 6 | { 7 | "formatter": { 8 | "type": "string", 9 | "pattern": "{severity}, {timestamp:.23s}: {message} [{...:{{name}={value}:p}s}]", 10 | "sevmap": ["D", "I", "W", "E"] 11 | }, 12 | "sinks": [ 13 | { 14 | "type": "console" 15 | } 16 | ] 17 | } 18 | ] 19 | }, 20 | "severity" : "info" 21 | }, 22 | "network": { 23 | "pinned": { 24 | "locator": 10053 25 | } 26 | }, 27 | "paths": { 28 | "plugins": ["/usr/lib/cocaine"], 29 | "runtime": "/var/run/cocaine" 30 | }, 31 | "services": { 32 | "locator": { 33 | "type": "locator" 34 | }, 35 | "logging": { 36 | "type": "logging" 37 | }, 38 | "storage": { 39 | "type": "storage", 40 | "args": { 41 | "backend": "core" 42 | } 43 | } 44 | }, 45 | "storages": { 46 | "core": { 47 | "type": "files", 48 | "args": { 49 | "path": "/var/lib/cocaine" 50 | } 51 | } 52 | }, 53 | "authentications": { 54 | "core": { 55 | "type": "promiscuous" 56 | }, 57 | }, 58 | "authorizations": { 59 | "event": { 60 | "type": "disabled" 61 | }, 62 | "storage": { 63 | "type": "storage", 64 | "args": { 65 | "backend": "core" 66 | } 67 | }, 68 | "unicorn": { 69 | "type": "unicorn", 70 | "args": { 71 | "backend": "auth" 72 | } 73 | } 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /debian/cocaine-runtime.default: -------------------------------------------------------------------------------- 1 | CONFIG_PATH="/etc/cocaine/cocaine-default.conf" 2 | RUNTIME_PATH="/var/run/cocaine" 3 | DISABLE_COCAINE_SERVICE=true 4 | -------------------------------------------------------------------------------- /debian/cocaine-runtime.dirs: -------------------------------------------------------------------------------- 1 | var/cache/cocaine 2 | var/lib/cocaine 3 | var/spool/cocaine 4 | -------------------------------------------------------------------------------- /debian/cocaine-runtime.install: -------------------------------------------------------------------------------- 1 | etc/cocaine/cocaine-default.conf 2 | usr/bin/cocaine-runtime 3 | -------------------------------------------------------------------------------- /debian/cocaine-runtime.postinst: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -e 4 | 5 | case $1 in 6 | configure) 7 | # Create a new system user to run the cocaine 8 | # and to own the cocaine runtime resources 9 | adduser --quiet --system --ingroup adm cocaine 10 | 11 | # Set the correct permissions on the cocaine 12 | # runtime resources directory 13 | chown -R cocaine /var/{lib,cache,spool}/cocaine 14 | ;; 15 | 16 | abort-upgrade|abort-remove|abort-deconfigure) 17 | ;; 18 | 19 | *) 20 | echo "postinst called with an unknown argument \`$1'" >&2 21 | ;; 22 | esac 23 | 24 | #DEBHELPER# 25 | 26 | exit 0 27 | -------------------------------------------------------------------------------- /debian/cocaine-runtime.postrm: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -e 4 | 5 | #DEBHELPER# 6 | 7 | case $1 in 8 | purge) 9 | deluser --quiet --system cocaine 10 | ;; 11 | 12 | upgrade|disappear|failed-upgrade|abort-install|abort-upgrade) 13 | ;; 14 | 15 | *) 16 | echo "postrm called with an unknown argument \`$1'" >&2 17 | ;; 18 | esac 19 | 20 | exit 0 21 | -------------------------------------------------------------------------------- /debian/compat: -------------------------------------------------------------------------------- 1 | 7 2 | -------------------------------------------------------------------------------- /debian/control: -------------------------------------------------------------------------------- 1 | Source: cocaine-core 2 | Section: utils 3 | Priority: extra 4 | Maintainer: Andrey Sibiryov 5 | Build-Depends: cmake, cdbs, debhelper (>= 7.0.13), libltdl-dev, libmsgpack-dev, 6 | libmhash-dev, libarchive-dev, uuid-dev, libcgroup-dev, binutils-dev, 7 | libboost-dev, 8 | libboost-filesystem-dev, 9 | libboost-thread-dev, 10 | libboost-program-options-dev, 11 | metrics-dev (>= 3.0.0), 12 | blackhole-dev (>= 1.5.0) 13 | Standards-Version: 3.9.5 14 | Vcs-Git: git://github.com/cocaine/cocaine-core.git 15 | Vcs-Browser: https://github.com/cocaine/cocaine-core 16 | 17 | Package: libcocaine-core3 18 | Architecture: any 19 | Depends: ${shlibs:Depends}, ${misc:Depends}, libcocaine-io-util3 (= ${binary:Version}) 20 | Breaks: libcocaine-plugin-ipvs3(<0.12.14.18) 21 | Description: Cocaine - Core Libraries 22 | Cocaine core libraries package. 23 | 24 | Package: libcocaine-io-util3 25 | Architecture: any 26 | Depends: ${shlibs:Depends}, ${misc:Depends} 27 | Description: Cocaine - Util Libraries. 28 | Cocaine common utility libraries package(HPACK, protocol, errors, tracing). 29 | 30 | Package: libcocaine-dev 31 | Architecture: any 32 | Section: libdevel 33 | Depends: ${shlibs:Depends}, ${misc:Depends}, libcocaine-core3 (= ${binary:Version}), libcocaine-io-util3 (= ${binary:Version}), 34 | libltdl-dev, libmsgpack-dev, libboost-dev, 35 | metrics-dev (>= 3.0.0), 36 | blackhole-dev (>= 1.5.0) 37 | Description: Cocaine - Development Headers 38 | Cocaine development headers package. 39 | 40 | Package: cocaine-dbg 41 | Architecture: any 42 | Section: debug 43 | Depends: ${shlibs:Depends}, ${misc:Depends}, libcocaine-core3 (= ${binary:Version}) 44 | Description: Cocaine - Debug Files 45 | Cocaine debug files and symbols. 46 | 47 | Package: cocaine-runtime 48 | Architecture: any 49 | Depends: ${shlibs:Depends}, ${misc:Depends}, libcocaine-core3 (= ${binary:Version}), 50 | adduser 51 | Conflicts: cocaine-server 52 | Description: Cocaine - Runtime 53 | Cocaine runtime components package. 54 | -------------------------------------------------------------------------------- /debian/copyright: -------------------------------------------------------------------------------- 1 | Copyright (c) 2011-2014 Andrey Sibiryov 2 | Copyright (c) 2011-2014 Other contributors as noted in the AUTHORS file. 3 | 4 | This file is part of Cocaine. 5 | 6 | Cocaine is free software; you can redistribute it and/or modify 7 | it under the terms of the GNU Lesser General Public License as published by 8 | the Free Software Foundation; either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | Cocaine is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU Lesser General Public License for more details. 15 | 16 | You should have received a copy of the GNU Lesser General Public License 17 | along with this program. If not, see . 18 | 19 | On Debian systems, the complete text of the GNU Lesser General 20 | Public License can be found in "/usr/share/common-licenses/LGPL-3" 21 | -------------------------------------------------------------------------------- /debian/libcocaine-core3.install: -------------------------------------------------------------------------------- 1 | usr/lib/libcocaine-core.so.* 2 | -------------------------------------------------------------------------------- /debian/libcocaine-dev.install: -------------------------------------------------------------------------------- 1 | usr/include/cocaine/* 2 | -------------------------------------------------------------------------------- /debian/libcocaine-dev.links: -------------------------------------------------------------------------------- 1 | usr/lib/libcocaine-core.so.3 usr/lib/libcocaine-core.so 2 | usr/lib/libcocaine-io-util.so.3 usr/lib/libcocaine-io-util.so 3 | -------------------------------------------------------------------------------- /debian/libcocaine-io-util3.install: -------------------------------------------------------------------------------- 1 | usr/lib/libcocaine-io-util.so.* 2 | -------------------------------------------------------------------------------- /debian/rules: -------------------------------------------------------------------------------- 1 | #!/usr/bin/make -f 2 | 3 | include /usr/share/cdbs/1/class/cmake.mk 4 | include /usr/share/cdbs/1/rules/debhelper.mk 5 | 6 | DEB_DBG_PACKAGES := cocaine-dbg 7 | DEB_DH_INSTALLINIT_ARGS := -r 8 | 9 | install/cocaine-runtime:: 10 | install -d $(DEB_DESTDIR)etc/cocaine 11 | install -m644 debian/cocaine-runtime.conf $(DEB_DESTDIR)etc/cocaine/cocaine-default.conf 12 | -------------------------------------------------------------------------------- /debian/source/format: -------------------------------------------------------------------------------- 1 | 3.0 (native) 2 | -------------------------------------------------------------------------------- /docker/.dockerignore: -------------------------------------------------------------------------------- 1 | .git 2 | -------------------------------------------------------------------------------- /docker/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM ubuntu:trusty 2 | 3 | MAINTAINER Andrey Sibiryov "me@kobology.ru" 4 | 5 | RUN DEBIAN_FRONTEND=noninteractive apt-get -qq update 6 | RUN DEBIAN_FRONTEND=noninteractive apt-get -qq install build-essential devscripts equivs git-core 7 | 8 | # Fetch the latest codebase 9 | RUN git clone https://github.com/cocaine/cocaine-core --recursive -b master building/cocaine-core 10 | 11 | # Install build dependencies 12 | RUN cd building/cocaine-core && \ 13 | DEBIAN_FRONTEND=noninteractive mk-build-deps -ir -t "apt-get -qq --no-install-recommends" 14 | 15 | # Build and install 16 | RUN cd building/cocaine-core && \ 17 | debuild -e CC -e CXX -uc -us -j$(cat /proc/cpuinfo | fgrep -c processor) && \ 18 | debi 19 | 20 | # Cleanup 21 | RUN DEBIAN_FRONTEND=noninteractive apt-get -qq purge cocaine-core-build-deps && \ 22 | DEBIAN_FRONTEND=noninteractive apt-get -qq purge build-essential devscripts equivs git-core && \ 23 | DEBIAN_FRONTEND=noninteractive apt-get -qq autoremove --purge && \ 24 | rm -rf building 25 | 26 | # Setup runtime environment 27 | RUN mkdir -p /var/run/cocaine 28 | 29 | EXPOSE 10053 30 | 31 | CMD ["cocaine-runtime", "-c", "/etc/cocaine/cocaine-default.conf"] 32 | -------------------------------------------------------------------------------- /fedora/cocaine-runtime.service: -------------------------------------------------------------------------------- 1 | [Unit] 2 | Description=Cocaine runtime components 3 | After=syslog.target network.target 4 | 5 | [Service] 6 | PIDFile=/var/run/cocaine-runtime.pid 7 | ExecStart=/usr/bin/cocaine-runtime -c /etc/cocaine/cocaine-default.conf 8 | User=root 9 | Group=root 10 | 11 | [Install] 12 | WantedBy=multi-user.target 13 | -------------------------------------------------------------------------------- /fedora/cocaine-runtime.tmpfiles: -------------------------------------------------------------------------------- 1 | # cocaine-runtime runtime directory 2 | d /var/run/cocaine 0755 root root - 3 | -------------------------------------------------------------------------------- /include/cocaine/api/authentication.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #include 9 | 10 | #include "cocaine/auth/uid.hpp" 11 | #include "cocaine/forwards.hpp" 12 | #include "cocaine/hpack/header.hpp" 13 | 14 | namespace cocaine { 15 | namespace api { 16 | 17 | /// Identification and autentification interface. 18 | class authentication_t { 19 | public: 20 | typedef authentication_t category_type; 21 | typedef std::chrono::system_clock clock_type; 22 | 23 | struct token_t { 24 | /// Token type. Think of the first part of the Authorization HTTP header. 25 | std::string type; 26 | 27 | /// Token body. 28 | std::string body; 29 | 30 | /// Time point when the token becomes expired. Note that due to NTP misconfiguration, 31 | /// slow network of whatever else the token may become expired in unexpected way. Use this 32 | /// information as a cache hint. 33 | clock_type::time_point expires_in; 34 | 35 | token_t() = default; 36 | 37 | /// Returns true if the token is expired. 38 | auto 39 | expired() const -> bool { 40 | return clock_type::now() >= expires_in; 41 | } 42 | }; 43 | 44 | typedef std::function callback_type; 45 | 46 | public: 47 | virtual 48 | ~authentication_t() = default; 49 | 50 | /// Tries to obtain, possibly asynchronously, an authorization token with its type and optional 51 | /// expiration time point. 52 | /// 53 | /// The implementation is free to cache obtained token. 54 | /// 55 | /// \param callback The function which is called on either when the token is read or any error. 56 | /// It's strongly recommended to wrap the callback with an executor to avoid deadlocks or 57 | /// inner threads context switching. 58 | virtual 59 | auto 60 | token(callback_type callback) -> void = 0; 61 | 62 | /// Extracts an authorization header from the given headers with its further identification 63 | /// check. 64 | /// 65 | /// \tparam Event Requested event. 66 | /// \param headers HPACK headers. 67 | virtual 68 | auto 69 | identify(const hpack::headers_t& headers) const -> auth::identity_t; 70 | 71 | /// Performs an identification check for a given entity represented with a token. 72 | /// 73 | /// \param event Requested event. 74 | /// \param token Identification credentials. 75 | virtual 76 | auto 77 | identify(const std::string& credentials) const -> auth::identity_t = 0; 78 | }; 79 | 80 | auto 81 | authentication(context_t& context, const std::string& name, const std::string& service) -> 82 | std::shared_ptr; 83 | 84 | } // namespace api 85 | } // namespace cocaine 86 | -------------------------------------------------------------------------------- /include/cocaine/api/authorization/event.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | 6 | #include "cocaine/forwards.hpp" 7 | #include "cocaine/rpc/protocol.hpp" 8 | 9 | namespace cocaine { 10 | namespace api { 11 | namespace authorization { 12 | 13 | /// Event-based authorization. 14 | class event_t { 15 | public: 16 | typedef event_t category_type; 17 | 18 | virtual 19 | ~event_t() = default; 20 | 21 | template 22 | auto 23 | verify(const auth::identity_t& identity) -> void { 24 | verify(Event::alias(), identity); 25 | } 26 | 27 | virtual 28 | auto 29 | verify(const std::string& event, const auth::identity_t& identity) -> void = 0; 30 | }; 31 | 32 | auto 33 | event(context_t& context, const std::string& service) -> std::shared_ptr; 34 | 35 | } // namespace authorization 36 | } // namespace api 37 | } // namespace cocaine 38 | -------------------------------------------------------------------------------- /include/cocaine/api/authorization/storage.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #include "cocaine/auth/uid.hpp" 10 | #include "cocaine/forwards.hpp" 11 | #include "cocaine/rpc/protocol.hpp" 12 | 13 | namespace cocaine { 14 | namespace api { 15 | namespace authorization { 16 | 17 | class storage_t { 18 | public: 19 | typedef storage_t category_type; 20 | typedef std::function callback_type; 21 | 22 | virtual 23 | ~storage_t() = default; 24 | 25 | template 26 | auto 27 | verify(const std::string& collection, const std::string& key, const auth::identity_t& identity, callback_type callback) -> void { 28 | verify(io::event_traits::id, collection, key, identity, std::move(callback)); 29 | } 30 | 31 | /// Verifies access to a collection for the given identity. 32 | /// 33 | /// An implementation is meant to match the underlying access rights with users. An exception 34 | /// must be thrown on any unauthorized access as like as access mismatch. 35 | /// 36 | /// \overload 37 | /// \param event Event to identify access rights. 38 | /// \param collection Collection to operate with. 39 | /// \param key Key to operate with. 40 | /// \param identity Identity of a user or users ithat wish to operate with the resource. 41 | virtual 42 | auto 43 | verify(std::size_t event, const std::string& collection, const std::string& key, const auth::identity_t& identity, callback_type callback) 44 | -> void = 0; 45 | }; 46 | 47 | auto 48 | storage(context_t& context, const std::string& service) -> std::shared_ptr; 49 | 50 | } // namespace authorization 51 | } // namespace api 52 | } // namespace cocaine 53 | -------------------------------------------------------------------------------- /include/cocaine/api/authorization/unicorn.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #include "cocaine/auth/uid.hpp" 10 | #include "cocaine/forwards.hpp" 11 | #include "cocaine/rpc/protocol.hpp" 12 | 13 | namespace cocaine { 14 | namespace api { 15 | namespace authorization { 16 | 17 | class unicorn_t { 18 | public: 19 | typedef unicorn_t category_type; 20 | typedef std::function callback_type; 21 | 22 | virtual 23 | ~unicorn_t() = default; 24 | 25 | template 26 | auto 27 | verify(const std::string& path, const auth::identity_t& identity, callback_type callback) -> void { 28 | verify(io::event_traits::id, path, identity, std::move(callback)); 29 | } 30 | 31 | virtual 32 | auto 33 | verify(std::size_t event, const std::string& path, const auth::identity_t& identity, callback_type callback) 34 | -> void = 0; 35 | }; 36 | 37 | auto 38 | unicorn(context_t& context, const std::string& service) -> std::shared_ptr; 39 | 40 | } // namespace authorization 41 | } // namespace api 42 | } // namespace cocaine 43 | -------------------------------------------------------------------------------- /include/cocaine/api/cluster.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2011-2014 Andrey Sibiryov 3 | Copyright (c) 2011-2014 Other contributors as noted in the AUTHORS file. 4 | 5 | This file is part of Cocaine. 6 | 7 | Cocaine is free software; you can redistribute it and/or modify 8 | it under the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | Cocaine is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #ifndef COCAINE_CLUSTER_API_HPP 22 | #define COCAINE_CLUSTER_API_HPP 23 | 24 | #include "cocaine/common.hpp" 25 | 26 | #include 27 | 28 | namespace cocaine { namespace api { 29 | 30 | struct cluster_t { 31 | typedef cluster_t category_type; 32 | 33 | enum class mode_t { 34 | full, 35 | announce_only 36 | }; 37 | 38 | struct interface { 39 | virtual 40 | auto 41 | asio() -> asio::io_service& = 0; 42 | 43 | virtual 44 | void 45 | link_node(const std::string& uuid, const std::vector& endpoints) = 0; 46 | 47 | virtual 48 | void 49 | drop_node(const std::string& uuid) = 0; 50 | 51 | virtual 52 | auto 53 | uuid() const -> std::string = 0; 54 | }; 55 | 56 | virtual 57 | ~cluster_t() { 58 | // Empty. 59 | } 60 | 61 | protected: 62 | cluster_t(context_t&, interface&, mode_t /* mode */ , const std::string& /* name */, const dynamic_t& /* args */) { 63 | // Empty. 64 | } 65 | }; 66 | 67 | typedef std::unique_ptr cluster_ptr; 68 | 69 | }} // namespace cocaine::api 70 | 71 | #endif 72 | -------------------------------------------------------------------------------- /include/cocaine/api/executor.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | namespace cocaine { 8 | namespace api { 9 | 10 | class executor_t { 11 | template 12 | class move_only_wrapper { 13 | public: 14 | move_only_wrapper(F _f): 15 | f(std::make_shared(std::move(_f))) 16 | {} 17 | 18 | auto 19 | operator()() -> void { 20 | return (*f)(); 21 | } 22 | 23 | private: 24 | std::shared_ptr f; 25 | }; 26 | public: 27 | typedef std::function work_t; 28 | 29 | virtual 30 | ~executor_t() = default; 31 | 32 | virtual 33 | auto 34 | spawn(work_t work) -> void = 0; 35 | 36 | template 37 | auto 38 | spawn(F f) -> typename std::enable_if::value>::type { 39 | spawn(move_only_wrapper(std::move(f))); 40 | } 41 | }; 42 | 43 | } // namespace api 44 | } // namespace cocaine 45 | -------------------------------------------------------------------------------- /include/cocaine/api/service.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2011-2014 Andrey Sibiryov 3 | Copyright (c) 2011-2014 Other contributors as noted in the AUTHORS file. 4 | 5 | This file is part of Cocaine. 6 | 7 | Cocaine is free software; you can redistribute it and/or modify 8 | it under the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | Cocaine is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #ifndef COCAINE_SERVICE_API_HPP 22 | #define COCAINE_SERVICE_API_HPP 23 | 24 | #include "cocaine/common.hpp" 25 | 26 | namespace cocaine { namespace api { 27 | 28 | struct service_t { 29 | typedef service_t category_type; 30 | 31 | virtual 32 | ~service_t() = default; 33 | 34 | virtual 35 | auto 36 | prototype() -> io::basic_dispatch_t& = 0; 37 | 38 | protected: 39 | service_t(context_t&, asio::io_service&, const std::string& /* name */, const dynamic_t& /* args */) { 40 | // Empty. 41 | } 42 | }; 43 | 44 | typedef std::unique_ptr service_ptr; 45 | 46 | }} // namespace cocaine::api 47 | 48 | #endif 49 | -------------------------------------------------------------------------------- /include/cocaine/auth/uid.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | namespace cocaine { 9 | namespace auth { 10 | 11 | using cid_t = std::uint32_t; 12 | using uid_t = std::uint64_t; 13 | 14 | static constexpr uid_t superuser = std::numeric_limits::min(); 15 | static constexpr uid_t anonymous = std::numeric_limits::max(); 16 | 17 | /// Identification information of a request. 18 | class identity_t { 19 | struct data_t; 20 | std::shared_ptr d; 21 | 22 | public: 23 | class builder_t { 24 | std::unique_ptr d; 25 | 26 | public: 27 | builder_t(); 28 | ~builder_t(); 29 | 30 | auto 31 | cids(std::vector cids) & -> builder_t&; 32 | 33 | auto 34 | cids(std::vector cids) && -> builder_t&&; 35 | 36 | auto 37 | uids(std::vector uids) & -> builder_t&; 38 | 39 | auto 40 | uids(std::vector uids) && -> builder_t&&; 41 | 42 | auto 43 | build() && -> identity_t; 44 | }; 45 | 46 | public: 47 | /// Constructs an anonymous identity. 48 | identity_t(); 49 | 50 | /// Constructs an identity using private data. 51 | explicit identity_t(std::unique_ptr d); 52 | 53 | /// Constructs an identity with the given UID's. 54 | __attribute__((deprecated("use `builder_t` instead"))) 55 | explicit identity_t(std::vector uids); 56 | 57 | /// Returns the client ids. 58 | auto 59 | cids() const -> const std::vector&; 60 | 61 | /// Returns list of user identificators. 62 | auto 63 | uids() const -> const std::vector&; 64 | }; 65 | 66 | } // namespace auth 67 | } // namespace cocaine 68 | -------------------------------------------------------------------------------- /include/cocaine/common.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2011-2014 Andrey Sibiryov 3 | Copyright (c) 2011-2014 Other contributors as noted in the AUTHORS file. 4 | 5 | This file is part of Cocaine. 6 | 7 | Cocaine is free software; you can redistribute it and/or modify 8 | it under the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | Cocaine is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #ifndef COCAINE_COMMON_HPP 22 | #define COCAINE_COMMON_HPP 23 | 24 | #include "cocaine/config.hpp" 25 | #include "cocaine/platform.hpp" 26 | 27 | #define BOOST_FILESYSTEM_VERSION 3 28 | #define BOOST_FILESYSTEM_NO_DEPRECATED 29 | 30 | #define COCAINE_DECLARE_NONCOPYABLE(type) \ 31 | type(const type& other) = delete; \ 32 | \ 33 | type& \ 34 | operator=(const type& other) = delete; 35 | 36 | #define COCAINE_UNUSED_(parameter) \ 37 | parameter __attribute__((unused)) 38 | 39 | #include "cocaine/forwards.hpp" 40 | 41 | #endif 42 | -------------------------------------------------------------------------------- /include/cocaine/context/filter.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2016+ Anton Matveenko 3 | Copyright (c) 2016+ Other contributors as noted in the AUTHORS file. 4 | 5 | This file is part of Cocaine. 6 | 7 | Cocaine is free software; you can redistribute it and/or modify 8 | it under the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | Cocaine is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #ifndef COCAINE_CONTEXT_FILTER_HPP 22 | #define COCAINE_CONTEXT_FILTER_HPP 23 | 24 | #include 25 | #include 26 | #include 27 | #include 28 | 29 | namespace cocaine { 30 | 31 | /** 32 | * It is a wrapper on std::function 33 | * We use this, because class unlike typedef can be forwarded 34 | **/ 35 | class filter_t { 36 | public: 37 | typedef blackhole::severity_t severity_t; 38 | typedef blackhole::attribute_pack attribute_pack; 39 | 40 | typedef std::function inner_t; 41 | 42 | filter_t(inner_t _inner) : inner(std::move(_inner)) {} 43 | 44 | bool operator()(severity_t severity, attribute_pack& pack) const { 45 | return inner(severity, pack); 46 | } 47 | private: 48 | inner_t inner; 49 | }; 50 | 51 | } 52 | #endif 53 | -------------------------------------------------------------------------------- /include/cocaine/context/mapper.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2011-2015 Andrey Sibiryov 3 | Copyright (c) 2011-2015 Other contributors as noted in the AUTHORS file. 4 | 5 | This file is part of Cocaine. 6 | 7 | Cocaine is free software; you can redistribute it and/or modify 8 | it under the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | Cocaine is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #ifndef COCAINE_CONTEXT_MAPPER_HPP 22 | #define COCAINE_CONTEXT_MAPPER_HPP 23 | 24 | #include "cocaine/common.hpp" 25 | 26 | #include 27 | #include 28 | #include 29 | 30 | namespace cocaine { 31 | 32 | // Dynamic port mapper 33 | 34 | class port_mapping_t { 35 | const 36 | std::map m_pinned; 37 | 38 | // Ports available for dynamic allocation. 39 | std::deque m_shared; 40 | std::mutex m_mutex; 41 | 42 | // Ports currently in use. 43 | std::map m_in_use; 44 | 45 | public: 46 | explicit 47 | port_mapping_t(const struct config_t& config); 48 | 49 | // Modifiers 50 | 51 | port_t 52 | assign(const std::string& name); 53 | 54 | void 55 | retain(const std::string& name); 56 | }; 57 | 58 | } // namespace cocaine 59 | 60 | #endif 61 | -------------------------------------------------------------------------------- /include/cocaine/context/quote.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2016+ Anton Matveenko 3 | Copyright (c) 2016+ Other contributors as noted in the AUTHORS file. 4 | 5 | This file is part of Cocaine. 6 | 7 | Cocaine is free software; you can redistribute it and/or modify 8 | it under the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | Cocaine is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #ifndef COCAINE_CONTEXT_QUOTE_HPP 22 | #define COCAINE_CONTEXT_QUOTE_HPP 23 | 24 | #include "cocaine/forwards.hpp" 25 | 26 | #include 27 | #include 28 | 29 | namespace cocaine { namespace context { 30 | 31 | struct quote_t { 32 | std::vector endpoints; 33 | io::dispatch_ptr_t prototype; 34 | }; 35 | 36 | }} // namespace cocaine::context 37 | 38 | #endif 39 | -------------------------------------------------------------------------------- /include/cocaine/defaults.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2011-2014 Andrey Sibiryov 3 | Copyright (c) 2011-2014 Other contributors as noted in the AUTHORS file. 4 | 5 | This file is part of Cocaine. 6 | 7 | Cocaine is free software; you can redistribute it and/or modify 8 | it under the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | Cocaine is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #ifndef COCAINE_DEFAULTS_HPP 22 | #define COCAINE_DEFAULTS_HPP 23 | 24 | #include 25 | 26 | namespace cocaine { 27 | 28 | struct defaults { 29 | // Default paths. 30 | static const std::string plugins_path; 31 | static const std::string runtime_path; 32 | 33 | // Defaults for networking. 34 | static const std::string endpoint; 35 | 36 | // Defaults for logging service. 37 | static const std::string log_verbosity; 38 | static const std::string log_timestamp; 39 | }; 40 | 41 | } // namespace cocaine 42 | 43 | #endif -------------------------------------------------------------------------------- /include/cocaine/detail/cluster/multicast.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2011-2014 Andrey Sibiryov 3 | Copyright (c) 2011-2014 Other contributors as noted in the AUTHORS file. 4 | 5 | This file is part of Cocaine. 6 | 7 | Cocaine is free software; you can redistribute it and/or modify 8 | it under the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | Cocaine is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #ifndef COCAINE_MULTICAST_CLUSTER_HPP 22 | #define COCAINE_MULTICAST_CLUSTER_HPP 23 | 24 | #include "cocaine/api/cluster.hpp" 25 | 26 | #include "cocaine/idl/context.hpp" 27 | 28 | #include 29 | 30 | #include 31 | #include 32 | 33 | namespace cocaine { namespace cluster { 34 | 35 | class multicast_cfg_t 36 | { 37 | public: 38 | // An UDP endpoint to bind for multicast node announces. Not a multicast group. 39 | asio::ip::udp::endpoint endpoint; 40 | 41 | // Will announce local endpoints to the specified multicast group every `interval` seconds. 42 | asio::deadline_timer::duration_type interval; 43 | }; 44 | 45 | class multicast_t: 46 | public api::cluster_t 47 | { 48 | struct announce_t; 49 | 50 | context_t& m_context; 51 | 52 | const std::unique_ptr m_log; 53 | 54 | // Interoperability with the locator service. 55 | interface& m_locator; 56 | 57 | // Component config. 58 | const multicast_cfg_t m_cfg; 59 | 60 | asio::ip::udp::socket m_socket; 61 | asio::deadline_timer m_timer; 62 | 63 | // Announce expiration timeouts. 64 | std::map> m_expirations; 65 | 66 | // Signal to handle context ready event 67 | std::shared_ptr> m_signals; 68 | 69 | public: 70 | multicast_t(context_t& context, interface& locator, mode_t mode, const std::string& name, const dynamic_t& args); 71 | 72 | virtual 73 | ~multicast_t(); 74 | 75 | private: 76 | void 77 | on_publish(const std::error_code& ec); 78 | 79 | void 80 | on_receive(const std::error_code& ec, size_t bytes_received, const std::shared_ptr& ptr); 81 | 82 | void 83 | on_expired(const std::error_code& ec, const std::string& uuid); 84 | }; 85 | 86 | }} // namespace cocaine::cluster 87 | 88 | #endif 89 | -------------------------------------------------------------------------------- /include/cocaine/detail/cluster/predefine.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2011-2014 Andrey Sibiryov 3 | Copyright (c) 2011-2014 Other contributors as noted in the AUTHORS file. 4 | 5 | This file is part of Cocaine. 6 | 7 | Cocaine is free software; you can redistribute it and/or modify 8 | it under the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | Cocaine is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #ifndef COCAINE_PREDEFINE_CLUSTER_HPP 22 | #define COCAINE_PREDEFINE_CLUSTER_HPP 23 | 24 | #include "cocaine/api/cluster.hpp" 25 | 26 | #include "cocaine/idl/context.hpp" 27 | 28 | #include 29 | #include 30 | 31 | namespace cocaine { namespace cluster { 32 | 33 | class predefine_cfg_t 34 | { 35 | public: 36 | // Maps randomly generated UUIDs to predefined host endpoints. 37 | std::map> endpoints; 38 | 39 | // Will try to reconnect to the hosts specified above every `interval` seconds. 40 | asio::deadline_timer::duration_type interval; 41 | }; 42 | 43 | class predefine_t: 44 | public api::cluster_t 45 | { 46 | const std::unique_ptr m_log; 47 | 48 | // Interoperability with the locator service. 49 | interface& m_locator; 50 | 51 | // Component config. 52 | const predefine_cfg_t m_cfg; 53 | 54 | // Simply try linking the whole predefined list every timer tick. 55 | asio::deadline_timer m_timer; 56 | 57 | // Slot for context singals. 58 | std::shared_ptr> m_signals; 59 | 60 | public: 61 | predefine_t(context_t& context, interface& locator, mode_t mode, const std::string& name, const dynamic_t& args); 62 | 63 | virtual 64 | ~predefine_t(); 65 | 66 | private: 67 | void 68 | on_announce(const std::error_code& ec); 69 | }; 70 | 71 | }} // namespace cocaine::cluster 72 | 73 | #endif 74 | -------------------------------------------------------------------------------- /include/cocaine/detail/crypto.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2011-2014 Andrey Sibiryov 3 | Copyright (c) 2011-2014 Other contributors as noted in the AUTHORS file. 4 | 5 | This file is part of Cocaine. 6 | 7 | Cocaine is free software; you can redistribute it and/or modify 8 | it under the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | Cocaine is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #ifndef COCAINE_AUTH_HPP 22 | #define COCAINE_AUTH_HPP 23 | 24 | #include "cocaine/common.hpp" 25 | 26 | #include "cocaine/api/storage.hpp" 27 | 28 | #define PROTOTYPES 29 | #include 30 | #include 31 | 32 | namespace cocaine { 33 | 34 | template 35 | class crypto { 36 | COCAINE_DECLARE_NONCOPYABLE(crypto) 37 | 38 | const std::unique_ptr m_log; 39 | const std::string m_service; 40 | 41 | api::storage_ptr m_store; 42 | 43 | public: 44 | crypto(context_t& context, const std::string& service); 45 | ~crypto(); 46 | 47 | std::string 48 | sign(const std::string& message, const std::string& token_id) const; 49 | }; 50 | 51 | typedef crypto crypto_t; 52 | 53 | } // namespace cocaine 54 | 55 | #endif 56 | -------------------------------------------------------------------------------- /include/cocaine/detail/essentials.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2011-2014 Andrey Sibiryov 3 | Copyright (c) 2011-2014 Other contributors as noted in the AUTHORS file. 4 | 5 | This file is part of Cocaine. 6 | 7 | Cocaine is free software; you can redistribute it and/or modify 8 | it under the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | Cocaine is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #include "cocaine/repository.hpp" 22 | 23 | namespace cocaine { namespace essentials { 24 | 25 | void 26 | initialize(api::repository_t& repository); 27 | 28 | }} // namespace cocaine::essentials 29 | -------------------------------------------------------------------------------- /include/cocaine/detail/runtime/logging.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2011-2014 Andrey Sibiryov 3 | Copyright (c) 2011-2014 Other contributors as noted in the AUTHORS file. 4 | 5 | This file is part of Cocaine. 6 | 7 | Cocaine is free software; you can redistribute it and/or modify 8 | it under the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | Cocaine is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #ifndef COCAINE_BOOTSTRAP_LOGGING_HPP 22 | #define COCAINE_BOOTSTRAP_LOGGING_HPP 23 | 24 | #include 25 | 26 | namespace cocaine { namespace logging { 27 | 28 | class console_t; 29 | 30 | }} 31 | 32 | namespace blackhole { 33 | inline namespace v1 { 34 | 35 | template<> 36 | struct factory : public factory { 37 | auto type() const noexcept -> const char*; 38 | auto from(const config::node_t& config) const -> std::unique_ptr; 39 | }; 40 | 41 | } // namespace v1 42 | } // namespace blackhole 43 | 44 | #endif 45 | -------------------------------------------------------------------------------- /include/cocaine/detail/runtime/pid_file.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2011-2014 Andrey Sibiryov 3 | Copyright (c) 2011-2014 Other contributors as noted in the AUTHORS file. 4 | 5 | This file is part of Cocaine. 6 | 7 | Cocaine is free software; you can redistribute it and/or modify 8 | it under the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | Cocaine is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #ifndef COCAINE_BOOTSTRAP_PIDFILE_HPP 22 | #define COCAINE_BOOTSTRAP_PIDFILE_HPP 23 | 24 | #include "cocaine/common.hpp" 25 | 26 | #include 27 | 28 | namespace cocaine { 29 | 30 | class pid_file_t { 31 | COCAINE_DECLARE_NONCOPYABLE(pid_file_t) 32 | 33 | const boost::filesystem::path m_filepath; 34 | 35 | public: 36 | pid_file_t(const boost::filesystem::path& filepath); 37 | ~pid_file_t(); 38 | 39 | private: 40 | void 41 | remove(); 42 | }; 43 | 44 | } // namespace cocaine 45 | 46 | #endif 47 | -------------------------------------------------------------------------------- /include/cocaine/detail/service/locator/routing.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2011-2014 Andrey Sibiryov 3 | Copyright (c) 2011-2014 Other contributors as noted in the AUTHORS file. 4 | 5 | This file is part of Cocaine. 6 | 7 | Cocaine is free software; you can redistribute it and/or modify 8 | it under the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | Cocaine is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #ifndef COCAINE_LOCATOR_SERVICE_ROUTING_HPP 22 | #define COCAINE_LOCATOR_SERVICE_ROUTING_HPP 23 | 24 | #include "cocaine/common.hpp" 25 | 26 | #include 27 | #include 28 | 29 | namespace cocaine { namespace service { 30 | 31 | // Ketama algorithm implementation 32 | 33 | struct continuum_t { 34 | typedef uint32_t point_type; 35 | 36 | typedef struct element_type { 37 | point_type point; 38 | std::string value; 39 | } element_t; 40 | 41 | friend 42 | bool 43 | operator< (const element_t& lhs, const element_t& rhs) { 44 | return lhs.point < rhs.point; 45 | } 46 | 47 | friend 48 | bool 49 | operator==(const element_t& lhs, const element_t& rhs) { 50 | return lhs.point == rhs.point; 51 | } 52 | 53 | friend 54 | bool 55 | operator< (const point_type lhs, const element_t& rhs) { 56 | return lhs < rhs.point; 57 | } 58 | 59 | typedef std::map stored_type; 60 | 61 | public: 62 | continuum_t(std::unique_ptr log, const stored_type& group); 63 | 64 | // Observers 65 | 66 | std::string 67 | get(const std::string& key) const; 68 | 69 | std::string 70 | get() const; 71 | 72 | std::vector> 73 | all() const; 74 | 75 | private: 76 | // Shared to allow cloning of rg_map_t for routing group updates. 77 | const std::shared_ptr m_log; 78 | 79 | // The hashring. 80 | std::vector m_elements; 81 | 82 | // Used for keyless operations. 83 | std::default_random_engine mutable m_rng; 84 | std::uniform_int_distribution mutable m_distribution; 85 | }; 86 | 87 | }} // namespace cocaine::service 88 | 89 | #endif 90 | -------------------------------------------------------------------------------- /include/cocaine/detail/service/logging.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2011-2014 Andrey Sibiryov 3 | Copyright (c) 2011-2014 Other contributors as noted in the AUTHORS file. 4 | 5 | This file is part of Cocaine. 6 | 7 | Cocaine is free software; you can redistribute it and/or modify 8 | it under the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | Cocaine is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #ifndef COCAINE_LOGGING_SERVICE_HPP 22 | #define COCAINE_LOGGING_SERVICE_HPP 23 | 24 | #include "cocaine/api/service.hpp" 25 | 26 | #include "cocaine/idl/context.hpp" 27 | #include "cocaine/idl/logging.hpp" 28 | #include "cocaine/rpc/dispatch.hpp" 29 | 30 | namespace cocaine { namespace service { 31 | 32 | class logging_t: 33 | public api::service_t, 34 | public dispatch 35 | { 36 | logging::priorities verbosity; 37 | std::unique_ptr logger; 38 | std::shared_ptr> signals; 39 | 40 | public: 41 | logging_t(context_t& context, asio::io_service& asio, const std::string& name, const dynamic_t& args); 42 | 43 | virtual 44 | auto 45 | prototype() -> io::basic_dispatch_t&; 46 | 47 | private: 48 | void 49 | on_emit(logging::priorities level, std::string source, std::string message, 50 | blackhole::attributes_t attributes); 51 | 52 | auto 53 | on_verbosity() const -> logging::priorities; 54 | }; 55 | 56 | }} // namespace cocaine::service 57 | 58 | #endif 59 | -------------------------------------------------------------------------------- /include/cocaine/detail/service/storage.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2011-2014 Andrey Sibiryov 3 | Copyright (c) 2011-2014 Other contributors as noted in the AUTHORS file. 4 | 5 | This file is part of Cocaine. 6 | 7 | Cocaine is free software; you can redistribute it and/or modify 8 | it under the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | Cocaine is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #pragma once 22 | 23 | #include "cocaine/api/service.hpp" 24 | #include "cocaine/idl/storage.hpp" 25 | #include "cocaine/rpc/dispatch.hpp" 26 | 27 | namespace cocaine { 28 | namespace service { 29 | 30 | struct storage_t: 31 | public api::service_t, 32 | public dispatch 33 | { 34 | storage_t(context_t& context, asio::io_service& asio, const std::string& name, const dynamic_t& args); 35 | 36 | virtual 37 | auto 38 | prototype() -> io::basic_dispatch_t&; 39 | }; 40 | 41 | } // namespace service 42 | } // namespace cocaine 43 | -------------------------------------------------------------------------------- /include/cocaine/dynamic.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2011-2014 Andrey Sibiryov 3 | Copyright (c) 2011-2014 Other contributors as noted in the AUTHORS file. 4 | 5 | This file is part of Cocaine. 6 | 7 | Cocaine is free software; you can redistribute it and/or modify 8 | it under the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | Cocaine is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #ifndef COCAINE_DYNAMIC_HPP 22 | #define COCAINE_DYNAMIC_HPP 23 | 24 | #include "cocaine/dynamic/dynamic.hpp" 25 | 26 | #endif 27 | -------------------------------------------------------------------------------- /include/cocaine/dynamic/constructors/endpoint.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2017+ Anton Matveenko 3 | Copyright (c) 2017+ Other contributors as noted in the AUTHORS file. 4 | 5 | This file is part of Cocaine. 6 | 7 | Cocaine is free software; you can redistribute it and/or modify 8 | it under the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | Cocaine is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #pragma once 22 | 23 | #include 24 | 25 | #include 26 | 27 | namespace cocaine { 28 | 29 | template<> 30 | struct dynamic_constructor { 31 | static const bool enable = true; 32 | 33 | static inline 34 | void 35 | convert(const asio::ip::tcp::endpoint& from, dynamic_t::value_t& to) { 36 | dynamic_constructor::convert(dynamic_t::array_t(), to); 37 | auto& array = boost::get>(to).get(); 38 | array.resize(2); 39 | array[0] = from.address().to_string(); 40 | array[1] = from.port(); 41 | } 42 | }; 43 | 44 | } // namespace cocaine 45 | -------------------------------------------------------------------------------- /include/cocaine/dynamic/constructors/graph.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2017+ Anton Matveenko 3 | Copyright (c) 2017+ Other contributors as noted in the AUTHORS file. 4 | 5 | This file is part of Cocaine. 6 | 7 | Cocaine is free software; you can redistribute it and/or modify 8 | it under the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | Cocaine is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #pragma once 22 | 23 | #include "cocaine/dynamic/constructors.hpp" 24 | #include "cocaine/dynamic/constructors/optional.hpp" 25 | #include "cocaine/rpc/graph.hpp" 26 | 27 | namespace cocaine { 28 | 29 | template<> 30 | struct dynamic_constructor: public dynamic_constructor {}; 31 | 32 | } // namespace cocaine 33 | -------------------------------------------------------------------------------- /include/cocaine/dynamic/constructors/optional.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2017+ Anton Matveenko 3 | Copyright (c) 2017+ Other contributors as noted in the AUTHORS file. 4 | 5 | This file is part of Cocaine. 6 | 7 | Cocaine is free software; you can redistribute it and/or modify 8 | it under the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | Cocaine is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #pragma once 22 | 23 | #include "cocaine/dynamic/constructors.hpp" 24 | 25 | #include 26 | 27 | namespace cocaine { 28 | 29 | template 30 | struct dynamic_constructor> { 31 | static const bool enable = true; 32 | 33 | static inline 34 | void 35 | convert(const boost::optional& from, dynamic_t::value_t& to) { 36 | if(from) { 37 | dynamic_constructor::type>::convert(*from, to); 38 | } else { 39 | to = dynamic_t::null_t(); 40 | } 41 | } 42 | }; 43 | 44 | } // namespace cocaine 45 | -------------------------------------------------------------------------------- /include/cocaine/dynamic/constructors/set.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2017+ Anton Matveenko 3 | Copyright (c) 2017+ Other contributors as noted in the AUTHORS file. 4 | 5 | This file is part of Cocaine. 6 | 7 | Cocaine is free software; you can redistribute it and/or modify 8 | it under the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | Cocaine is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #pragma once 22 | 23 | #include 24 | 25 | #include 26 | 27 | namespace cocaine { 28 | 29 | template 30 | struct dynamic_constructor> { 31 | static const bool enable = true; 32 | 33 | static inline 34 | void 35 | convert(const std::set& from, dynamic_t::value_t& to) { 36 | dynamic_constructor::convert(dynamic_t::array_t(), to); 37 | 38 | auto& array = boost::get>(to).get(); 39 | array.reserve(from.size()); 40 | 41 | for(const auto& e: from) { 42 | array.emplace_back(e); 43 | } 44 | } 45 | }; 46 | 47 | } // namespace cocaine 48 | -------------------------------------------------------------------------------- /include/cocaine/dynamic/constructors/shared_ptr.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2017+ Anton Matveenko 3 | Copyright (c) 2017+ Other contributors as noted in the AUTHORS file. 4 | 5 | This file is part of Cocaine. 6 | 7 | Cocaine is free software; you can redistribute it and/or modify 8 | it under the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | Cocaine is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #pragma once 22 | 23 | #include 24 | 25 | #include 26 | 27 | namespace cocaine { 28 | 29 | template 30 | struct dynamic_constructor> { 31 | static const bool enable = true; 32 | 33 | static inline 34 | void 35 | convert(const std::shared_ptr& from, dynamic_t::value_t& to) { 36 | if(from) { 37 | dynamic_constructor::type>::convert(*from, to); 38 | } else { 39 | to = dynamic_t::null_t(); 40 | } 41 | } 42 | }; 43 | 44 | } // namespace cocaine 45 | -------------------------------------------------------------------------------- /include/cocaine/dynamic/converters/endpoint.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | #include 8 | 9 | namespace cocaine { 10 | 11 | template<> 12 | struct dynamic_converter { 13 | typedef asio::ip::tcp::endpoint result_type; 14 | 15 | static const bool enable = true; 16 | 17 | static inline 18 | result_type 19 | convert(const dynamic_t& from) { 20 | if (!from.is_array() || from.as_array().size() != 2) { 21 | throw error_t("invalid dynamic value for endpoint deserialization {}", from); 22 | } 23 | auto ep_pair = from.as_array(); 24 | if (!ep_pair[0].is_string() || !ep_pair[1].is_uint()) { 25 | throw error_t("invalid dynamic value for endpoint deserialization", from); 26 | } 27 | std::string host = ep_pair[0].to(); 28 | unsigned short int port = ep_pair[1].to(); 29 | asio::ip::tcp::endpoint result(asio::ip::address::from_string(host), port); 30 | return result; 31 | } 32 | 33 | static inline 34 | bool 35 | convertible(const dynamic_t& from) { 36 | return from.is_array() && 37 | from.as_array().size() == 2 && 38 | from.as_array()[0].is_string() && 39 | (from.as_array()[1].is_uint() || from.as_array()[1].is_int()); 40 | } 41 | }; 42 | 43 | 44 | } // namespace cocaine 45 | -------------------------------------------------------------------------------- /include/cocaine/dynamic/object.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2013-2014 Andrey Goryachev 3 | Copyright (c) 2011-2014 Other contributors as noted in the AUTHORS file. 4 | 5 | This file is part of Cocaine. 6 | 7 | Cocaine is free software; you can redistribute it and/or modify 8 | it under the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | Cocaine is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #ifndef COCAINE_DYNAMIC_OBJECT_HPP 22 | #define COCAINE_DYNAMIC_OBJECT_HPP 23 | 24 | #include 25 | 26 | namespace cocaine { 27 | 28 | class dynamic_t::object_t : 29 | public std::map 30 | { 31 | typedef std::map base_type; 32 | 33 | public: 34 | object_t() = default; 35 | 36 | template 37 | object_t(InputIt first, InputIt last): 38 | base_type(first, last) 39 | { } 40 | 41 | object_t(const object_t& other): 42 | base_type(other) 43 | { } 44 | 45 | object_t(object_t&& other): 46 | base_type(std::move(other)) 47 | { } 48 | 49 | object_t(std::initializer_list list): 50 | base_type(list) 51 | { } 52 | 53 | object_t(const base_type& other): 54 | base_type(other) 55 | { } 56 | 57 | object_t(base_type&& other): 58 | base_type(std::move(other)) 59 | { } 60 | 61 | object_t& 62 | operator=(const object_t& other) { 63 | base_type::operator=(other); 64 | return *this; 65 | } 66 | 67 | object_t& 68 | operator=(object_t&& other) { 69 | base_type::operator=(std::move(other)); 70 | return *this; 71 | } 72 | 73 | cocaine::dynamic_t& 74 | at(const std::string& key); 75 | 76 | const cocaine::dynamic_t& 77 | at(const std::string& key) const; 78 | 79 | cocaine::dynamic_t& 80 | at(const std::string& key, cocaine::dynamic_t& def); 81 | 82 | const cocaine::dynamic_t& 83 | at(const std::string& key, const cocaine::dynamic_t& def) const; 84 | 85 | using base_type::operator[]; 86 | 87 | const cocaine::dynamic_t& 88 | operator[](const std::string& key) const; 89 | }; 90 | 91 | } // namespace cocaine 92 | 93 | #endif // COCAINE_DYNAMIC_OBJECT_HPP 94 | -------------------------------------------------------------------------------- /include/cocaine/engine.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2011-2014 Andrey Sibiryov 3 | Copyright (c) 2011-2014 Other contributors as noted in the AUTHORS file. 4 | 5 | This file is part of Cocaine. 6 | 7 | Cocaine is free software; you can redistribute it and/or modify 8 | it under the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | Cocaine is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #ifndef COCAINE_ENGINE_HPP 22 | #define COCAINE_ENGINE_HPP 23 | 24 | #include "cocaine/common.hpp" 25 | 26 | #include 27 | 28 | namespace cocaine { 29 | 30 | class session_t; 31 | 32 | template 33 | class session; 34 | 35 | class execution_unit_t { 36 | COCAINE_DECLARE_NONCOPYABLE(execution_unit_t) 37 | 38 | class gc_action_t; 39 | 40 | // Connections 41 | 42 | std::map> m_sessions; 43 | 44 | // I/O 45 | 46 | std::shared_ptr m_asio; 47 | std::unique_ptr m_chamber; 48 | 49 | // Initialized here because of the dependency on the io::chamber_t's thread ID. 50 | const std::unique_ptr m_log; 51 | metrics::registry_t& m_metrics; 52 | 53 | static const unsigned int kCollectionInterval = 60; 54 | 55 | // Collects detached sessions every kCollectionInterval seconds. Normally, session slots will be 56 | // reused because of system fd rotation, but for low loads this will help a bit. 57 | std::unique_ptr m_cron; 58 | 59 | context_t& context; 60 | 61 | public: 62 | explicit 63 | execution_unit_t(context_t& context); 64 | 65 | ~execution_unit_t(); 66 | 67 | template 68 | std::shared_ptr> 69 | attach(std::unique_ptr ptr, const io::dispatch_ptr_t& dispatch); 70 | 71 | double 72 | utilization() const; 73 | }; 74 | 75 | } // namespace cocaine 76 | 77 | #endif 78 | -------------------------------------------------------------------------------- /include/cocaine/executor/asio.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "cocaine/api/executor.hpp" 4 | 5 | #include 6 | 7 | #include 8 | #include 9 | 10 | namespace cocaine { 11 | namespace executor { 12 | 13 | // Runs asio::io_service in a spawned thread and posts all callbacks provided to spawn in it. 14 | class owning_asio_t: public api::executor_t { 15 | public: 16 | enum class stop_policy_t { 17 | graceful, 18 | force 19 | }; 20 | 21 | // Choose wheteher to wait wor async operations completion in dtor or not 22 | owning_asio_t(stop_policy_t stop_policy = stop_policy_t::graceful); 23 | 24 | ~owning_asio_t(); 25 | 26 | auto 27 | spawn(work_t work) -> void override; 28 | 29 | auto 30 | asio() -> asio::io_service&; 31 | 32 | private: 33 | asio::io_service io_loop; 34 | boost::optional work; 35 | boost::thread thread; 36 | stop_policy_t stop_policy; 37 | }; 38 | 39 | // Posts callbacks passed to spawn to externally provided io_service 40 | class borrowing_asio_t: public api::executor_t { 41 | public: 42 | borrowing_asio_t(asio::io_service& io_loop); 43 | 44 | auto 45 | spawn(work_t work) -> void override; 46 | 47 | private: 48 | asio::io_service& io_loop; 49 | }; 50 | 51 | } // namespace executor 52 | } // namespace cocaine 53 | -------------------------------------------------------------------------------- /include/cocaine/format.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2011-2014 Andrey Sibiryov 3 | Copyright (c) 2011-2014 Other contributors as noted in the AUTHORS file. 4 | 5 | This file is part of Cocaine. 6 | 7 | Cocaine is free software; you can redistribute it and/or modify 8 | it under the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | Cocaine is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #ifndef COCAINE_FORMAT_HPP 22 | #define COCAINE_FORMAT_HPP 23 | 24 | #include 25 | 26 | namespace cocaine { 27 | 28 | /// Specialize this trait if you want custom behavior while formatting arguments. 29 | template 30 | struct display_traits { 31 | static 32 | auto 33 | apply(const T& value) -> const T& { 34 | return value; 35 | } 36 | }; 37 | 38 | template 39 | inline 40 | std::string 41 | format(const std::string& format, const Args&... args) { 42 | try { 43 | return blackhole::fmt::format( 44 | format, 45 | display_traits::apply(args)... 46 | ); 47 | } catch(const blackhole::fmt::FormatError& e) { 48 | return std::string(""; 49 | } 50 | } 51 | 52 | } // namespace cocaine 53 | 54 | #endif 55 | -------------------------------------------------------------------------------- /include/cocaine/format/base.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | 6 | namespace cocaine { 7 | 8 | template 9 | struct display { 10 | static 11 | auto 12 | apply(std::ostream& stream, const T& value) -> std::ostream& { 13 | return stream << value; 14 | } 15 | }; 16 | 17 | /// Base class that can be used by display_traits to inherit from and provide lazy formatting 18 | template 19 | struct lazy_display { 20 | struct functor_t { 21 | auto 22 | operator()(std::ostream& stream) const -> std::ostream& { 23 | return display::apply(stream, value); 24 | } 25 | const T& value; 26 | }; 27 | 28 | static 29 | auto 30 | apply(const T& value) -> functor_t { 31 | return functor_t{value}; 32 | } 33 | }; 34 | 35 | template 36 | struct string_display { 37 | static 38 | auto 39 | apply(const T& value) -> std::string { 40 | std::ostringstream stream; 41 | display::apply(stream, value); 42 | return stream.str(); 43 | } 44 | }; 45 | 46 | } // namespace cocaine 47 | -------------------------------------------------------------------------------- /include/cocaine/format/deque.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #include "cocaine/format/base.hpp" 6 | #include "cocaine/format/linear_container.hpp" 7 | 8 | namespace cocaine { 9 | 10 | template 11 | struct display> : public linear_container_display> {}; 12 | 13 | template 14 | struct display_traits> : public lazy_display> {}; 15 | 16 | } // namespace cocaine 17 | -------------------------------------------------------------------------------- /include/cocaine/format/dynamic.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "cocaine/format/base.hpp" 4 | #include "cocaine/dynamic.hpp" 5 | 6 | namespace cocaine { 7 | 8 | template<> 9 | struct display { 10 | using value_type = dynamic_t; 11 | 12 | static 13 | auto 14 | apply(std::ostream& stream, const value_type& value) -> std::ostream& { 15 | return stream << boost::lexical_cast(value); 16 | } 17 | }; 18 | 19 | template<> 20 | struct display_traits : public lazy_display {}; 21 | 22 | } // namespace cocaine 23 | -------------------------------------------------------------------------------- /include/cocaine/format/endpoint.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "cocaine/format/base.hpp" 4 | 5 | #include 6 | 7 | namespace cocaine { 8 | 9 | template 10 | struct display> { 11 | using value_type = asio::ip::basic_endpoint; 12 | 13 | static 14 | auto 15 | apply(std::ostream& stream, const value_type& value) -> std::ostream& { 16 | return stream << value.address() << ":" << value.port(); 17 | } 18 | }; 19 | 20 | template 21 | struct display_traits> : public string_display> {}; 22 | 23 | } // namespace cocaine 24 | -------------------------------------------------------------------------------- /include/cocaine/format/error_code.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "cocaine/format/base.hpp" 4 | 5 | #include 6 | 7 | namespace cocaine { 8 | 9 | template<> 10 | struct display { 11 | using value_type = std::error_code; 12 | 13 | static 14 | auto 15 | apply(std::ostream& stream, const value_type& ec) -> std::ostream& { 16 | return stream << cocaine::format("[{}] {}", ec.value(), ec.message()); 17 | } 18 | }; 19 | 20 | template<> 21 | struct display_traits : public lazy_display {}; 22 | 23 | } // namespace cocaine 24 | -------------------------------------------------------------------------------- /include/cocaine/format/exception.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "cocaine/format/base.hpp" 4 | 5 | #include 6 | 7 | namespace cocaine { 8 | 9 | template<> 10 | struct display { 11 | using value_type = std::exception; 12 | 13 | static 14 | auto 15 | apply(std::ostream& stream, const value_type& value) -> std::ostream& { 16 | const std::system_error* ptr = dynamic_cast(&value); 17 | if(ptr) { 18 | return stream << cocaine::format("[{}] {}", ptr->code().value(), ptr->what()); 19 | } else { 20 | return stream << value.what(); 21 | } 22 | } 23 | }; 24 | 25 | template<> 26 | struct display_traits : public lazy_display {}; 27 | 28 | template<> 29 | struct display_traits : public lazy_display {}; 30 | 31 | } // namespace cocaine 32 | -------------------------------------------------------------------------------- /include/cocaine/format/graph.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "cocaine/format/base.hpp" 4 | #include "cocaine/format/map.hpp" 5 | #include "cocaine/format/optional.hpp" 6 | #include "cocaine/format/tuple.hpp" 7 | #include "cocaine/rpc/graph.hpp" 8 | 9 | namespace cocaine { 10 | 11 | template<> 12 | struct display : public display {}; 13 | 14 | template<> 15 | struct display_traits : public display_traits {}; 16 | 17 | } // namespace cocaine 18 | -------------------------------------------------------------------------------- /include/cocaine/format/header.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "cocaine/format/base.hpp" 4 | #include "cocaine/hpack/header.hpp" 5 | 6 | namespace cocaine { 7 | 8 | template<> 9 | struct display { 10 | using value_type = hpack::header_t; 11 | 12 | static 13 | auto 14 | apply(std::ostream& stream, const value_type& value) -> std::ostream& { 15 | return stream << value.name() << ": " << value.value(); 16 | } 17 | }; 18 | 19 | template<> 20 | struct display_traits : public string_display {}; 21 | 22 | } // namespace cocaine 23 | -------------------------------------------------------------------------------- /include/cocaine/format/kv_container.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "cocaine/format/base.hpp" 4 | #include "cocaine/utility.hpp" 5 | 6 | namespace cocaine { 7 | 8 | template 9 | struct kv_container_display { 10 | typedef C value_type; 11 | 12 | static 13 | auto 14 | apply(std::ostream& stream, const value_type& value) -> std::ostream& { 15 | auto written = 0; 16 | 17 | stream << "{"; 18 | for (auto& v : value) { 19 | if (written > 0) { 20 | stream << ", "; 21 | } 22 | 23 | using first_type = typename pristine(v))>::type; 24 | using second_type = typename pristine(v))>::type; 25 | display::apply(stream, std::get<0>(v)); 26 | stream << ": "; 27 | display::apply(stream, std::get<1>(v)); 28 | written += 1; 29 | } 30 | stream << "}"; 31 | 32 | return stream; 33 | } 34 | }; 35 | 36 | } // namespace cocaine 37 | -------------------------------------------------------------------------------- /include/cocaine/format/linear_container.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #include "cocaine/format/base.hpp" 6 | 7 | namespace cocaine { 8 | 9 | template 10 | struct linear_container_display { 11 | typedef C value_type; 12 | 13 | static 14 | auto 15 | apply(std::ostream& stream, const value_type& value) -> std::ostream& { 16 | auto written = 0; 17 | 18 | stream << "["; 19 | for (auto& v : value) { 20 | if (written > 0) { 21 | stream << ", "; 22 | } 23 | 24 | display::apply(stream, v); 25 | written += 1; 26 | } 27 | stream << "]"; 28 | 29 | return stream; 30 | } 31 | }; 32 | 33 | } // namespace cocaine 34 | -------------------------------------------------------------------------------- /include/cocaine/format/map.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | 6 | #include "cocaine/format/base.hpp" 7 | #include "cocaine/format/kv_container.hpp" 8 | 9 | namespace cocaine { 10 | 11 | template 12 | struct display> : public kv_container_display> {}; 13 | 14 | template 15 | struct display> : public kv_container_display> {}; 16 | 17 | template 18 | struct display> : public kv_container_display> {}; 19 | 20 | template 21 | struct display> : public kv_container_display> {}; 22 | 23 | template 24 | struct display_traits> : public lazy_display> {}; 25 | 26 | template 27 | struct display_traits> : public lazy_display> {}; 28 | 29 | template 30 | struct display_traits> : public lazy_display> {}; 31 | 32 | template 33 | struct display_traits> : public lazy_display> {}; 34 | 35 | } // namespace cocaine 36 | -------------------------------------------------------------------------------- /include/cocaine/format/optional.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "cocaine/format/base.hpp" 4 | #include "cocaine/utility.hpp" 5 | 6 | #include 7 | 8 | namespace cocaine { 9 | 10 | template 11 | struct display> { 12 | using value_type = boost::optional; 13 | using underlying_type = typename pristine::type; 14 | 15 | static 16 | auto 17 | apply(std::ostream& stream, const value_type& value) -> std::ostream& { 18 | if (value) { 19 | display::apply(stream, *value); 20 | } else { 21 | stream << "none"; 22 | } 23 | return stream; 24 | } 25 | }; 26 | 27 | template 28 | struct display_traits> : public lazy_display> {}; 29 | 30 | } // namespace cocaine 31 | -------------------------------------------------------------------------------- /include/cocaine/format/ptr.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "cocaine/format/base.hpp" 4 | 5 | #include 6 | 7 | namespace cocaine { 8 | 9 | template 10 | struct ptr_display { 11 | using value_type = T; 12 | using underlying_type = typename pristine())>::type; 13 | 14 | static 15 | auto 16 | apply(std::ostream& stream, const value_type& value) -> std::ostream& { 17 | if(value) { 18 | display::apply(stream, *value); 19 | } else { 20 | stream << "nullptr"; 21 | } 22 | return stream; 23 | } 24 | }; 25 | 26 | template 27 | struct display>: public ptr_display> {}; 28 | 29 | template 30 | struct display>: public ptr_display> {}; 31 | 32 | template 33 | struct display_traits>: public lazy_display> {}; 34 | 35 | template 36 | struct display_traits>: public lazy_display> {}; 37 | 38 | } // namespace cocaine 39 | -------------------------------------------------------------------------------- /include/cocaine/format/set.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #include "cocaine/format/base.hpp" 6 | #include "cocaine/format/linear_container.hpp" 7 | 8 | namespace cocaine { 9 | 10 | template 11 | struct display> : public linear_container_display> {}; 12 | 13 | template 14 | struct display_traits> : public lazy_display> {}; 15 | 16 | } // namespace cocaine 17 | -------------------------------------------------------------------------------- /include/cocaine/format/tuple.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #include "cocaine/format/base.hpp" 6 | #include "cocaine/tuple.hpp" 7 | 8 | namespace cocaine { 9 | 10 | template 11 | struct display> { 12 | using value_type = std::tuple; 13 | 14 | struct applicator_t { 15 | std::ostream& stream; 16 | 17 | template 18 | auto 19 | operator()(const T& value) -> void { 20 | display::type>::apply(stream, value); 21 | } 22 | 23 | template 24 | auto 25 | operator()(const T& value, const TupleArgs& ... args) -> void { 26 | display::type>::apply(stream, value); 27 | stream << ", "; 28 | operator()(args...); 29 | } 30 | }; 31 | 32 | static 33 | auto 34 | apply(std::ostream& stream, const value_type& value) -> std::ostream& { 35 | stream << "["; 36 | applicator_t applicator{stream}; 37 | tuple::invoke(value, applicator); 38 | stream << "]"; 39 | return stream; 40 | } 41 | }; 42 | 43 | template 44 | struct display_traits> : public lazy_display> {}; 45 | 46 | } // namespace cocaine 47 | -------------------------------------------------------------------------------- /include/cocaine/format/vector.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #include "cocaine/format/base.hpp" 6 | #include "cocaine/format/linear_container.hpp" 7 | 8 | namespace cocaine { 9 | 10 | template 11 | struct display> : public linear_container_display> {}; 12 | 13 | template 14 | struct display_traits> : public lazy_display> {}; 15 | 16 | } // namespace cocaine 17 | -------------------------------------------------------------------------------- /include/cocaine/hpack/mpl.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2011-2015 Anton Matveenko 3 | Copyright (c) 2011-2015 Other contributors as noted in the AUTHORS file. 4 | 5 | This file is part of Cocaine. 6 | 7 | Cocaine is free software; you can redistribute it and/or modify 8 | it under the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | Cocaine is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | 22 | #pragma once 23 | 24 | #include 25 | 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | 32 | #include 33 | #include 34 | #include 35 | 36 | // Some woodoo magic of boost mpl. 37 | // This allows to generate mpl vector larger than 50 38 | // See http://comments.gmane.org/gmane.comp.lib.boost.user/6986 39 | 40 | namespace boost { namespace mpl { 41 | 42 | #define BOOST_PP_ITERATION_PARAMS_1 \ 43 | (3,(51, 100, "boost/mpl/vector/aux_/numbered.hpp")) 44 | #include BOOST_PP_ITERATE() 45 | 46 | }} // namespace boost::mpl 47 | -------------------------------------------------------------------------------- /include/cocaine/idl/control.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | // primitive protocol is always required for responses on control events 6 | #include "cocaine/idl/primitive.hpp" 7 | #include "cocaine/rpc/tags.hpp" 8 | 9 | namespace cocaine { namespace io { 10 | 11 | template 12 | struct messages; 13 | 14 | template 15 | struct protocol; 16 | 17 | struct control_tag; 18 | 19 | struct control { 20 | 21 | /// Allows for immediate termination of a stream. 22 | struct revoke { 23 | typedef control_tag tag; 24 | 25 | static const char* alias() { 26 | return "revoke"; 27 | } 28 | 29 | typedef boost::mpl::list< 30 | /// Channel to be revoked. 31 | std::uint64_t, 32 | /// Error reason for channel revocation. 33 | std::error_code 34 | >::type argument_type; 35 | }; 36 | 37 | /// The settings event conveys configuration parameters that affect how endpoints communicate, 38 | /// such as preferences and constraints on peer behavior. 39 | /// 40 | /// This is also used to acknowledge the receipt of those parameters. 41 | struct settings { 42 | typedef control_tag tag; 43 | 44 | static const char* alias() { 45 | return "settings"; 46 | } 47 | }; 48 | 49 | /// The ping event is a mechanism for measuring a minimal round-trip time from the sender, as well 50 | /// as determining whether an idle connection is still functional. 51 | struct ping { 52 | typedef control_tag tag; 53 | 54 | static const char* alias() { 55 | return "ping"; 56 | } 57 | }; 58 | 59 | /// The goaway event is used to initiate shutdown of a connection or to signal serious error 60 | /// conditions. 61 | /// 62 | /// It allows an endpoint to gracefully stop accepting new streams while still 63 | /// finishing processing of previously established streams. This enables administrative actions, 64 | /// like server maintenance. 65 | struct goaway { 66 | typedef control_tag tag; 67 | 68 | static const char* alias() { 69 | return "goaway"; 70 | } 71 | 72 | typedef boost::mpl::list< 73 | /// Error reason. 74 | std::error_code 75 | >::type argument_type; 76 | }; 77 | 78 | }; // struct control 79 | 80 | template<> 81 | struct protocol { 82 | typedef boost::mpl::int_< 83 | 1 84 | >::type version; 85 | 86 | typedef boost::mpl::list< 87 | control::revoke, 88 | control::settings, 89 | control::ping, 90 | control::goaway 91 | // TODO: To be added more, incomplete. 92 | >::type messages; 93 | 94 | typedef control scope; 95 | }; 96 | 97 | }} // namespace cocaine::io 98 | -------------------------------------------------------------------------------- /include/cocaine/idl/primitive.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2011-2014 Andrey Sibiryov 3 | Copyright (c) 2011-2014 Other contributors as noted in the AUTHORS file. 4 | 5 | This file is part of Cocaine. 6 | 7 | Cocaine is free software; you can redistribute it and/or modify 8 | it under the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | Cocaine is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #ifndef COCAINE_PRIMITIVE_SERVICE_INTERFACE_HPP 22 | #define COCAINE_PRIMITIVE_SERVICE_INTERFACE_HPP 23 | 24 | #include "cocaine/rpc/protocol.hpp" 25 | #include "cocaine/rpc/tags.hpp" 26 | 27 | #include 28 | 29 | #include 30 | 31 | namespace cocaine { namespace io { 32 | 33 | // Streaming service interface 34 | 35 | template 36 | struct primitive { 37 | 38 | static_assert(boost::mpl::is_sequence::value, 39 | "primitive protocol template argument must be a type sequence"); 40 | 41 | struct value { 42 | typedef primitive_tag tag; 43 | 44 | static const char* alias() { 45 | return "value"; 46 | } 47 | 48 | typedef T argument_type; 49 | typedef void upstream_type; 50 | }; 51 | 52 | struct error { 53 | typedef primitive_tag tag; 54 | 55 | static const char* alias() { 56 | return "error"; 57 | } 58 | 59 | typedef boost::mpl::list< 60 | /* Serialized error category and error code. */ 61 | std::error_code, 62 | /* Specially crafted personal error message. */ 63 | optional 64 | >::type argument_type; 65 | 66 | // Terminal message. 67 | typedef void upstream_type; 68 | }; 69 | 70 | }; // struct primitive 71 | 72 | template 73 | struct protocol> { 74 | typedef T sequence_type; 75 | 76 | typedef boost::mpl::int_< 77 | 1 78 | >::type version; 79 | 80 | typedef typename boost::mpl::list< 81 | typename primitive::value, 82 | typename primitive::error 83 | >::type messages; 84 | 85 | typedef primitive scope; 86 | typedef primitive_tag transition_type; 87 | }; 88 | 89 | }} // namespace cocaine::io 90 | 91 | #endif 92 | -------------------------------------------------------------------------------- /include/cocaine/memory.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2011-2014 Andrey Sibiryov 3 | Copyright (c) 2011-2014 Other contributors as noted in the AUTHORS file. 4 | 5 | This file is part of Cocaine. 6 | 7 | Cocaine is free software; you can redistribute it and/or modify 8 | it under the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | Cocaine is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #ifndef COCAINE_MEMORY_HPP 22 | #define COCAINE_MEMORY_HPP 23 | 24 | #include 25 | 26 | #ifndef __cpp_lib_make_unique 27 | namespace std { 28 | 29 | template 30 | unique_ptr 31 | make_unique(Args&&... args) { 32 | return std::unique_ptr(new T(std::forward(args)...)); 33 | } 34 | 35 | } // namespace std 36 | #endif 37 | 38 | namespace cocaine { namespace io { 39 | 40 | template 41 | struct uninitialized: public std::allocator { 42 | template struct rebind { 43 | typedef uninitialized other; 44 | }; 45 | 46 | void construct(T*) { } 47 | void destroy (T*) { } 48 | }; 49 | 50 | }} // namespace cocaine::io 51 | 52 | #endif 53 | -------------------------------------------------------------------------------- /include/cocaine/middleware/auth.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | #include 8 | 9 | #include "cocaine/api/authentication.hpp" 10 | #include "cocaine/auth/uid.hpp" 11 | #include "cocaine/context.hpp" 12 | #include "cocaine/hpack/header.hpp" 13 | 14 | namespace cocaine { 15 | namespace middleware { 16 | 17 | class auth_t { 18 | std::shared_ptr auth; 19 | 20 | public: 21 | explicit 22 | auth_t(context_t& context, const std::string& service) : 23 | auth(api::authentication(context, "core", service)) 24 | {} 25 | 26 | template 27 | auto 28 | operator()(F fn, Event, const hpack::headers_t& headers, Args&&... args) -> 29 | decltype(fn(headers, std::forward(args)...)) 30 | { 31 | const auto identity = auth->identify(headers); 32 | return fn(headers, std::forward(args)..., boost::get(identity)); 33 | } 34 | }; 35 | 36 | } // namespace middleware 37 | } // namespace cocaine 38 | -------------------------------------------------------------------------------- /include/cocaine/middleware/headers.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "cocaine/hpack/header.hpp" 4 | 5 | namespace cocaine { 6 | namespace middleware { 7 | 8 | /// Middleware that eats HPACK headers argument from the execution chain. 9 | class drop_headers_t { 10 | public: 11 | template 12 | auto 13 | operator()(F fn, Event, const hpack::headers_t&, Args&&... args) -> 14 | decltype(fn(std::forward(args)...)) 15 | { 16 | return fn(std::forward(args)...); 17 | } 18 | }; 19 | 20 | } // namespace middleware 21 | } // namespace cocaine 22 | -------------------------------------------------------------------------------- /include/cocaine/platform.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2011-2014 Andrey Sibiryov 3 | Copyright (c) 2011-2014 Other contributors as noted in the AUTHORS file. 4 | 5 | This file is part of Cocaine. 6 | 7 | Cocaine is free software; you can redistribute it and/or modify 8 | it under the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | Cocaine is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #ifndef COCAINE_PLATFORM_HPP 22 | #define COCAINE_PLATFORM_HPP 23 | 24 | #if defined(__GNUC__) 25 | #if (__GNUC__ == 4 && __GNUC_MINOR__ >= 4) || __GNUC__ >= 5 26 | #define HAVE_GCC44 27 | #endif 28 | 29 | #if (__GNUC__ == 4 && __GNUC_MINOR__ >= 6) || __GNUC__ >= 5 30 | #define HAVE_GCC46 31 | #endif 32 | 33 | #if (__GNUC__ == 4 && __GNUC_MINOR__ >= 8) || __GNUC__ >= 5 34 | #define HAVE_GCC48 35 | #endif 36 | #endif 37 | 38 | #if defined(__clang__) || defined(HAVE_GCC47) 39 | #define COCAINE_HAS_FEATURE_UNDERLYING_TYPE 40 | #endif 41 | 42 | #if defined(__clang__) || defined(HAVE_GCC48) 43 | #define COCAINE_HAS_FEATURE_STEADY_CLOCK 44 | #endif 45 | 46 | #if defined(__clang__) 47 | #define COCAINE_HAS_FEATURE_PAIR_TO_TUPLE_CONVERSION 48 | #endif 49 | 50 | #endif 51 | -------------------------------------------------------------------------------- /include/cocaine/repository/authentication.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "cocaine/api/authentication.hpp" 4 | #include "cocaine/common.hpp" 5 | #include "cocaine/repository.hpp" 6 | 7 | namespace cocaine { 8 | namespace api { 9 | 10 | template<> 11 | struct category_traits { 12 | typedef std::shared_ptr ptr_type; 13 | 14 | struct factory_type : public basic_factory { 15 | virtual 16 | auto 17 | get(context_t& context, const std::string& name, const std::string& service, const dynamic_t& args) -> ptr_type = 0; 18 | }; 19 | 20 | template 21 | struct default_factory : public factory_type { 22 | auto 23 | get(context_t& context, const std::string&, const std::string& service, const dynamic_t& args) -> ptr_type override { 24 | return std::make_shared(context, service, args); 25 | } 26 | }; 27 | }; 28 | 29 | } // namespace api 30 | } // namespace cocaine 31 | -------------------------------------------------------------------------------- /include/cocaine/repository/authorization.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "cocaine/api/authorization/event.hpp" 4 | #include "cocaine/api/authorization/storage.hpp" 5 | #include "cocaine/api/authorization/unicorn.hpp" 6 | #include "cocaine/common.hpp" 7 | #include "cocaine/repository.hpp" 8 | 9 | namespace cocaine { 10 | namespace api { 11 | 12 | template 13 | struct make_category { 14 | typedef std::shared_ptr ptr_type; 15 | 16 | struct factory_type : public basic_factory { 17 | virtual 18 | auto 19 | get(context_t& context, const std::string& name, const std::string& service, const dynamic_t& args) -> ptr_type = 0; 20 | }; 21 | 22 | template 23 | struct default_factory : public factory_type { 24 | auto 25 | get(context_t& context, const std::string&, const std::string& service, const dynamic_t& args) -> ptr_type override { 26 | return std::make_shared(context, service, args); 27 | } 28 | }; 29 | }; 30 | 31 | template<> 32 | struct category_traits : public make_category {}; 33 | 34 | template<> 35 | struct category_traits : public make_category {}; 36 | 37 | template<> 38 | struct category_traits : public make_category {}; 39 | 40 | } // namespace api 41 | } // namespace cocaine 42 | -------------------------------------------------------------------------------- /include/cocaine/repository/cluster.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2011-2014 Andrey Sibiryov 3 | Copyright (c) 2011-2014 Other contributors as noted in the AUTHORS file. 4 | 5 | This file is part of Cocaine. 6 | 7 | Cocaine is free software; you can redistribute it and/or modify 8 | it under the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | Cocaine is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #ifndef COCAINE_REPOSITORY_CLUSTER_HPP 22 | #define COCAINE_REPOSITORY_CLUSTER_HPP 23 | 24 | #include "cocaine/api/cluster.hpp" 25 | #include "cocaine/common.hpp" 26 | #include "cocaine/repository.hpp" 27 | 28 | namespace cocaine { namespace api { 29 | 30 | template<> 31 | struct category_traits { 32 | typedef cluster_ptr ptr_type; 33 | typedef cluster_t::interface interface; 34 | 35 | struct factory_type: public basic_factory { 36 | virtual 37 | ptr_type 38 | get(context_t& context, interface& locator, cluster_t::mode_t mode, const std::string& name, const dynamic_t& args) = 0; 39 | }; 40 | 41 | template 42 | struct default_factory: public factory_type { 43 | virtual 44 | ptr_type 45 | get(context_t& context, interface& locator, cluster_t::mode_t mode, const std::string& name, const dynamic_t& args) { 46 | return ptr_type(new T(context, locator, mode, name, args)); 47 | } 48 | }; 49 | }; 50 | 51 | }} // namespace cocaine::api 52 | 53 | #endif 54 | -------------------------------------------------------------------------------- /include/cocaine/repository/gateway.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2011-2014 Andrey Sibiryov 3 | Copyright (c) 2011-2014 Other contributors as noted in the AUTHORS file. 4 | 5 | This file is part of Cocaine. 6 | 7 | Cocaine is free software; you can redistribute it and/or modify 8 | it under the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | Cocaine is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #ifndef COCAINE_REPOSITORY_GATEWAY_HPP 22 | #define COCAINE_REPOSITORY_GATEWAY_HPP 23 | 24 | #include "cocaine/api/gateway.hpp" 25 | #include "cocaine/common.hpp" 26 | #include "cocaine/repository.hpp" 27 | 28 | namespace cocaine { namespace api { 29 | 30 | template<> 31 | struct category_traits { 32 | typedef gateway_ptr ptr_type; 33 | 34 | struct factory_type: public basic_factory { 35 | virtual 36 | ptr_type 37 | get(context_t& context, const std::string& uuid, const std::string& name, const dynamic_t& args, 38 | const dynamic_t::object_t& locator_extra) = 0; 39 | }; 40 | 41 | template 42 | struct default_factory: public factory_type { 43 | virtual 44 | ptr_type 45 | get(context_t& context, const std::string& uuid, const std::string& name, const dynamic_t& args, 46 | const dynamic_t::object_t& locator_extra) 47 | { 48 | return ptr_type(new T(context, uuid, name, args, locator_extra)); 49 | } 50 | }; 51 | }; 52 | 53 | }} // namespace cocaine::api 54 | 55 | #endif 56 | -------------------------------------------------------------------------------- /include/cocaine/repository/service.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2011-2014 Andrey Sibiryov 3 | Copyright (c) 2011-2014 Other contributors as noted in the AUTHORS file. 4 | 5 | This file is part of Cocaine. 6 | 7 | Cocaine is free software; you can redistribute it and/or modify 8 | it under the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | Cocaine is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #ifndef COCAINE_REPOSITORY_SERVICE_HPP 22 | #define COCAINE_REPOSITORY_SERVICE_HPP 23 | 24 | #include "cocaine/api/service.hpp" 25 | #include "cocaine/common.hpp" 26 | #include "cocaine/repository.hpp" 27 | 28 | namespace cocaine { namespace api { 29 | 30 | template<> 31 | struct category_traits { 32 | typedef service_ptr ptr_type; 33 | 34 | struct factory_type: public basic_factory { 35 | virtual 36 | ptr_type 37 | get(context_t& context, asio::io_service& asio, const std::string& name, const dynamic_t& args) = 0; 38 | }; 39 | 40 | template 41 | struct default_factory: public factory_type { 42 | virtual 43 | ptr_type 44 | get(context_t& context, asio::io_service& asio, const std::string& name, const dynamic_t& args) { 45 | return ptr_type(new T(context, asio, name, args)); 46 | } 47 | }; 48 | }; 49 | 50 | }} // namespace cocaine::api 51 | 52 | #endif 53 | -------------------------------------------------------------------------------- /include/cocaine/repository/storage.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2011-2014 Andrey Sibiryov 3 | Copyright (c) 2011-2014 Other contributors as noted in the AUTHORS file. 4 | 5 | This file is part of Cocaine. 6 | 7 | Cocaine is free software; you can redistribute it and/or modify 8 | it under the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | Cocaine is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #ifndef COCAINE_REPOSITORY_STORAGE_HPP 22 | #define COCAINE_REPOSITORY_STORAGE_HPP 23 | 24 | #include "cocaine/api/storage.hpp" 25 | #include "cocaine/common.hpp" 26 | #include "cocaine/dynamic/dynamic.hpp" 27 | #include "cocaine/locked_ptr.hpp" 28 | #include "cocaine/repository.hpp" 29 | 30 | #include 31 | #include 32 | #include 33 | 34 | namespace cocaine { namespace api { 35 | 36 | template<> 37 | struct category_traits { 38 | typedef storage_ptr ptr_type; 39 | 40 | struct factory_type: public basic_factory { 41 | virtual 42 | ptr_type 43 | get(context_t& context, const std::string& name, const dynamic_t& args) = 0; 44 | }; 45 | 46 | template 47 | struct default_factory: public factory_type { 48 | virtual 49 | ptr_type 50 | get(context_t& context, const std::string& name, const dynamic_t& args) { 51 | ptr_type instance; 52 | 53 | instances.apply([&](std::map>& instances) { 54 | if((instance = instances[name].lock()) == nullptr) { 55 | instance = std::make_shared(context, name, args); 56 | instances[name] = instance; 57 | } 58 | }); 59 | 60 | return instance; 61 | } 62 | 63 | private: 64 | synchronized>> instances; 65 | }; 66 | }; 67 | 68 | 69 | }} // namespace cocaine::api 70 | 71 | #endif 72 | -------------------------------------------------------------------------------- /include/cocaine/repository/unicorn.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 2015+ Copyright (c) Anton Matveenko 3 | * All rights reserved. 4 | * 5 | * This program is free software; you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation; either version 2 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | */ 15 | 16 | #ifndef COCAINE_REPOSITORY_UNICORN_HPP 17 | #define COCAINE_REPOSITORY_UNICORN_HPP 18 | 19 | #include "cocaine/api/unicorn.hpp" 20 | #include "cocaine/forwards.hpp" 21 | #include "cocaine/locked_ptr.hpp" 22 | #include "cocaine/repository.hpp" 23 | 24 | #include 25 | 26 | namespace cocaine { namespace api { 27 | 28 | template<> 29 | struct category_traits { 30 | typedef unicorn_ptr ptr_type; 31 | 32 | struct factory_type: public basic_factory { 33 | virtual 34 | ptr_type 35 | get(context_t& context, const std::string& name, const dynamic_t& args) = 0; 36 | }; 37 | 38 | template 39 | struct default_factory: public factory_type { 40 | virtual 41 | ptr_type 42 | get(context_t& context, const std::string& name, const dynamic_t& args) { 43 | ptr_type instance; 44 | 45 | instances.apply([&](std::map>& instances_) { 46 | if((instance = instances_[name].lock()) == nullptr) { 47 | instance = std::make_shared(context, name, args); 48 | instances_[name] = instance; 49 | } 50 | }); 51 | 52 | return instance; 53 | } 54 | 55 | private: 56 | synchronized>> instances; 57 | }; 58 | }; 59 | 60 | }} 61 | 62 | #endif // COCAINE_UNICORN_API_HPP 63 | -------------------------------------------------------------------------------- /include/cocaine/rpc/asio/decoder.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2011-2014 Andrey Sibiryov 3 | Copyright (c) 2011-2014 Other contributors as noted in the AUTHORS file. 4 | 5 | This file is part of Cocaine. 6 | 7 | Cocaine is free software; you can redistribute it and/or modify 8 | it under the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | Cocaine is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #ifndef COCAINE_IO_DECODER_HPP 22 | #define COCAINE_IO_DECODER_HPP 23 | 24 | #include "cocaine/common.hpp" 25 | #include "cocaine/hpack/header.hpp" 26 | 27 | #include 28 | 29 | #include 30 | 31 | namespace cocaine { namespace io { 32 | 33 | struct decoder_t; 34 | 35 | namespace aux { 36 | 37 | struct decoded_message_t { 38 | friend struct io::decoder_t; 39 | 40 | auto 41 | span() const -> uint64_t; 42 | 43 | auto 44 | type() const -> uint64_t; 45 | 46 | auto 47 | args() const -> const msgpack::object&; 48 | 49 | auto 50 | headers() const -> const hpack::headers_t&; 51 | 52 | void 53 | clear(); 54 | 55 | private: 56 | 57 | // These objects keep references to message buffer in the Decoder. 58 | msgpack::object object; 59 | hpack::headers_t metadata; 60 | }; 61 | 62 | } // namespace aux 63 | 64 | struct decoder_t { 65 | COCAINE_DECLARE_NONCOPYABLE(decoder_t) 66 | 67 | decoder_t() = default; 68 | ~decoder_t() = default; 69 | 70 | typedef aux::decoded_message_t message_type; 71 | 72 | size_t 73 | decode(const char* data, size_t size, message_type& message, std::error_code& ec); 74 | 75 | private: 76 | msgpack::zone zone; 77 | 78 | // HPACK HTTP/2.0 tables. 79 | hpack::header_table_t hpack_context; 80 | }; 81 | 82 | }} // namespace cocaine::io 83 | 84 | #endif 85 | -------------------------------------------------------------------------------- /include/cocaine/rpc/frozen.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2011-2015 Andrey Sibiryov 3 | Copyright (c) 2011-2015 Other contributors as noted in the AUTHORS file. 4 | 5 | This file is part of Cocaine. 6 | 7 | Cocaine is free software; you can redistribute it and/or modify 8 | it under the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | Cocaine is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #ifndef COCAINE_IO_FROZEN_EVENTS_HPP 22 | #define COCAINE_IO_FROZEN_EVENTS_HPP 23 | 24 | #include "cocaine/rpc/slot.hpp" 25 | 26 | #include 27 | 28 | namespace cocaine { namespace io { 29 | 30 | namespace mpl = boost::mpl; 31 | 32 | // Frozen events 33 | 34 | template 35 | struct frozen { 36 | typedef Event event_type; 37 | typedef typename basic_slot::tuple_type tuple_type; 38 | 39 | frozen() = default; 40 | 41 | template 42 | frozen(event_type, Args&&... args): 43 | tuple(std::forward(args)...) 44 | { } 45 | 46 | // NOTE: If the message cannot be sent right away, then the message arguments are placed into a 47 | // temporary storage until the upstream is attached. 48 | tuple_type tuple; 49 | }; 50 | 51 | template 52 | frozen 53 | make_frozen(Args&&... args) { 54 | return frozen(Event(), std::forward(args)...); 55 | } 56 | 57 | template 58 | struct make_frozen_over { 59 | typedef typename mpl::transform< 60 | typename messages::full::type, 61 | typename mpl::lambda> 62 | >::type frozen_types; 63 | 64 | typedef typename boost::make_variant_over::type type; 65 | }; 66 | 67 | }} // namespace cocaine::io 68 | 69 | #endif 70 | -------------------------------------------------------------------------------- /include/cocaine/rpc/graph.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2011-2014 Andrey Sibiryov 3 | Copyright (c) 2011-2014 Other contributors as noted in the AUTHORS file. 4 | 5 | This file is part of Cocaine. 6 | 7 | Cocaine is free software; you can redistribute it and/or modify 8 | it under the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | Cocaine is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #ifndef COCAINE_IO_DISPATCH_GRAPH_HPP 22 | #define COCAINE_IO_DISPATCH_GRAPH_HPP 23 | 24 | #include 25 | #include 26 | #include 27 | 28 | #include 29 | 30 | namespace cocaine { namespace io { 31 | 32 | struct graph_node_t; 33 | 34 | namespace aux { 35 | 36 | // Protocol transitions are described by an optional. Transition could be a new graph 37 | // point, an empty graph point (terminal message) or none, which means recurrent transition. 38 | 39 | typedef std::map< 40 | int, 41 | std::tuple> 42 | > recursion_base_t; 43 | 44 | } // namespace aux 45 | 46 | struct graph_node_t: public aux::recursion_base_t { 47 | typedef aux::recursion_base_t base_type; 48 | }; 49 | 50 | typedef std::map< 51 | int, 52 | std::tuple, boost::optional> 53 | > graph_root_t; 54 | 55 | }} // namespace cocaine::io 56 | 57 | #endif 58 | -------------------------------------------------------------------------------- /include/cocaine/rpc/result_of.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2011-2014 Andrey Sibiryov 3 | Copyright (c) 2011-2014 Other contributors as noted in the AUTHORS file. 4 | 5 | This file is part of Cocaine. 6 | 7 | Cocaine is free software; you can redistribute it and/or modify 8 | it under the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | Cocaine is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #ifndef COCAINE_IO_RESULT_OF_HPP 22 | #define COCAINE_IO_RESULT_OF_HPP 23 | 24 | #include "cocaine/rpc/protocol.hpp" 25 | 26 | #include "cocaine/tuple.hpp" 27 | 28 | #include 29 | #include 30 | 31 | namespace cocaine { namespace io { 32 | 33 | namespace mpl = boost::mpl; 34 | 35 | struct mute_slot_tag; 36 | 37 | namespace aux { 38 | 39 | template 40 | struct result_of_impl; 41 | 42 | template 43 | struct result_of_impl> { 44 | template::value> 45 | struct fold_type_list { 46 | typedef typename tuple::fold::type type; 47 | }; 48 | 49 | template 50 | struct fold_type_list { 51 | typedef typename mpl::front::type type; 52 | }; 53 | 54 | // In case there's only one type in the typelist, leave it as it is. Otherwise form a tuple out 55 | // of all the types in the typelist. 56 | typedef typename fold_type_list::type type; 57 | }; 58 | 59 | template<> 60 | struct result_of_impl>> { 61 | // Special case for void streams, i.e. streams which can only return end-of-stream or error. 62 | typedef void type; 63 | }; 64 | 65 | template 66 | struct result_of_impl>: 67 | public result_of_impl> 68 | { }; 69 | 70 | template<> 71 | struct result_of_impl { 72 | // No messages will follow an invocation of this slot. 73 | typedef mute_slot_tag type; 74 | }; 75 | 76 | }} // namespace io::aux 77 | 78 | template 79 | struct result_of::type> { 80 | typedef typename io::aux::result_of_impl< 81 | typename io::event_traits::upstream_type 82 | >::type type; 83 | }; 84 | 85 | } // namespace cocaine 86 | 87 | #endif 88 | -------------------------------------------------------------------------------- /include/cocaine/rpc/sanitize.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2011-2015 Andrey Sibiryov 3 | Copyright (c) 2011-2015 Other contributors as noted in the AUTHORS file. 4 | 5 | This file is part of Cocaine. 6 | 7 | Cocaine is free software; you can redistribute it and/or modify 8 | it under the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | Cocaine is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #ifndef COCAINE_IO_SANITIZE_HPP 22 | #define COCAINE_IO_SANITIZE_HPP 23 | 24 | #include "cocaine/rpc/tags.hpp" 25 | 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | 33 | namespace cocaine { namespace io { 34 | 35 | namespace mpl = boost::mpl; 36 | 37 | template 38 | struct sanitize { 39 | typedef typename mpl::stable_partition< 40 | Sequence, 41 | typename mpl::lambda< 42 | details::is_required 43 | >::type, 44 | mpl::back_inserter>, 45 | mpl::back_inserter> 46 | >::type partitions; 47 | 48 | static const bool value = mpl::equal< 49 | typename mpl::joint_view::type, 50 | Sequence 51 | >::value; 52 | }; 53 | 54 | }} // namespace cocaine::io 55 | 56 | #endif 57 | -------------------------------------------------------------------------------- /include/cocaine/rpc/slot.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2011-2014 Andrey Sibiryov 3 | Copyright (c) 2011-2014 Other contributors as noted in the AUTHORS file. 4 | 5 | This file is part of Cocaine. 6 | 7 | Cocaine is free software; you can redistribute it and/or modify 8 | it under the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | Cocaine is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #ifndef COCAINE_IO_SLOT_HPP 22 | #define COCAINE_IO_SLOT_HPP 23 | 24 | #include "cocaine/rpc/protocol.hpp" 25 | 26 | #include "cocaine/tuple.hpp" 27 | 28 | #include 29 | #include 30 | #include 31 | 32 | namespace cocaine { namespace io { 33 | 34 | namespace mpl = boost::mpl; 35 | 36 | template 37 | class basic_slot { 38 | typedef Event event_type; 39 | typedef event_traits traits_type; 40 | 41 | public: 42 | // Expected dispatch, parameter and upstream types. 43 | typedef std::vector meta_type; 44 | typedef typename traits_type::tuple_type tuple_type; 45 | typedef typename traits_type::sequence_type sequence_type; 46 | typedef dispatch dispatch_type; 47 | typedef upstream upstream_type; 48 | typedef boost::optional> result_type; 49 | 50 | virtual 51 | ~basic_slot() = default; 52 | 53 | virtual 54 | boost::optional> 55 | operator()(const meta_type& meta, tuple_type&& args, upstream_type&& upstream) = 0; 56 | }; 57 | 58 | template 59 | struct is_recursed: 60 | public std::is_same::dispatch_type, typename Event::tag> 61 | { }; 62 | 63 | template 64 | struct is_terminal: 65 | public std::is_same::dispatch_type, void> 66 | { }; 67 | 68 | }} // namespace cocaine::io 69 | 70 | #endif 71 | -------------------------------------------------------------------------------- /include/cocaine/rpc/slot/generic.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2011-2014 Andrey Sibiryov 3 | Copyright (c) 2011-2014 Other contributors as noted in the AUTHORS file. 4 | 5 | This file is part of Cocaine. 6 | 7 | Cocaine is free software; you can redistribute it and/or modify 8 | it under the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | Cocaine is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #ifndef COCAINE_IO_GENERIC_SLOT_HPP 22 | #define COCAINE_IO_GENERIC_SLOT_HPP 23 | 24 | #include "cocaine/rpc/slot.hpp" 25 | 26 | #include "cocaine/tuple.hpp" 27 | 28 | #include 29 | 30 | namespace cocaine { namespace io { 31 | 32 | template 33 | struct generic_slot: 34 | public basic_slot 35 | { 36 | typedef typename basic_slot::dispatch_type dispatch_type; 37 | typedef typename basic_slot::meta_type meta_type; 38 | typedef typename basic_slot::tuple_type tuple_type; 39 | typedef typename basic_slot::upstream_type upstream_type; 40 | typedef typename basic_slot::result_type result_type; 41 | typedef std::function function_type; 42 | 43 | explicit 44 | generic_slot(function_type callable_): 45 | callable(std::move(callable_)) 46 | {} 47 | 48 | 49 | result_type 50 | operator()(const meta_type& meta, tuple_type&& args, upstream_type&& upstream) override { 51 | return callable(meta, std::forward(args), std::forward(upstream)); 52 | } 53 | 54 | private: 55 | function_type callable; 56 | }; 57 | 58 | }} // namespace cocaine::io 59 | 60 | #endif 61 | -------------------------------------------------------------------------------- /include/cocaine/rpc/tags.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2011-2014 Andrey Sibiryov 3 | Copyright (c) 2011-2014 Other contributors as noted in the AUTHORS file. 4 | 5 | This file is part of Cocaine. 6 | 7 | Cocaine is free software; you can redistribute it and/or modify 8 | it under the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | Cocaine is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #ifndef COCAINE_IO_TAGS_HPP 22 | #define COCAINE_IO_TAGS_HPP 23 | 24 | #include "cocaine/utility.hpp" 25 | 26 | #include 27 | 28 | namespace cocaine { namespace io { 29 | 30 | // Argument tags 31 | 32 | template 33 | struct optional; 34 | 35 | template 36 | struct optional_with_default; 37 | 38 | // Forward common protocol tags 39 | 40 | template 41 | struct primitive_tag; 42 | 43 | template 44 | struct option_of { 45 | typedef primitive_tag::type> tag; 46 | }; 47 | 48 | template 49 | struct streaming_tag; 50 | 51 | template 52 | struct stream_of { 53 | typedef streaming_tag::type> tag; 54 | }; 55 | 56 | namespace details { 57 | 58 | template 59 | struct is_required: 60 | public std::true_type 61 | { }; 62 | 63 | template 64 | struct is_required>: 65 | public std::false_type 66 | { }; 67 | 68 | template 69 | struct is_required>: 70 | public std::false_type 71 | { }; 72 | 73 | template 74 | struct unwrap_type { 75 | typedef T type; 76 | }; 77 | 78 | template 79 | struct unwrap_type> { 80 | typedef T type; 81 | }; 82 | 83 | template 84 | struct unwrap_type> { 85 | typedef T type; 86 | }; 87 | 88 | // Protocol compatibility 89 | 90 | template 91 | struct is_compatible: 92 | public std::false_type 93 | { }; 94 | 95 | template 96 | struct is_compatible, primitive_tag>: 97 | public boost::mpl::equal::type 98 | { }; 99 | 100 | template 101 | struct is_compatible, streaming_tag>: 102 | public boost::mpl::equal::type 103 | { }; 104 | 105 | }}} // namespace cocaine::io::details 106 | 107 | #endif 108 | -------------------------------------------------------------------------------- /include/cocaine/trace/logger.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2015-2016 Anton Matveenko 3 | Copyright (c) 2015-2016 Other contributors as noted in the AUTHORS file. 4 | 5 | This file is part of Cocaine. 6 | 7 | Cocaine is free software; you can redistribute it and/or modify 8 | it under the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | Cocaine is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser Gene ral Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #ifndef COCAINE_TRACE_LOGGER 22 | #define COCAINE_TRACE_LOGGER 23 | 24 | #include "cocaine/context/filter.hpp" 25 | #include "cocaine/locked_ptr.hpp" 26 | #include "cocaine/trace/trace.hpp" 27 | 28 | #include 29 | #include 30 | 31 | namespace cocaine { namespace logging { 32 | 33 | class trace_wrapper_t : 34 | public blackhole::logger_t 35 | { 36 | std::unique_ptr inner; 37 | synchronized> m_filter; 38 | 39 | public: 40 | trace_wrapper_t(std::unique_ptr log); 41 | 42 | auto attributes() const noexcept -> blackhole::attributes_t; 43 | 44 | auto filter(filter_t new_filter) -> void; 45 | 46 | auto log(blackhole::severity_t severity, const blackhole::message_t& message) -> void; 47 | auto log(blackhole::severity_t severity, const blackhole::message_t& message, blackhole::attribute_pack& pack) -> void; 48 | auto log(blackhole::severity_t severity, const blackhole::lazy_message_t& message, blackhole::attribute_pack& pack) -> void; 49 | 50 | auto manager() -> blackhole::scope::manager_t&; 51 | }; 52 | 53 | }} // namespace cocaine::logging 54 | 55 | #endif // COCAINE_TRACE_LOGGER 56 | -------------------------------------------------------------------------------- /include/cocaine/traits.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2011-2014 Andrey Sibiryov 3 | Copyright (c) 2011-2014 Other contributors as noted in the AUTHORS file. 4 | 5 | This file is part of Cocaine. 6 | 7 | Cocaine is free software; you can redistribute it and/or modify 8 | it under the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | Cocaine is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #ifndef COCAINE_SERIALIZATION_TRAITS_HPP 22 | #define COCAINE_SERIALIZATION_TRAITS_HPP 23 | 24 | #include 25 | 26 | namespace cocaine { namespace io { 27 | 28 | template 29 | struct type_traits { 30 | template 31 | static inline 32 | void 33 | pack(msgpack::packer& packer, const T& source) { 34 | packer << source; 35 | } 36 | 37 | static inline 38 | void 39 | unpack(const msgpack::object& unpacked, T& target) { 40 | unpacked >> target; 41 | } 42 | }; 43 | 44 | }} // namespace cocaine::io 45 | 46 | #endif 47 | -------------------------------------------------------------------------------- /include/cocaine/traits/endpoint.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2013-2014 Andrey Goryachev 3 | Copyright (c) 2011-2014 Other contributors as noted in the AUTHORS file. 4 | 5 | This file is part of Cocaine. 6 | 7 | Cocaine is free software; you can redistribute it and/or modify 8 | it under the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | Cocaine is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #ifndef COCAINE_IO_ENDPOINT_SERIALIZATION_TRAITS_HPP 22 | #define COCAINE_IO_ENDPOINT_SERIALIZATION_TRAITS_HPP 23 | 24 | #include "cocaine/traits.hpp" 25 | #include "cocaine/traits/tuple.hpp" 26 | 27 | #include 28 | 29 | #include 30 | 31 | namespace cocaine { namespace io { 32 | 33 | // Addresses are packed as strings in order for other languages like Python or JS to be able to use 34 | // them without figuring out the correct sockaddr structure formats. 35 | 36 | template 37 | struct type_traits> { 38 | typedef asio::ip::basic_endpoint endpoint_type; 39 | typedef boost::mpl::vector storage_type; 40 | 41 | template 42 | static inline 43 | void 44 | pack(msgpack::packer& target, const endpoint_type& source) { 45 | const std::string address = source.address().to_string(); 46 | const unsigned short port = source.port(); 47 | 48 | type_traits::pack(target, address, port); 49 | } 50 | 51 | static inline 52 | void 53 | unpack(const msgpack::object& source, endpoint_type& target) { 54 | std::string address; 55 | unsigned short port; 56 | 57 | type_traits::unpack(source, address, port); 58 | 59 | target.address(asio::ip::address::from_string(address)); 60 | target.port(port); 61 | } 62 | }; 63 | 64 | }} // namespace cocaine::io 65 | 66 | #endif 67 | -------------------------------------------------------------------------------- /include/cocaine/traits/enum.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2011-2014 Andrey Sibiryov 3 | Copyright (c) 2011-2014 Other contributors as noted in the AUTHORS file. 4 | 5 | This file is part of Cocaine. 6 | 7 | Cocaine is free software; you can redistribute it and/or modify 8 | it under the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | Cocaine is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #ifndef COCAINE_ENUM_SERIALIZATION_TRAITS_HPP 22 | #define COCAINE_ENUM_SERIALIZATION_TRAITS_HPP 23 | 24 | #include "cocaine/platform.hpp" 25 | #include "cocaine/traits.hpp" 26 | 27 | #include 28 | 29 | namespace cocaine { namespace io { 30 | 31 | // This magic specialization allows to pack enumerations. On older compilers it will assume that 32 | // the underlying type is int, on GCC 4.7 and Clang it can detect the underlying type. 33 | 34 | template 35 | struct type_traits< 36 | T, 37 | typename std::enable_if::value>::type 38 | > 39 | { 40 | #ifdef COCAINE_HAS_FEATURE_UNDERLYING_TYPE 41 | typedef typename std::underlying_type::type base_type; 42 | #else 43 | typedef int base_type; 44 | #endif 45 | 46 | template 47 | static inline 48 | void 49 | pack(msgpack::packer& target, const T& source) { 50 | target << static_cast(source); 51 | } 52 | 53 | static inline 54 | void 55 | unpack(const msgpack::object& source, T& target) { 56 | target = static_cast(source.as()); 57 | } 58 | }; 59 | 60 | }} // namespace cocaine::io 61 | 62 | #endif 63 | -------------------------------------------------------------------------------- /include/cocaine/traits/error_code.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2011-2015 Andrey Sibiryov 3 | Copyright (c) 2011-2015 Other contributors as noted in the AUTHORS file. 4 | 5 | This file is part of Cocaine. 6 | 7 | Cocaine is free software; you can redistribute it and/or modify 8 | it under the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | Cocaine is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #ifndef COCAINE_ERROR_CODE_SERIALIZATION_TRAITS_HPP 22 | #define COCAINE_ERROR_CODE_SERIALIZATION_TRAITS_HPP 23 | 24 | #include "cocaine/errors.hpp" 25 | #include "cocaine/traits.hpp" 26 | #include "cocaine/traits/tuple.hpp" 27 | 28 | #include 29 | 30 | namespace cocaine { namespace io { 31 | 32 | template<> 33 | struct type_traits { 34 | typedef boost::mpl::list::type sequence_type; 35 | 36 | template 37 | static inline 38 | void 39 | pack(msgpack::packer& target, const std::error_code& source) { 40 | int category_id = error::registrar::map(source.category()); 41 | int ec = source.value(); 42 | 43 | type_traits::pack(target, category_id, ec); 44 | } 45 | 46 | static inline 47 | void 48 | unpack(const msgpack::object& source, std::error_code& target) { 49 | int category_id; 50 | int ec; 51 | 52 | type_traits::unpack(source, category_id, ec); 53 | 54 | target.assign(ec, error::registrar::map(category_id)); 55 | } 56 | }; 57 | 58 | }} // namespace cocaine::io 59 | 60 | #endif 61 | -------------------------------------------------------------------------------- /include/cocaine/traits/frozen.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2013-2014 Andrey Goryachev 3 | Copyright (c) 2011-2014 Other contributors as noted in the AUTHORS file. 4 | 5 | This file is part of Cocaine. 6 | 7 | Cocaine is free software; you can redistribute it and/or modify 8 | it under the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | Cocaine is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #ifndef COCAINE_IO_FROZEN_SERIALIZATION_TRAITS_HPP 22 | #define COCAINE_IO_FROZEN_SERIALIZATION_TRAITS_HPP 23 | 24 | #include "cocaine/traits.hpp" 25 | #include "cocaine/traits/tuple.hpp" 26 | 27 | #include "cocaine/rpc/frozen.hpp" 28 | 29 | namespace cocaine { namespace io { 30 | 31 | template 32 | struct type_traits> { 33 | template 34 | static inline 35 | void 36 | pack(msgpack::packer& target, const frozen& source) { 37 | type_traits::tuple_type>::pack(target, source.tuple); 38 | } 39 | 40 | static inline 41 | void 42 | unpack(const msgpack::object& source, frozen& target) { 43 | type_traits::tuple_type>::unpack(source, target.tuple); 44 | } 45 | }; 46 | 47 | }} // namespace cocaine::io 48 | 49 | #endif 50 | -------------------------------------------------------------------------------- /include/cocaine/traits/graph.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2011-2014 Andrey Sibiryov 3 | Copyright (c) 2011-2014 Other contributors as noted in the AUTHORS file. 4 | 5 | This file is part of Cocaine. 6 | 7 | Cocaine is free software; you can redistribute it and/or modify 8 | it under the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | Cocaine is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #ifndef COCAINE_IO_DISPATCH_GRAPH_SERIALIZATION_TRAITS_HPP 22 | #define COCAINE_IO_DISPATCH_GRAPH_SERIALIZATION_TRAITS_HPP 23 | 24 | #include "cocaine/traits.hpp" 25 | #include "cocaine/traits/optional.hpp" 26 | 27 | #include "cocaine/rpc/graph.hpp" 28 | 29 | namespace cocaine { namespace io { 30 | 31 | template<> 32 | struct type_traits { 33 | template 34 | static inline 35 | void 36 | pack(msgpack::packer& target, const graph_node_t& source) { 37 | target << static_cast(source); 38 | } 39 | 40 | static inline 41 | void 42 | unpack(const msgpack::object& source, graph_node_t& target) { 43 | source >> static_cast(target); 44 | } 45 | }; 46 | 47 | }} // namespace cocaine::io 48 | 49 | #endif 50 | -------------------------------------------------------------------------------- /include/cocaine/traits/literal.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2011-2014 Andrey Sibiryov 3 | Copyright (c) 2011-2014 Other contributors as noted in the AUTHORS file. 4 | 5 | This file is part of Cocaine. 6 | 7 | Cocaine is free software; you can redistribute it and/or modify 8 | it under the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | Cocaine is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #ifndef COCAINE_LITERAL_SERIALIZATION_TRAITS_HPP 22 | #define COCAINE_LITERAL_SERIALIZATION_TRAITS_HPP 23 | 24 | #include "cocaine/traits.hpp" 25 | 26 | namespace cocaine { namespace io { 27 | 28 | // This magic specialization allows to pack string literals. It packs only the meaningful bytes, 29 | // trailing zero is dropped. Unpacking is intentionally prohibited as it might force us to silently 30 | // drop characters if the buffer is not long enough. 31 | 32 | template 33 | struct type_traits { 34 | template 35 | static inline 36 | void 37 | pack(msgpack::packer& target, const char* source) { 38 | target.pack_raw(N - 1); 39 | target.pack_raw_body(source, N - 1); 40 | } 41 | }; 42 | 43 | // Specialization to pack character arrays without copying to a std::string first. 44 | 45 | struct literal_t { 46 | const char * blob; 47 | const size_t size; 48 | 49 | // This is needed to mark this struct as implicitly convertible to std::string, although this 50 | // conversion never takes place, only statically checked in the typelist traits. 51 | operator std::string() const; 52 | }; 53 | 54 | template<> 55 | struct type_traits { 56 | template 57 | static inline 58 | void 59 | pack(msgpack::packer& target, const literal_t& source) { 60 | target.pack_raw(source.size); 61 | target.pack_raw_body(source.blob, source.size); 62 | } 63 | 64 | template 65 | static inline 66 | void 67 | pack(msgpack::packer& target, const std::string& source) { 68 | target << source; 69 | } 70 | 71 | static inline 72 | void 73 | unpack(const msgpack::object& source, std::string& target) { 74 | source >> target; 75 | } 76 | }; 77 | 78 | }} // namespace cocaine::io 79 | 80 | #endif 81 | -------------------------------------------------------------------------------- /include/cocaine/traits/map.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2013-2014 Andrey Goryachev 3 | Copyright (c) 2011-2014 Other contributors as noted in the AUTHORS file. 4 | 5 | This file is part of Cocaine. 6 | 7 | Cocaine is free software; you can redistribute it and/or modify 8 | it under the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | Cocaine is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #ifndef COCAINE_IO_MAP_SERIALIZATION_TRAITS_HPP 22 | #define COCAINE_IO_MAP_SERIALIZATION_TRAITS_HPP 23 | 24 | #include "cocaine/traits.hpp" 25 | 26 | #include 27 | 28 | namespace cocaine { namespace io { 29 | 30 | template 31 | struct type_traits> { 32 | template 33 | static inline 34 | void 35 | pack(msgpack::packer& target, const std::map& source) { 36 | target.pack_map(source.size()); 37 | 38 | for(auto it = source.begin(); it != source.end(); ++it) { 39 | type_traits::pack(target, it->first); 40 | type_traits::pack(target, it->second); 41 | } 42 | } 43 | 44 | static inline 45 | void 46 | unpack(const msgpack::object& source, std::map& target) { 47 | if(source.type != msgpack::type::MAP) { 48 | throw msgpack::type_error(); 49 | } 50 | 51 | target.clear(); 52 | 53 | for(size_t i = 0; i < source.via.map.size; ++i) { 54 | std::pair map_pair; 55 | 56 | type_traits::unpack(source.via.map.ptr[i].key, map_pair.first); 57 | type_traits::unpack(source.via.map.ptr[i].val, map_pair.second); 58 | 59 | target.insert(std::move(map_pair)); 60 | } 61 | } 62 | }; 63 | 64 | }} // namespace cocaine::io 65 | 66 | #endif 67 | -------------------------------------------------------------------------------- /include/cocaine/traits/optional.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2011-2014 Andrey Sibiryov 3 | Copyright (c) 2011-2014 Other contributors as noted in the AUTHORS file. 4 | 5 | This file is part of Cocaine. 6 | 7 | Cocaine is free software; you can redistribute it and/or modify 8 | it under the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | Cocaine is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #ifndef COCAINE_IO_OPTIONAL_SERIALIZATION_TRAITS_HPP 22 | #define COCAINE_IO_OPTIONAL_SERIALIZATION_TRAITS_HPP 23 | 24 | #include "cocaine/traits.hpp" 25 | 26 | #include 27 | 28 | namespace cocaine { namespace io { 29 | 30 | template 31 | struct type_traits> { 32 | template 33 | static inline 34 | void 35 | pack(msgpack::packer& target, const boost::optional& source) { 36 | if(source) { 37 | type_traits::pack(target, *source); 38 | } else { 39 | target << msgpack::type::nil(); 40 | } 41 | } 42 | 43 | static inline 44 | void 45 | unpack(const msgpack::object& source, boost::optional& target) { 46 | if(source.type != msgpack::type::NIL) { 47 | target = T(); 48 | type_traits::unpack(source, *target); 49 | } else { 50 | target = boost::none; 51 | } 52 | } 53 | }; 54 | 55 | }} // namespace cocaine::io 56 | 57 | #endif 58 | -------------------------------------------------------------------------------- /include/cocaine/traits/siginfo.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2011-2014 Andrey Sibiryov 3 | Copyright (c) 2011-2014 Other contributors as noted in the AUTHORS file. 4 | 5 | This file is part of Cocaine. 6 | 7 | Cocaine is free software; you can redistribute it and/or modify 8 | it under the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | Cocaine is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #pragma once 22 | 23 | #include "cocaine/traits.hpp" 24 | 25 | #include 26 | 27 | #include 28 | 29 | namespace cocaine { namespace io { 30 | 31 | // Stub implementation for context disppatch with os_signal event 32 | 33 | template<> 34 | struct type_traits { 35 | template 36 | static inline 37 | void 38 | pack(msgpack::packer&, siginfo_t) { 39 | BOOST_ASSERT(false); 40 | } 41 | 42 | static inline 43 | void 44 | unpack(const msgpack::object&, siginfo_t&) { 45 | BOOST_ASSERT(false); 46 | } 47 | }; 48 | 49 | }} // namespace cocaine::io 50 | -------------------------------------------------------------------------------- /include/cocaine/traits/vector.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2013-2014 Andrey Goryachev 3 | Copyright (c) 2011-2014 Other contributors as noted in the AUTHORS file. 4 | 5 | This file is part of Cocaine. 6 | 7 | Cocaine is free software; you can redistribute it and/or modify 8 | it under the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | Cocaine is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #ifndef COCAINE_IO_VECTOR_SERIALIZATION_TRAITS_HPP 22 | #define COCAINE_IO_VECTOR_SERIALIZATION_TRAITS_HPP 23 | 24 | #include "cocaine/traits.hpp" 25 | 26 | #include 27 | 28 | #ifdef __has_include 29 | #if __has_include() 30 | #include 31 | #endif 32 | #endif 33 | 34 | namespace cocaine { namespace io { 35 | 36 | template 37 | struct vector_traits { 38 | typedef T vector_type; 39 | typedef typename T::value_type value_type; 40 | 41 | template 42 | static inline 43 | void 44 | pack(msgpack::packer& target, const vector_type& source) { 45 | target.pack_array(source.size()); 46 | 47 | for(auto it = source.begin(); it != source.end(); ++it) { 48 | type_traits::pack(target, *it); 49 | } 50 | } 51 | 52 | static inline 53 | void 54 | unpack(const msgpack::object& source, vector_type& target) { 55 | if(source.type != msgpack::type::ARRAY) { 56 | throw msgpack::type_error(); 57 | } 58 | 59 | target.assign(source.via.array.size, value_type()); 60 | 61 | for(size_t i = 0; i < source.via.array.size; ++i) { 62 | type_traits::unpack(source.via.array.ptr[i], target[i]); 63 | } 64 | } 65 | }; 66 | 67 | template 68 | struct type_traits> : 69 | public vector_traits> {}; 70 | 71 | #ifdef __has_include 72 | #if __has_include() 73 | 74 | template 75 | struct type_traits> : 76 | public vector_traits> {}; 77 | 78 | #endif 79 | #endif 80 | 81 | }} // namespace cocaine::io 82 | 83 | #endif 84 | -------------------------------------------------------------------------------- /include/cocaine/unicorn/path.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 2015+ Copyright (c) Anton Matveenko 3 | * All rights reserved. 4 | * 5 | * This program is free software; you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation; either version 2 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | */ 15 | 16 | #ifndef COCAINE_UNICORN_PATH 17 | #define COCAINE_UNICORN_PATH 18 | 19 | #include 20 | 21 | namespace cocaine { namespace unicorn { 22 | typedef std::string path_t; 23 | }} 24 | 25 | #endif // COCAINE_UNICORN_PATH 26 | -------------------------------------------------------------------------------- /include/cocaine/unicorn/value.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 2015+ Copyright (c) Anton Matveenko 3 | * All rights reserved. 4 | * 5 | * This program is free software; you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation; either version 2 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | */ 15 | 16 | #pragma once 17 | 18 | #include "cocaine/common.hpp" 19 | #include "cocaine/dynamic.hpp" 20 | 21 | namespace cocaine { 22 | namespace unicorn { 23 | 24 | using value_t = dynamic_t; 25 | using version_t = long long; 26 | 27 | static constexpr version_t min_version = -2; 28 | static constexpr version_t not_existing_version = -1; 29 | 30 | class versioned_value_t { 31 | struct { 32 | value_t value; 33 | version_t version; 34 | } data; 35 | 36 | public: 37 | versioned_value_t(value_t value, version_t version); 38 | 39 | auto 40 | value() const -> const value_t& { 41 | return data.value; 42 | } 43 | 44 | auto 45 | version() const -> version_t { 46 | return data.version; 47 | } 48 | 49 | auto 50 | exists() const noexcept -> bool { 51 | return version() != not_existing_version; 52 | } 53 | }; 54 | 55 | } // namespace unicorn 56 | } // namespace cocaine 57 | -------------------------------------------------------------------------------- /include/cocaine/unique_id.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2011-2014 Andrey Sibiryov 3 | Copyright (c) 2011-2014 Other contributors as noted in the AUTHORS file. 4 | 5 | This file is part of Cocaine. 6 | 7 | Cocaine is free software; you can redistribute it and/or modify 8 | it under the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | Cocaine is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #ifndef COCAINE_UNIQUE_ID_HPP 22 | #define COCAINE_UNIQUE_ID_HPP 23 | 24 | #include "cocaine/common.hpp" 25 | 26 | #include 27 | #include 28 | #include 29 | 30 | namespace cocaine { 31 | 32 | struct unique_id_t { 33 | unique_id_t(); 34 | 35 | explicit 36 | unique_id_t(const std::string& other); 37 | 38 | std::string 39 | string() const; 40 | 41 | bool 42 | operator==(const unique_id_t& other) const; 43 | 44 | friend 45 | std::ostream& 46 | operator<<(std::ostream& stream, const unique_id_t& id); 47 | 48 | public: 49 | // NOTE: Store 128-bit UUIDs as two 64-bit unsigned integers. 50 | std::array uuid; 51 | }; 52 | 53 | } // namespace cocaine 54 | 55 | #endif 56 | -------------------------------------------------------------------------------- /include/cocaine/utility/exchange.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | namespace cocaine { 4 | namespace utility { 5 | 6 | /// Replaces the value of curr with next and returns the old value of curr. 7 | /// 8 | /// \param curr - object whose value to replace. 9 | /// \param next - the value to assign to curr. 10 | /// \return The old value of curr. 11 | template 12 | T 13 | exchange(T& curr, U&& next) { 14 | T prev = std::move(curr); 15 | curr = std::forward(next); 16 | return prev; 17 | } 18 | 19 | } // namespace utility 20 | } // namespace cocaine 21 | -------------------------------------------------------------------------------- /include/cocaine/utility/future.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 2015+ Copyright (c) Anton Matveenko 3 | * All rights reserved. 4 | * 5 | * This program is free software; you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation; either version 2 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | */ 15 | 16 | #pragma once 17 | 18 | #include 19 | #include 20 | #include 21 | 22 | namespace cocaine { 23 | 24 | template 25 | std::future::type> make_ready_future(T&& value) { 26 | std::promise::type> promise; 27 | promise.set_value(std::forward(value)); 28 | return promise.get_future(); 29 | } 30 | 31 | inline 32 | std::future make_ready_future() { 33 | std::promise promise; 34 | promise.set_value(); 35 | return promise.get_future(); 36 | 37 | } 38 | 39 | template 40 | std::future make_exceptional_future() { 41 | std::promise promise; 42 | promise.set_exception(std::current_exception()); 43 | return promise.get_future(); 44 | } 45 | 46 | template 47 | std::future make_exceptional_future(E&& e) { 48 | std::promise promise; 49 | auto e_ptr = std::make_exception_ptr(e); 50 | promise.set_exception(e_ptr); 51 | return promise.get_future(); 52 | } 53 | 54 | template 55 | std::future make_exceptional_future(std::error_code ec) { 56 | return make_exceptional_future(std::system_error(std::move(ec))); 57 | } 58 | 59 | template 60 | std::future make_exceptional_future(std::error_code ec, std::string msg) { 61 | return make_exceptional_future(std::system_error(std::move(ec), std::move(msg))); 62 | } 63 | 64 | } 65 | -------------------------------------------------------------------------------- /libcocaine-core3.version: -------------------------------------------------------------------------------- 1 | { 2 | global: 3 | *; 4 | local: 5 | *rapidjson*; 6 | }; 7 | -------------------------------------------------------------------------------- /src/api/authentication.cpp: -------------------------------------------------------------------------------- 1 | #include "cocaine/api/authentication.hpp" 2 | 3 | #include 4 | 5 | #include "cocaine/context.hpp" 6 | #include "cocaine/context/config.hpp" 7 | #include "cocaine/hpack/header_definitions.hpp" 8 | #include "cocaine/repository.hpp" 9 | #include "cocaine/repository/authentication.hpp" 10 | 11 | namespace cocaine { 12 | namespace api { 13 | 14 | auto 15 | authentication_t::identify(const hpack::headers_t& headers) const -> auth::identity_t { 16 | // Even if there is no credentials provided some authorization components may allow access. 17 | std::string credentials; 18 | if (auto header = hpack::header::find_first>(headers)) { 19 | credentials = header->value(); 20 | } 21 | 22 | return identify(credentials); 23 | } 24 | 25 | auto 26 | authentication(context_t& context, const std::string& name, const std::string& service) -> 27 | std::shared_ptr 28 | { 29 | if (auto cfg = context.config().component_group("authentications").get(name)) { 30 | return context.repository().get(cfg->type(), context, name, service, cfg->args()); 31 | } 32 | 33 | throw error_t(error::component_not_found, "authentication component \"{}\" not found in the config", name); 34 | } 35 | 36 | } // namespace api 37 | } // namespace cocaine 38 | -------------------------------------------------------------------------------- /src/api/authorization.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include 6 | 7 | #include "cocaine/api/authorization/event.hpp" 8 | #include "cocaine/api/authorization/storage.hpp" 9 | #include "cocaine/api/authorization/unicorn.hpp" 10 | #include "cocaine/context.hpp" 11 | #include "cocaine/context/config.hpp" 12 | #include "cocaine/forwards.hpp" 13 | #include "cocaine/repository.hpp" 14 | #include "cocaine/repository/authorization.hpp" 15 | #include "cocaine/rpc/protocol.hpp" 16 | 17 | namespace cocaine { 18 | namespace api { 19 | namespace authorization { 20 | 21 | auto 22 | event(context_t& context, const std::string& service) -> std::shared_ptr { 23 | const auto name = "event"; 24 | if (auto cfg = context.config().component_group("authorizations").get(name)) { 25 | return context.repository().get(cfg->type(), context, name, service, cfg->args()); 26 | } 27 | 28 | throw error_t(error::component_not_found, "authorizations \"event\" component not found in the config"); 29 | } 30 | 31 | auto 32 | storage(context_t& context, const std::string& service) -> std::shared_ptr { 33 | const auto name = "storage"; 34 | if (auto cfg = context.config().component_group("authorizations").get(name)) { 35 | return context.repository().get(cfg->type(), context, name, service, cfg->args()); 36 | } 37 | 38 | throw error_t(error::component_not_found, "authorizations \"storage\" component not found in the config"); 39 | } 40 | 41 | auto 42 | unicorn(context_t& context, const std::string& service) -> std::shared_ptr { 43 | const auto name = "unicorn"; 44 | if (auto cfg = context.config().component_group("authorizations").get(name)) { 45 | return context.repository().get(cfg->type(), context, name, service, cfg->args()); 46 | } 47 | 48 | throw error_t(error::component_not_found, "authorizations \"unicorn\" component not found in the config"); 49 | } 50 | 51 | } // namespace authorization 52 | } // namespace api 53 | } // namespace cocaine 54 | -------------------------------------------------------------------------------- /src/api/storage.cpp: -------------------------------------------------------------------------------- 1 | #include "cocaine/api/storage.hpp" 2 | #include "cocaine/context.hpp" 3 | #include "cocaine/context/config.hpp" 4 | #include "cocaine/repository/storage.hpp" 5 | 6 | #include 7 | 8 | #include 9 | 10 | namespace cocaine { 11 | namespace api { 12 | namespace ph = std::placeholders; 13 | 14 | std::future 15 | storage_t::read(const std::string& collection, const std::string& key) { 16 | auto promise = std::make_shared>(); 17 | read(collection, key, [=](std::future future){ 18 | assign_future_result(*promise, std::move(future)); 19 | }); 20 | return promise->get_future(); 21 | } 22 | 23 | std::future 24 | storage_t::write(const std::string& collection, 25 | const std::string& key, 26 | const std::string& blob, 27 | const std::vector& tags) { 28 | auto promise = std::make_shared>(); 29 | write(collection, key, blob, tags, [=](std::future future){ 30 | assign_future_result(*promise, std::move(future)); 31 | }); 32 | return promise->get_future(); 33 | } 34 | 35 | std::future 36 | storage_t::remove(const std::string& collection, const std::string& key) { 37 | auto promise = std::make_shared>(); 38 | remove(collection, key, [=](std::future future){ 39 | assign_future_result(*promise, std::move(future)); 40 | }); 41 | return promise->get_future(); 42 | } 43 | 44 | std::future> 45 | storage_t::find(const std::string& collection, const std::vector& tags) { 46 | auto promise = std::make_shared>>(); 47 | find(collection, tags, [=](std::future> future){ 48 | assign_future_result(*promise, std::move(future)); 49 | }); 50 | return promise->get_future(); 51 | } 52 | 53 | storage_ptr 54 | storage(context_t& context, const std::string& name) { 55 | auto storage = context.config().storages().get(name); 56 | if(!storage) { 57 | throw error_t(error::component_not_found, "storage component \"{}\" not found in the config", name); 58 | } 59 | return context.repository().get(storage->type(), context, name, storage->args()); 60 | } 61 | 62 | } // namespace api 63 | } // namespace cocaine 64 | -------------------------------------------------------------------------------- /src/api/unicorn.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2011-2014 Andrey Sibiryov 3 | Copyright (c) 2011-2014 Other contributors as noted in the AUTHORS file. 4 | 5 | This file is part of Cocaine. 6 | 7 | Cocaine is free software; you can redistribute it and/or modify 8 | it under the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | Cocaine is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #include "cocaine/api/unicorn.hpp" 22 | #include "cocaine/context.hpp" 23 | #include "cocaine/context/config.hpp" 24 | #include "cocaine/repository/unicorn.hpp" 25 | 26 | #include 27 | 28 | namespace cocaine { namespace api { 29 | 30 | auto_scope_t::auto_scope_t(unicorn_scope_ptr wrapped) : 31 | wrapped(std::move(wrapped)) 32 | {} 33 | 34 | auto_scope_t::auto_scope_t(auto_scope_t&& other) : 35 | wrapped(std::move(other.wrapped)) 36 | {} 37 | 38 | auto_scope_t& auto_scope_t::operator=(auto_scope_t&& other) 39 | { 40 | close(); 41 | wrapped = std::move(other.wrapped); 42 | return *this; 43 | } 44 | 45 | auto_scope_t::~auto_scope_t() { 46 | close(); 47 | } 48 | 49 | auto auto_scope_t::close() -> void { 50 | if(wrapped){ 51 | wrapped->close(); 52 | } 53 | } 54 | 55 | // Unicorn 56 | 57 | unicorn_t::unicorn_t(context_t& /*context*/, const std::string& /*name*/, const dynamic_t& /*args*/) { 58 | // empty 59 | } 60 | 61 | /** 62 | * Unicorn trait for service creation. 63 | * Trait to create unicorn service by name. All instances are cached by name as it is done in storage. 64 | */ 65 | unicorn_ptr 66 | unicorn(context_t& context, const std::string& name) { 67 | auto unicorn = context.config().unicorns().get(name); 68 | if(!unicorn) { 69 | throw error_t(error::component_not_found, "unicorn component \"{}\" not found in the config", name); 70 | } 71 | return context.repository().get(unicorn->type(), context, name, unicorn->args()); 72 | } 73 | 74 | }} // namespace cocaine::api 75 | -------------------------------------------------------------------------------- /src/auth/uid.cpp: -------------------------------------------------------------------------------- 1 | #include "cocaine/auth/uid.hpp" 2 | 3 | namespace cocaine { 4 | namespace auth { 5 | 6 | struct identity_t::data_t { 7 | std::vector cids; 8 | std::vector uids; 9 | }; 10 | 11 | identity_t::builder_t::builder_t() : 12 | d(new data_t) 13 | {} 14 | 15 | identity_t::builder_t::~builder_t() = default; 16 | 17 | auto 18 | identity_t::builder_t::cids(std::vector cids) & -> builder_t& { 19 | d->cids = std::move(cids); 20 | return *this; 21 | } 22 | 23 | auto 24 | identity_t::builder_t::cids(std::vector cids) && -> builder_t&& { 25 | d->cids = std::move(cids); 26 | return std::move(*this); 27 | } 28 | 29 | auto 30 | identity_t::builder_t::uids(std::vector uids) & -> builder_t& { 31 | d->uids = std::move(uids); 32 | return *this; 33 | } 34 | 35 | auto 36 | identity_t::builder_t::uids(std::vector uids) && -> builder_t&& { 37 | d->uids = std::move(uids); 38 | return std::move(*this); 39 | } 40 | 41 | auto 42 | identity_t::builder_t::build() && -> identity_t { 43 | return identity_t{std::move(this->d)}; 44 | } 45 | 46 | identity_t::identity_t() : 47 | d(std::make_shared()) 48 | {} 49 | 50 | identity_t::identity_t(std::unique_ptr d) : 51 | d(std::move(d)) 52 | {} 53 | 54 | identity_t::identity_t(std::vector uids) : 55 | d(std::make_shared()) 56 | { 57 | d->uids = std::move(uids); 58 | } 59 | 60 | auto 61 | identity_t::cids() const -> const std::vector& { 62 | return d->cids; 63 | } 64 | 65 | auto 66 | identity_t::uids() const -> const std::vector& { 67 | return d->uids; 68 | } 69 | 70 | } // namespace auth 71 | } // namespace cocaine 72 | -------------------------------------------------------------------------------- /src/authentication/promiscuous.hpp: -------------------------------------------------------------------------------- 1 | #include "cocaine/api/authentication.hpp" 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | #include "cocaine/auth/uid.hpp" 8 | 9 | namespace cocaine { 10 | namespace authentication { 11 | 12 | class promiscuous_t : public api::authentication_t { 13 | public: 14 | typedef api::authentication_t::callback_type callback_type; 15 | 16 | public: 17 | promiscuous_t(context_t&, const std::string&, const dynamic_t&) {} 18 | 19 | auto 20 | token(callback_type callback) -> void override { 21 | callback({{}, {}, {}}, {}); // Bayan. 22 | } 23 | 24 | using api::authentication_t::identify; 25 | 26 | auto 27 | identify(const std::string& credentials) const -> auth::identity_t override { 28 | try { 29 | std::vector splitted; 30 | boost::split(splitted, credentials, boost::is_any_of(",")); 31 | 32 | std::vector uids; 33 | for (const auto& uid : splitted) { 34 | uids.push_back(boost::lexical_cast(uid)); 35 | } 36 | return auth::identity_t::builder_t().uids(std::move(uids)).build(); 37 | } catch (const std::exception&) { 38 | return auth::identity_t::builder_t().build(); 39 | } 40 | } 41 | }; 42 | 43 | } // namespace authentication 44 | } // namespace cocaine 45 | -------------------------------------------------------------------------------- /src/authorization/event.hpp: -------------------------------------------------------------------------------- 1 | #include "cocaine/api/authorization/event.hpp" 2 | 3 | namespace cocaine { 4 | namespace authorization { 5 | namespace event { 6 | 7 | class disabled_t : public api::authorization::event_t { 8 | public: 9 | disabled_t(context_t& context, const std::string& service, const dynamic_t& args); 10 | 11 | auto 12 | verify(const std::string& event, const auth::identity_t& identity) -> void override; 13 | }; 14 | 15 | class enabled_t : public api::authorization::event_t { 16 | class control_t; 17 | std::shared_ptr control; 18 | 19 | const std::shared_ptr log; 20 | 21 | public: 22 | enabled_t(context_t& context, const std::string& service, const dynamic_t& args); 23 | ~enabled_t(); 24 | 25 | auto 26 | verify(const std::string& event, const auth::identity_t& identity) -> void override; 27 | }; 28 | 29 | } // namespace event 30 | } // namespace authorization 31 | } // namespace cocaine 32 | -------------------------------------------------------------------------------- /src/authorization/storage.hpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include 5 | 6 | #include "cocaine/api/authorization/storage.hpp" 7 | #include "cocaine/forwards.hpp" 8 | #include "cocaine/locked_ptr.hpp" 9 | #include "storage/metainfo.hpp" 10 | 11 | namespace cocaine { 12 | namespace authorization { 13 | namespace storage { 14 | 15 | class disabled_t : public api::authorization::storage_t { 16 | public: 17 | disabled_t(context_t& context, const std::string& service, const dynamic_t& args); 18 | 19 | auto 20 | verify(std::size_t event, const std::string& collection, const std::string& key, const auth::identity_t& identity, callback_type callback) 21 | -> void override; 22 | }; 23 | 24 | class enabled_t : public api::authorization::storage_t { 25 | std::unique_ptr log; 26 | std::shared_ptr backend; 27 | 28 | std::chrono::seconds cache_duration; 29 | synchronized>> cache; 30 | 31 | public: 32 | enabled_t(context_t& context, const std::string& service, const dynamic_t& args); 33 | 34 | auto 35 | verify(std::size_t event, const std::string& collection, const std::string& key, const auth::identity_t& identity, callback_type callback) 36 | -> void override; 37 | 38 | private: 39 | auto 40 | verify(std::size_t event, const std::string& collection, const auth::identity_t& identity, callback_type callback) 41 | -> void; 42 | }; 43 | 44 | } // namespace storage 45 | } // namespace authorization 46 | } // namespace cocaine 47 | -------------------------------------------------------------------------------- /src/authorization/storage/metainfo.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | #include "cocaine/traits/enum.hpp" 8 | #include "cocaine/traits/map.hpp" 9 | #include "cocaine/traits/tuple.hpp" 10 | 11 | namespace cocaine { 12 | namespace authorization { 13 | namespace storage { 14 | 15 | enum flags_t : std::size_t { none = 0x00, read = 0x01, write = 0x02, both = read | write }; 16 | 17 | struct metainfo_t { 18 | std::map c_perms; 19 | std::map u_perms; 20 | 21 | auto 22 | empty() const -> bool { 23 | return c_perms.empty() && u_perms.empty(); 24 | } 25 | }; 26 | 27 | template 28 | class cached { 29 | public: 30 | using value_type = T; 31 | using clock_type = std::chrono::system_clock; 32 | 33 | enum class tag_t { actual, expired }; 34 | 35 | private: 36 | value_type value; 37 | clock_type::time_point expiration_time; 38 | 39 | public: 40 | cached(value_type value, clock_type::duration duration) : 41 | value(std::move(value)), 42 | expiration_time(clock_type::now() + duration) 43 | {} 44 | 45 | auto 46 | is_expired() const -> bool { 47 | return clock_type::now() > expiration_time; 48 | } 49 | 50 | auto 51 | get() const -> std::tuple { 52 | auto tag = tag_t::actual; 53 | if (is_expired()) { 54 | tag = tag_t::expired; 55 | } 56 | 57 | return std::make_tuple(tag, value); 58 | } 59 | }; 60 | 61 | } // namespace storage 62 | } // namespace authorization 63 | } // namespace cocaine 64 | 65 | namespace cocaine { 66 | namespace io { 67 | 68 | template<> 69 | struct type_traits { 70 | typedef boost::mpl::list< 71 | std::map, 72 | std::map 73 | > underlying_type; 74 | 75 | template 76 | static 77 | void 78 | pack(msgpack::packer& target, const authorization::storage::metainfo_t& source) { 79 | type_traits::pack(target, source.c_perms, source.u_perms); 80 | } 81 | 82 | static 83 | void 84 | unpack(const msgpack::object& source, authorization::storage::metainfo_t& target) { 85 | type_traits::unpack(source, target.c_perms, target.u_perms); 86 | } 87 | }; 88 | 89 | } // namespace io 90 | } // namespace cocaine 91 | -------------------------------------------------------------------------------- /src/authorization/unicorn.cpp: -------------------------------------------------------------------------------- 1 | #include "unicorn.hpp" 2 | 3 | namespace cocaine { 4 | namespace authorization { 5 | namespace unicorn { 6 | 7 | disabled_t::disabled_t(context_t&, const std::string&, const dynamic_t&) {} 8 | 9 | auto 10 | disabled_t::verify(std::size_t, const std::string&, const auth::identity_t&, callback_type callback) 11 | -> void 12 | { 13 | callback({}); 14 | } 15 | 16 | } // namespace unicorn 17 | } // namespace authorization 18 | } // namespace cocaine 19 | -------------------------------------------------------------------------------- /src/authorization/unicorn.hpp: -------------------------------------------------------------------------------- 1 | #include "cocaine/api/authorization/unicorn.hpp" 2 | 3 | #include "cocaine/forwards.hpp" 4 | 5 | namespace cocaine { 6 | namespace authorization { 7 | namespace unicorn { 8 | 9 | class disabled_t : public api::authorization::unicorn_t { 10 | public: 11 | disabled_t(context_t& context, const std::string& service, const dynamic_t& args); 12 | 13 | auto 14 | verify(std::size_t event, const std::string& path, const auth::identity_t& identity, callback_type callback) 15 | -> void override; 16 | }; 17 | 18 | } // namespace unicorn 19 | } // namespace authorization 20 | } // namespace cocaine 21 | -------------------------------------------------------------------------------- /src/chamber.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2011-2014 Andrey Sibiryov 3 | Copyright (c) 2011-2014 Other contributors as noted in the AUTHORS file. 4 | 5 | This file is part of Cocaine. 6 | 7 | Cocaine is free software; you can redistribute it and/or modify 8 | it under the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | Cocaine is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #ifndef COCAINE_CHAMBER_HPP 22 | #define COCAINE_CHAMBER_HPP 23 | 24 | #include "cocaine/common.hpp" 25 | #include "cocaine/locked_ptr.hpp" 26 | 27 | #include 28 | #include 29 | 30 | #include 31 | #include 32 | 33 | #include 34 | 35 | namespace cocaine { namespace io { 36 | 37 | class chamber_t { 38 | class named_runnable_t; 39 | class stats_periodic_action_t; 40 | 41 | static const unsigned int kCollectionInterval = 2; 42 | 43 | const std::string name; 44 | const std::shared_ptr asio; 45 | 46 | // Takes resource usage snapshots every kCollectInterval seconds. 47 | asio::deadline_timer cron; 48 | 49 | // This thread will run the reactor's event loop until terminated. 50 | std::unique_ptr thread; 51 | 52 | typedef boost::accumulators::accumulator_set< 53 | double, 54 | boost::accumulators::features 55 | > load_average_t; 56 | 57 | // Rolling resource usage mean over last minute. 58 | synchronized load_acc1; 59 | 60 | public: 61 | chamber_t(const std::string& name, const std::shared_ptr& asio); 62 | ~chamber_t(); 63 | 64 | auto 65 | get_io_service() const -> asio::io_service& { 66 | return *asio; 67 | } 68 | 69 | auto 70 | load_avg1() const -> double { 71 | return boost::accumulators::rolling_mean(*load_acc1.synchronize()); 72 | } 73 | 74 | std::string 75 | thread_id() const; 76 | }; 77 | 78 | }} // namespace cocaine::io 79 | 80 | #endif 81 | -------------------------------------------------------------------------------- /src/context/mapper.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2011-2015 Andrey Sibiryov 3 | Copyright (c) 2011-2015 Other contributors as noted in the AUTHORS file. 4 | 5 | This file is part of Cocaine. 6 | 7 | Cocaine is free software; you can redistribute it and/or modify 8 | it under the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | Cocaine is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #include "cocaine/context/mapper.hpp" 22 | 23 | #include "cocaine/context/config.hpp" 24 | #include "cocaine/errors.hpp" 25 | 26 | #include 27 | #include 28 | #include 29 | 30 | using namespace cocaine; 31 | 32 | port_mapping_t::port_mapping_t(const config_t& config): 33 | m_pinned(config.network().ports().pinned()) 34 | { 35 | port_t minimum, maximum; 36 | 37 | std::tie(minimum, maximum) = config.network().ports().shared(); 38 | 39 | std::vector seed; 40 | 41 | if((minimum == 0 && maximum == 0) || maximum <= minimum) { 42 | seed.resize(65535); 43 | std::fill(seed.begin(), seed.end(), 0); 44 | } else { 45 | seed.resize(maximum - minimum); 46 | std::iota(seed.begin(), seed.end(), minimum); 47 | } 48 | 49 | std::random_device device; 50 | std::shuffle(seed.begin(), seed.end(), std::default_random_engine(device())); 51 | 52 | // Populate the shared port queue. 53 | m_shared = std::deque(seed.begin(), seed.end()); 54 | } 55 | 56 | port_t 57 | port_mapping_t::assign(const std::string& name) { 58 | std::lock_guard guard(m_mutex); 59 | 60 | if(m_in_use.count(name)) { 61 | throw cocaine::error_t("named port for {} is already in use", name); 62 | } 63 | 64 | if(m_pinned.count(name)) { 65 | return m_in_use.insert({name, m_pinned.at(name)}).first->second; 66 | } 67 | 68 | if(m_shared.empty()) { 69 | throw cocaine::error_t("no ports left for allocation"); 70 | } 71 | 72 | const auto port = m_shared.front(); m_shared.pop_front(); 73 | 74 | return m_in_use.insert({name, port}).first->second; 75 | } 76 | 77 | void 78 | port_mapping_t::retain(const std::string& name) { 79 | std::lock_guard guard(m_mutex); 80 | 81 | if(!m_in_use.count(name)) { 82 | throw cocaine::error_t("named port was never assigned"); 83 | } 84 | 85 | if(!m_pinned.count(name)) { 86 | m_shared.push_back(m_in_use.at(name)); 87 | } 88 | 89 | m_in_use.erase(name); 90 | } 91 | -------------------------------------------------------------------------------- /src/crypto.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2011-2014 Andrey Sibiryov 3 | Copyright (c) 2011-2014 Other contributors as noted in the AUTHORS file. 4 | 5 | This file is part of Cocaine. 6 | 7 | Cocaine is free software; you can redistribute it and/or modify 8 | it under the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | Cocaine is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #include "cocaine/detail/crypto.hpp" 22 | 23 | #include "cocaine/context.hpp" 24 | #include "cocaine/errors.hpp" 25 | #include "cocaine/logging.hpp" 26 | #include "cocaine/logging.hpp" 27 | 28 | #include 29 | 30 | using namespace cocaine; 31 | 32 | template 33 | crypto::crypto(context_t& context, const std::string& service): 34 | m_log(context.log("crypto")), 35 | m_service(service) 36 | { 37 | m_store = api::storage(context, "secure"); 38 | } 39 | 40 | template 41 | crypto::~crypto() { 42 | // Empty. 43 | } 44 | 45 | template 46 | std::string 47 | crypto::sign(const std::string& message, const std::string& token_id) const { 48 | std::string token; 49 | 50 | try { 51 | //TODO: either drop all crypto class or replace with async operation. 52 | token = m_store->read(m_service, token_id).get(); 53 | } catch(const std::system_error& e) { 54 | COCAINE_LOG_ERROR(m_log, "unable to load security token '{}' for service: {}", token_id, 55 | error::to_string(e)); 56 | throw std::system_error(error::token_not_found); 57 | } 58 | 59 | MHASH thread = mhash_hmac_init(HashID, const_cast(token.data()), token.size(), mhash_get_hash_pblock(HashID)); 60 | char* digest = static_cast(::alloca(mhash_get_block_size(HashID))); 61 | 62 | mhash(thread, message.data(), message.size()); 63 | mhash_hmac_deinit(thread, digest); 64 | 65 | return std::string(digest, mhash_get_block_size(HashID)); 66 | } 67 | 68 | template class cocaine::crypto; 69 | -------------------------------------------------------------------------------- /src/defaults.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2011-2014 Andrey Sibiryov 3 | Copyright (c) 2011-2014 Other contributors as noted in the AUTHORS file. 4 | 5 | This file is part of Cocaine. 6 | 7 | Cocaine is free software; you can redistribute it and/or modify 8 | it under the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | Cocaine is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #include "cocaine/defaults.hpp" 22 | 23 | using namespace cocaine; 24 | 25 | const std::string defaults::plugins_path = "/usr/lib/cocaine"; 26 | const std::string defaults::runtime_path = "/var/run/cocaine"; 27 | 28 | const std::string defaults::endpoint = "::"; 29 | 30 | const std::string defaults::log_verbosity = "info"; 31 | const std::string defaults::log_timestamp = "%Y-%m-%d %H:%M:%S.%f"; -------------------------------------------------------------------------------- /src/dispatch.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2011-2014 Andrey Sibiryov 3 | Copyright (c) 2011-2014 Other contributors as noted in the AUTHORS file. 4 | 5 | This file is part of Cocaine. 6 | 7 | Cocaine is free software; you can redistribute it and/or modify 8 | it under the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | Cocaine is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #include "cocaine/rpc/basic_dispatch.hpp" 22 | 23 | #include "cocaine/errors.hpp" 24 | 25 | using namespace cocaine::io; 26 | 27 | basic_dispatch_t::basic_dispatch_t(const std::string& name): 28 | m_name(name) 29 | { } 30 | 31 | basic_dispatch_t::~basic_dispatch_t() { 32 | // Empty. 33 | } 34 | 35 | auto 36 | basic_dispatch_t::attached(std::shared_ptr) -> void {} 37 | 38 | void 39 | basic_dispatch_t::discard(const std::error_code& COCAINE_UNUSED_(ec)) { 40 | // Empty. 41 | } 42 | 43 | std::string 44 | basic_dispatch_t::name() const { 45 | return m_name; 46 | } 47 | -------------------------------------------------------------------------------- /src/essentials.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2011-2014 Andrey Sibiryov 3 | Copyright (c) 2011-2014 Other contributors as noted in the AUTHORS file. 4 | 5 | This file is part of Cocaine. 6 | 7 | Cocaine is free software; you can redistribute it and/or modify 8 | it under the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | Cocaine is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #include "cocaine/detail/essentials.hpp" 22 | 23 | #include "cocaine/detail/cluster/multicast.hpp" 24 | #include "cocaine/detail/cluster/predefine.hpp" 25 | #include "cocaine/detail/gateway/adhoc.hpp" 26 | #include "cocaine/detail/service/locator.hpp" 27 | #include "cocaine/detail/service/logging.hpp" 28 | #include "cocaine/detail/service/storage.hpp" 29 | #include "cocaine/detail/storage/files.hpp" 30 | #include "cocaine/repository/authentication.hpp" 31 | #include "cocaine/repository/authorization.hpp" 32 | #include "cocaine/repository/cluster.hpp" 33 | #include "cocaine/repository/gateway.hpp" 34 | #include "cocaine/repository/service.hpp" 35 | #include "cocaine/repository/storage.hpp" 36 | 37 | #include "authentication/promiscuous.hpp" 38 | #include "authorization/event.hpp" 39 | #include "authorization/storage.hpp" 40 | #include "authorization/unicorn.hpp" 41 | 42 | void 43 | cocaine::essentials::initialize(api::repository_t& repository) { 44 | repository.insert("promiscuous"); 45 | repository.insert("disabled"); 46 | repository.insert("event"); 47 | repository.insert("disabled"); 48 | repository.insert("storage"); 49 | repository.insert("disabled"); 50 | repository.insert("multicast"); 51 | repository.insert("predefine"); 52 | repository.insert("adhoc"); 53 | repository.insert("locator"); 54 | repository.insert("logging"); 55 | repository.insert("storage"); 56 | repository.insert("files"); 57 | } 58 | -------------------------------------------------------------------------------- /src/executor/asio.cpp: -------------------------------------------------------------------------------- 1 | #include "cocaine/executor/asio.hpp" 2 | 3 | namespace cocaine { 4 | namespace executor { 5 | 6 | owning_asio_t::owning_asio_t(stop_policy_t policy): 7 | io_loop(), 8 | work(asio::io_service::work(io_loop)), 9 | thread([&](){ io_loop.run(); }), 10 | stop_policy(policy) 11 | {} 12 | 13 | owning_asio_t::~owning_asio_t() { 14 | work.reset(); 15 | if(stop_policy == stop_policy_t::force) { 16 | io_loop.stop(); 17 | } 18 | thread.join(); 19 | } 20 | 21 | auto 22 | owning_asio_t::spawn(work_t work) -> void { 23 | io_loop.post(std::move(work)); 24 | } 25 | 26 | auto 27 | owning_asio_t::asio() -> asio::io_service& { 28 | return io_loop; 29 | } 30 | 31 | borrowing_asio_t::borrowing_asio_t(asio::io_service& _io_loop) : 32 | io_loop(_io_loop) 33 | {} 34 | 35 | auto 36 | borrowing_asio_t::spawn(work_t work) -> void { 37 | io_loop.post(std::move(work)); 38 | } 39 | 40 | } // namespace executor 41 | } // namespace cocaine 42 | -------------------------------------------------------------------------------- /src/logging.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2011-2014 Andrey Sibiryov 3 | Copyright (c) 2011-2014 Other contributors as noted in the AUTHORS file. 4 | 5 | This file is part of Cocaine. 6 | 7 | Cocaine is free software; you can redistribute it and/or modify 8 | it under the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | Cocaine is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #include "cocaine/logging.hpp" 22 | 23 | #include 24 | 25 | #include 26 | #include 27 | #include 28 | 29 | #include 30 | 31 | #include "cocaine/format.hpp" 32 | 33 | namespace cocaine { namespace logging { 34 | 35 | std::string 36 | demangle(const std::string& mangled) { 37 | auto deleter = std::bind(&::free, std::placeholders::_1); 38 | auto status = 0; 39 | 40 | std::unique_ptr buffer( 41 | abi::__cxa_demangle(mangled.c_str(), nullptr, nullptr, &status), 42 | deleter 43 | ); 44 | 45 | static const std::map errors = { 46 | { 0, "The demangling operation succeeded." }, 47 | { -1, "A memory allocation failure occurred." }, 48 | { -2, "The mangled name is not a valid name under the C++ ABI mangling rules." }, 49 | { -3, "One of the arguments is invalid." } 50 | }; 51 | 52 | BOOST_ASSERT(errors.count(status)); 53 | 54 | if(status != 0) { 55 | return cocaine::format("unable to demangle '{}': {}", mangled, errors.at(status)); 56 | } 57 | 58 | return buffer.get(); 59 | } 60 | 61 | }} // namespace cocaine::logging 62 | -------------------------------------------------------------------------------- /src/runtime/logging.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2011-2014 Andrey Sibiryov 3 | Copyright (c) 2011-2014 Other contributors as noted in the AUTHORS file. 4 | 5 | This file is part of Cocaine. 6 | 7 | Cocaine is free software; you can redistribute it and/or modify 8 | it under the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | Cocaine is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #include "cocaine/detail/runtime/logging.hpp" 22 | 23 | #include "cocaine/common.hpp" 24 | 25 | #include 26 | #include 27 | #include 28 | #include 29 | 30 | namespace blackhole { 31 | 32 | auto 33 | factory::type() const noexcept -> const char* { 34 | return "console"; 35 | } 36 | 37 | auto 38 | factory::from(const config::node_t&) const -> std::unique_ptr { 39 | return blackhole::builder() 40 | .colorize(cocaine::logging::debug, termcolor_t()) 41 | .colorize(cocaine::logging::info, termcolor_t::blue()) 42 | .colorize(cocaine::logging::warning, termcolor_t::yellow()) 43 | .colorize(cocaine::logging::error, termcolor_t::red()) 44 | .stdout() 45 | .build(); 46 | } 47 | 48 | } // namespace blackhole 49 | -------------------------------------------------------------------------------- /src/runtime/pid_file.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2011-2014 Andrey Sibiryov 3 | Copyright (c) 2011-2014 Other contributors as noted in the AUTHORS file. 4 | 5 | This file is part of Cocaine. 6 | 7 | Cocaine is free software; you can redistribute it and/or modify 8 | it under the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | Cocaine is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #include "cocaine/detail/runtime/pid_file.hpp" 22 | #include "cocaine/errors.hpp" 23 | 24 | #include 25 | 26 | #include 27 | #include 28 | #include 29 | 30 | #include 31 | 32 | using namespace cocaine; 33 | 34 | namespace fs = boost::filesystem; 35 | 36 | pid_file_t::pid_file_t(const fs::path& filepath): 37 | m_filepath(filepath) 38 | { 39 | // If the pidfile exists, check if the process is still active. 40 | if(fs::exists(m_filepath)) { 41 | fs::ifstream stream(m_filepath); 42 | 43 | if(!stream) { 44 | throw cocaine::error_t("unable to read '{}'", m_filepath.string()); 45 | } 46 | 47 | pid_t pid; 48 | 49 | stream >> pid; 50 | 51 | if(::kill(pid, 0) < 0 && errno == ESRCH) { 52 | // Unlink the stale pid file. 53 | remove(); 54 | } else { 55 | throw cocaine::error_t("another process is active"); 56 | } 57 | } 58 | 59 | fs::ofstream stream(m_filepath); 60 | 61 | if(!stream) { 62 | throw cocaine::error_t("unable to write '{}'", m_filepath.string()); 63 | } 64 | 65 | stream << ::getpid(); 66 | stream.close(); 67 | } 68 | 69 | pid_file_t::~pid_file_t() { 70 | try { 71 | fs::remove(m_filepath); 72 | } catch(const fs::filesystem_error& e) { 73 | // Do nothing. 74 | } 75 | } 76 | 77 | void 78 | pid_file_t::remove() { 79 | try { 80 | fs::remove(m_filepath); 81 | } catch(const fs::filesystem_error& e) { 82 | throw cocaine::error_t("unable to remove '{}'", m_filepath.string()); 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /src/runtime/signal.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | 6 | #include "cocaine/idl/context.hpp" 7 | #include "cocaine/signal.hpp" 8 | 9 | namespace cocaine { 10 | 11 | struct propagate_t { 12 | retroactive_signal& hub; 13 | signal::handler_base_t& handler; 14 | std::function callback; 15 | 16 | auto 17 | operator()(const std::error_code& ec, int signum, const siginfo_t& info) -> void { 18 | if(ec == std::errc::operation_canceled) { 19 | return; 20 | } 21 | 22 | if (callback) { 23 | callback(); 24 | } 25 | 26 | hub.invoke(signum, info); 27 | handler.async_wait(signum, *this); 28 | } 29 | }; 30 | 31 | } // namespace cocaine 32 | -------------------------------------------------------------------------------- /src/unicorn/value.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 2015+ Copyright (c) Anton Matveenko 3 | * All rights reserved. 4 | * 5 | * This program is free software; you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation; either version 2 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | */ 15 | 16 | #include "cocaine/unicorn/value.hpp" 17 | 18 | namespace cocaine { namespace unicorn { 19 | 20 | versioned_value_t::versioned_value_t(value_t value, version_t version) : 21 | data{std::move(value), std::move(version)} 22 | {} 23 | 24 | }} 25 | -------------------------------------------------------------------------------- /src/unique_id.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2011-2014 Andrey Sibiryov 3 | Copyright (c) 2011-2014 Other contributors as noted in the AUTHORS file. 4 | 5 | This file is part of Cocaine. 6 | 7 | Cocaine is free software; you can redistribute it and/or modify 8 | it under the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | Cocaine is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #include "cocaine/unique_id.hpp" 22 | 23 | #include "cocaine/errors.hpp" 24 | 25 | #include 26 | 27 | using namespace cocaine; 28 | 29 | unique_id_t::unique_id_t() { 30 | uuid_generate(reinterpret_cast(uuid.data())); 31 | } 32 | 33 | unique_id_t::unique_id_t(const std::string& other) { 34 | const int rv = uuid_parse( 35 | other.c_str(), 36 | reinterpret_cast(uuid.data()) 37 | ); 38 | 39 | if(rv != 0) { 40 | throw cocaine::error_t("unable to parse '{}' as an unique id", other); 41 | } 42 | } 43 | 44 | std::string 45 | unique_id_t::string() const { 46 | // A storage for a 36-character long UUID plus the trailing zero. 47 | char unparsed[37]; 48 | 49 | uuid_unparse_lower( 50 | reinterpret_cast(uuid.data()), 51 | unparsed 52 | ); 53 | 54 | return std::string(unparsed); 55 | } 56 | 57 | bool 58 | unique_id_t::operator==(const unique_id_t& other) const { 59 | return uuid == other.uuid; 60 | } 61 | 62 | namespace cocaine { 63 | 64 | std::ostream& 65 | operator<<(std::ostream& stream, const unique_id_t& id) { 66 | return stream << id.string(); 67 | } 68 | 69 | } // namespace cocaine 70 | -------------------------------------------------------------------------------- /tests/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | SET(PROJECT cocaine-core-tests) 2 | 3 | PROJECT(${PROJECT}) 4 | 5 | # Benchmarks 6 | IF(COCAINE_ALLOW_BENCHMARKS) 7 | INCLUDE_DIRECTORIES( 8 | foreign/celero/include) 9 | 10 | ADD_SUBDIRECTORY( 11 | foreign/celero) 12 | 13 | UNSET(CELERO_RUN_EXAMPLE_ON_BUILD) 14 | UNSET(CELERO_CELERO_ENABLE_TESTS) 15 | UNSET(CELERO_COMPILE_DYNAMIC_LIBRARIES) 16 | 17 | ADD_EXECUTABLE(cocaine-benchmark 18 | benchmark.cpp) 19 | 20 | TARGET_LINK_LIBRARIES(cocaine-benchmark 21 | celero 22 | cocaine-core) 23 | 24 | SET_TARGET_PROPERTIES(cocaine-benchmark PROPERTIES 25 | COMPILE_FLAGS "-std=c++0x -W -Wall -Werror -pedantic") 26 | ENDIF() 27 | 28 | # Unit tests 29 | IF(COCAINE_ALLOW_TESTS AND 30 | NOT (CMAKE_VERSION VERSION_LESS 2.8.5 OR (DEFINED GCC_COMPILER_VERSION AND GCC_COMPILER_VERSION VERSION_LESS 4.8))) 31 | 32 | INCLUDE(DownloadGoogleTesting) 33 | 34 | ADD_SUBDIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/foreign/.googletest EXCLUDE_FROM_ALL) 35 | 36 | ENABLE_TESTING() 37 | download_google_testing() 38 | 39 | INCLUDE_DIRECTORIES(SYSTEM foreign/.googletest/googletest/include) 40 | 41 | INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/../include) 42 | 43 | ADD_EXECUTABLE(cocaine-core-tests 44 | unit/context.cpp 45 | unit/format.cpp 46 | unit/protocol.cpp 47 | unit/header.cpp 48 | unit/header_table.cpp 49 | unit/lexical_cast.cpp 50 | unit/uuid.cpp) 51 | 52 | TARGET_LINK_LIBRARIES(cocaine-core-tests 53 | ${CMAKE_THREAD_LIBS_INIT} 54 | cocaine-core 55 | cocaine-io-util 56 | gmock 57 | gtest 58 | gtest_main) 59 | 60 | SET_TARGET_PROPERTIES(cocaine-core-tests PROPERTIES 61 | COMPILE_FLAGS "-std=c++0x -W -Wall -Werror -pedantic") 62 | ENDIF() 63 | -------------------------------------------------------------------------------- /tests/ci/travis.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh -ex 2 | 3 | # Install build-depends 4 | yes | sudo mk-build-deps -i 5 | 6 | # Build packages 7 | yes | debuild -e CC -e CXX --prepend-path="/usr/local/bin/" -uc -us 8 | 9 | # TODO: Install packages 10 | # TODO: Run tests 11 | -------------------------------------------------------------------------------- /tests/unit/context.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include <../src/context/distributor.hpp> 5 | 6 | namespace cocaine { 7 | namespace { 8 | 9 | 10 | struct engine_mock_t { 11 | double u; 12 | 13 | engine_mock_t(double u) : u(u) {} 14 | 15 | auto 16 | utilization() const -> double { 17 | return u; 18 | } 19 | 20 | auto operator->() const -> const engine_mock_t* { 21 | return this; 22 | } 23 | 24 | auto operator* () const -> const engine_mock_t& { 25 | return *this; 26 | } 27 | 28 | auto operator* () -> engine_mock_t& { 29 | return *this; 30 | } 31 | }; 32 | 33 | TEST(context, bucket_random_dispatcher) { 34 | auto d = make_distributor>("bucket_random", dynamic_t::object_t()); 35 | std::vector values { 36 | 0.005, 0.002, 0.001, 0.15 37 | }; 38 | std::vector sample(values.begin(), values.end()); 39 | std::map counter; 40 | for(size_t i = 0; i < 3000; i++) { 41 | counter[d->next(sample)->utilization()]++; 42 | } 43 | ASSERT_NEAR(counter[values[0]], 1000, 50); 44 | ASSERT_NEAR(counter[values[1]], 1000, 50); 45 | ASSERT_NEAR(counter[values[2]], 1000, 50); 46 | ASSERT_EQ(counter[values[3]], 0); 47 | 48 | // One value with lowest value 49 | values[0] = 0.1; 50 | values[1] = 0.21; 51 | values[2] = 0.22; 52 | values[3] = 0.23; 53 | sample = std::vector(values.begin(), values.end()); 54 | counter.clear(); 55 | for(size_t i = 0; i < 3000; i++) { 56 | counter[d->next(sample)->utilization()]++; 57 | } 58 | ASSERT_EQ(counter[values[0]], 3000); 59 | 60 | // Extend bucket size, use the same sample 61 | counter.clear(); 62 | dynamic_t::object_t args; 63 | args["bucket_size"] = 0.3; 64 | d = make_distributor>("bucket_random", args); 65 | for(size_t i = 0; i < 3000; i++) { 66 | counter[d->next(sample)->utilization()]++; 67 | } 68 | EXPECT_NEAR(counter[values[0]], 750, 50); 69 | EXPECT_NEAR(counter[values[1]], 750, 50); 70 | EXPECT_NEAR(counter[values[2]], 750, 50); 71 | EXPECT_NEAR(counter[values[3]], 750, 50); 72 | 73 | // Check empty pool case 74 | sample.clear(); 75 | ASSERT_THROW(d->next(sample), cocaine::error_t); 76 | 77 | // check misconfiguration case 78 | args["bucket_size"] = 0.0; 79 | EXPECT_THROW(make_distributor>("bucket_random", args), cocaine::error_t); 80 | args["bucket_size"] = 2.0; 81 | EXPECT_THROW(make_distributor>("bucket_random", args), cocaine::error_t); 82 | 83 | } 84 | 85 | } // namespace 86 | } // namespace cocaine 87 | -------------------------------------------------------------------------------- /tests/unit/header.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2011-2015 Anton Matveenko 3 | Copyright (c) 2011-2015 Other contributors as noted in the AUTHORS file. 4 | 5 | This file is part of Cocaine. 6 | 7 | Cocaine is free software; you can redistribute it and/or modify 8 | it under the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | Cocaine is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #include 22 | #include 23 | 24 | #include 25 | #include 26 | 27 | #include 28 | 29 | using namespace cocaine::hpack; 30 | 31 | TEST(header_data_t, conversion) { 32 | size_t val = 42; 33 | auto data = header::pack(val); 34 | ASSERT_TRUE(data.size() == sizeof(val)); 35 | ASSERT_EQ(header::unpack(data), val); 36 | ASSERT_ANY_THROW(header::unpack(data)); 37 | int test2 = 42; 38 | ASSERT_EQ(header::unpack(header::pack(test2)), test2); 39 | 40 | char test3 = 42; 41 | ASSERT_EQ(header::unpack(header::pack(test3)), test3); 42 | } 43 | 44 | TEST(header_t, general) { 45 | header_t empty; 46 | ASSERT_EQ(empty.name(), ""); 47 | ASSERT_EQ(empty.value(), ""); 48 | header_t span1 = header_t::create>(); 49 | ASSERT_EQ(span1.name(), "span_id"); 50 | header_t span2 = header_t::create>(); 51 | header_t span3(span2); 52 | header_t span4; 53 | span4 = span3; 54 | ASSERT_EQ(span1, span2); 55 | ASSERT_EQ(span2, span3); 56 | ASSERT_EQ(span3, span4); 57 | } 58 | 59 | -------------------------------------------------------------------------------- /tests/unit/lexical_cast.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2011-2015 Anton Matveenko 3 | Copyright (c) 2011-2015 Other contributors as noted in the AUTHORS file. 4 | 5 | This file is part of Cocaine. 6 | 7 | Cocaine is free software; you can redistribute it and/or modify 8 | it under the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | Cocaine is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #include 22 | #include 23 | 24 | #include 25 | 26 | namespace { 27 | 28 | TEST(boost, lexical_cast) { 29 | ASSERT_TRUE(boost::lexical_cast(std::string("1"))); 30 | EXPECT_ANY_THROW(boost::lexical_cast(std::string("true"))); 31 | EXPECT_ANY_THROW(boost::lexical_cast(std::string("TRUE"))); 32 | EXPECT_ANY_THROW(boost::lexical_cast(std::string("True"))); 33 | EXPECT_ANY_THROW(boost::lexical_cast(std::string("42"))); 34 | ASSERT_FALSE(boost::lexical_cast(std::string("0"))); 35 | EXPECT_ANY_THROW(boost::lexical_cast(std::string(""))); 36 | EXPECT_ANY_THROW(boost::lexical_cast(std::string("FALSE"))); 37 | EXPECT_ANY_THROW(boost::lexical_cast(std::string("False"))); 38 | EXPECT_ANY_THROW(boost::lexical_cast(std::string("false"))); 39 | } 40 | 41 | } // namespace 42 | -------------------------------------------------------------------------------- /tests/unit/protocol.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | 5 | namespace cocaine { 6 | 7 | static_assert( 8 | std::is_same< 9 | io::messages::type, 10 | boost::mpl::list< 11 | io::storage::read, 12 | io::storage::write, 13 | io::storage::remove, 14 | io::storage::find 15 | >::type 16 | >::value, 17 | "`io::messages::type` is broken"); 18 | 19 | static_assert( 20 | boost::mpl::equal< 21 | io::messages::full::type, 22 | boost::mpl::list< 23 | io::storage::read, 24 | io::storage::write, 25 | io::storage::remove, 26 | io::storage::find, 27 | io::control::goaway, 28 | io::control::ping, 29 | io::control::settings, 30 | io::control::revoke 31 | >::type 32 | >::value, 33 | "`io::messages::full` is broken"); 34 | 35 | } // namespace cocaine 36 | -------------------------------------------------------------------------------- /tests/unit/test_idl.hpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | namespace cocaine { 6 | namespace io { 7 | 8 | struct test_tag; 9 | struct test_transition_tag; 10 | 11 | struct test { 12 | struct method1 { 13 | typedef test_tag tag; 14 | static const char* alias() { return "method1"; } 15 | typedef boost::mpl::list<>::type argument_type; 16 | typedef void upstream_type; 17 | }; 18 | 19 | struct method2 { 20 | typedef test_tag tag; 21 | static const char* alias() { return "method2"; } 22 | typedef boost::mpl::list<>::type argument_type; 23 | typedef test_transition_tag dispatch_type; 24 | typedef test_transition_tag upstream_type; 25 | }; 26 | }; 27 | 28 | struct inner_test { 29 | struct inner_method1 { 30 | typedef test_transition_tag tag; 31 | static const char* alias() { return "inner_method1"; } 32 | typedef boost::mpl::list<>::type argument_type; 33 | typedef void upstream_type; 34 | }; 35 | }; 36 | 37 | template<> 38 | struct protocol { 39 | typedef boost::mpl::int_<1>::type version; 40 | typedef boost::mpl::list::type messages; 41 | typedef test scope; 42 | }; 43 | 44 | template<> 45 | struct protocol { 46 | typedef boost::mpl::int_<1>::type version; 47 | typedef boost::mpl::list::type messages; 48 | typedef test_transition_tag transition_type; 49 | typedef inner_test scope; 50 | }; 51 | 52 | } // namespace io 53 | } // namespace cocaine 54 | -------------------------------------------------------------------------------- /tests/unit/uuid.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | 5 | namespace cocaine { 6 | namespace { 7 | 8 | TEST(uuid_t, operator_eq) { 9 | auto value = "550e8400-e29b-41d4-a716-446655440000"; 10 | 11 | unique_id_t uuid(value); 12 | unique_id_t other(value); 13 | 14 | EXPECT_EQ(other, uuid); 15 | } 16 | 17 | } // namespace 18 | } // namespace cocaine 19 | --------------------------------------------------------------------------------