Skip to content

Instantly share code, notes, and snippets.

@tmbdev
Created April 18, 2019 17:49
Show Gist options
  • Save tmbdev/b83e4f4ba99f2025ee4e08ca9ea410c3 to your computer and use it in GitHub Desktop.
Save tmbdev/b83e4f4ba99f2025ee4e08ca9ea410c3 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"ii libmsgpack-dev 2.1.5-1 amd64 binary-based efficient object serialization library\n",
"ii libmsgpackc2:amd64 2.1.5-1 amd64 binary-based efficient object serialization library\n"
]
}
],
"source": [
"!dpkg -l | grep msg"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Includes"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"#include <stdio.h>\n",
"#include <unistd.h>\n",
"#include <sys/types.h>\n",
"#include <sys/stat.h>\n",
"#include <fcntl.h>\n",
"#include <stdio.h>\n",
"#include <stdlib.h>\n",
"#include <string.h>\n",
"#include <unistd.h>"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"#include <iostream>\n",
"#include <cstdlib>\n",
"#include <string>"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"using namespace std;"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# ZMQ"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Reading package lists...\n",
"Building dependency tree...\n",
"Reading state information...\n",
"libzmqpp-dev is already the newest version (4.1.2-0ubuntu2).\n",
"0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.\n"
]
}
],
"source": [
"!sudo apt-get install libzmqpp-dev"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"#include <string>\n",
"#include <iostream>\n",
"#include <zmqpp/zmqpp.hpp>\n",
"#pragma cling load(\"libzmq.so\")\n",
"#pragma cling load(\"libzmqpp.so\")\n",
"using namespace std;\n",
"\n",
"zmqpp::context context;"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"zmqpp::socket server(context, zmqpp::socket_type::push);\n",
"server.connect(\"tcp://localhost:4242\");\n",
"zmqpp::message message;\n",
"message << \"hello world\";\n",
"server.send(message);"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"hello world\n"
]
}
],
"source": [
"zmqpp::socket client(context, zmqpp::socket_type::pull);\n",
"client.bind(\"tcp://*:4242\");\n",
"zmqpp::message msg;\n",
"client.receive(msg);\n",
"string text;\n",
"msg >> text;\n",
"cout << text << endl;"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# MsgPack"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"API: https://github.com/msgpack/msgpack-c/blob/master/QUICKSTART-CPP.md\n",
"https://github.com/msgpack/msgpack-c/wiki/v1_1_cpp_unpacker"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Reading package lists...\n",
"Building dependency tree...\n",
"Reading state information...\n",
"libmsgpack-dev is already the newest version (2.1.5-1).\n",
"0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.\n"
]
}
],
"source": [
"!sudo apt-get install -y libmsgpack-dev"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"#include <msgpack.hpp>\n",
"#include <map>\n",
"#include <vector>\n",
"//#pragma cling load(\"/usr/lib/x86_64-linux-gnu/libmsgpackc.so\")\n",
"#pragma cling load(\"libmsgpackc.so\")\n",
"using namespace std;"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Object Interface"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"ii libmsgpack-dev 2.1.5-1 amd64 binary-based efficient object serialization library\n",
"ii libmsgpackc2:amd64 2.1.5-1 amd64 binary-based efficient object serialization library\n"
]
}
],
"source": [
"!dpkg -l | grep msgpack"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"msgpack::object obj;"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"map<string, string> q;"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"q[\"x\"] = \"a\";\n",
"q[\"y\"] = \"b\";\n",
"q[\"z\"] = \"c\";"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
"#include <any>\n",
"std::map<string, any> qm;\n",
"qm[\"x\"] = 3;\n",
"qm[\"y\"] = \"z\";"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
"msgpack::sbuffer sbuf;\n",
"msgpack::pack(sbuf, q);"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{\"x\":\"a\", \"y\":\"b\", \"z\":\"c\"}\n"
]
}
],
"source": [
"msgpack::object_handle oh = msgpack::unpack(sbuf.data(), sbuf.size());\n",
"msgpack::object obj2 = oh.get();\n",
"cout << obj2 << endl;"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\"c\""
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"map<string, string> q2;\n",
"obj2.convert(q2);\n",
"q2[\"z\"]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Streaming Interface"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [],
"source": [
"msgpack::sbuffer buf;\n",
"msgpack::packer<msgpack::sbuffer> pk(&buf);\n",
"pk.pack_map(3);\n",
"pk.pack(\"1\"); pk.pack(\"a\");\n",
"pk.pack(\"2\"); pk.pack(\"b\");\n",
"pk.pack(\"3\"); pk.pack(\"c\");"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [],
"source": [
"msgpack::unpacker pac;\n",
"pac.reserve_buffer(buf.size());\n",
"memcpy(pac.buffer(), buf.data(), buf.size());\n",
"pac.buffer_consumed(buf.size());"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{\"1\":\"a\", \"2\":\"b\", \"3\":\"c\"}\n"
]
}
],
"source": [
"msgpack::object_handle oh2;\n",
"while(pac.next(oh2)) {\n",
" std::cout << oh2.get() << std::endl;\n",
"}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Dynamically Typed Interface"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [],
"source": [
"#include <msgpack.hpp>\n",
"#include <map>\n",
"#include <vector>\n",
"#include <any>\n",
"#include <iostream>\n",
"#pragma cling load(\"libmsgpackc.so\")\n",
"using namespace std;"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [],
"source": [
"// here are some things you can do with an any type\n",
"// note that some of these involve copies and might require better implementations later\n",
"\n",
"vector<any> vq;\n",
"vq.emplace_back(99);\n",
"any_cast<int>(vq.back());\n",
"vq.emplace_back(vector<any>());\n",
"any_cast<vector<any>>(vq.back()).emplace_back(3);\n",
"vector<any> top(any_cast<vector<any>>(vq.back()));\n",
"vq.pop_back();"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [],
"source": [
"using namespace std;\n",
"struct any_visitor : msgpack::v2::null_visitor {\n",
" any_visitor() {}\n",
" \n",
" vector<any> stack;\n",
" \n",
" void clear() {\n",
" stack.resize(0);\n",
" }\n",
"\n",
" bool visit_nil() {\n",
" stack.emplace_back((void*)0);\n",
" return true;\n",
" }\n",
" bool visit_boolean(bool v) {\n",
" stack.emplace_back(v);\n",
" return true;\n",
" }\n",
" bool visit_positive_integer(uint64_t v) {\n",
" stack.emplace_back(int(v));\n",
" return true;\n",
" }\n",
" bool visit_negative_integer(int64_t v) {\n",
" stack.emplace_back(int(v));\n",
" return true;\n",
" }\n",
" bool visit_str(const char* v, uint32_t size) {\n",
" stack.emplace_back(string(v, size));\n",
" return true;\n",
" }\n",
" bool start_array(uint32_t /*num_elements*/) {\n",
" stack.emplace_back(vector<any>());\n",
" return true;\n",
" }\n",
" bool end_array_item() {\n",
" int n = stack.size();\n",
" any_cast<vector<any>&>(stack[n-2]).emplace_back(stack[n-1]);\n",
" stack.pop_back();\n",
" return true;\n",
" }\n",
" bool end_array() {\n",
" return true;\n",
" }\n",
" bool start_map(uint32_t /*num_kv_pairs*/) {\n",
" stack.emplace_back(map<string, any>());\n",
" return true;\n",
" }\n",
" bool end_map_key() {\n",
" return true;\n",
" }\n",
" bool end_map_value() {\n",
" int n = stack.size();\n",
" cerr << any_cast<string>(stack[n-2]) << \":\" << stack[n-1].type().name() << endl;\n",
" any_cast<map<string, any>&>(stack[n-3])[any_cast<string>(stack[n-2])] = stack[n-1];\n",
" stack.pop_back();\n",
" stack.pop_back();\n",
" return true;\n",
" }\n",
" bool end_map() {\n",
" cerr << \"map:\" << any_cast<map<string, any>>(stack.back()).size() << endl;\n",
" return true;\n",
" }\n",
" void parse_error(size_t /*parsed_offset*/, size_t /*error_offset*/) {\n",
" std::cerr << \"parse error\"<<std::endl;\n",
" }\n",
" void insufficient_bytes(size_t /*parsed_offset*/, size_t /*error_offset*/) {\n",
" std::cout << \"insufficient bytes\"<<std::endl; \n",
" }\n",
"};"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [],
"source": [
"msgpack::sbuffer buf2;\n",
"msgpack::packer<msgpack::sbuffer> pk2(&buf2);\n",
"pk2.pack_map(3);\n",
"pk2.pack(\"1\"); pk2.pack(\"a\");\n",
"pk2.pack(\"2\"); pk2.pack(\"b\");\n",
"pk2.pack(\"3\"); pk2.pack(\"c\");"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"1:NSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE\n",
"2:NSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE\n",
"3:NSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE\n",
"map:3\n"
]
}
],
"source": [
"any_visitor v;\n",
"size_t offset;\n",
"msgpack::v2::parse(buf2.data(), buf2.size(), offset, v);"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\"St3mapINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESt3anySt4lessIS5_ESaISt4pairIKS5_S6_EEE\""
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"v.stack[0].type().name()"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\"a\""
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"any_cast<string>(any_cast<map<string,any>>(v.stack[0])[\"1\"])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# CURL"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Reading package lists...\n",
"Building dependency tree...\n",
"Reading state information...\n",
"libcurl4-openssl-dev is already the newest version (7.58.0-2ubuntu3.2).\n",
"0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.\n"
]
}
],
"source": [
"!sudo apt-get install libcurl4-openssl-dev"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [],
"source": [
"#include <curl/curl.h>\n",
"#pragma cling load(\"libcurl.so\")"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [],
"source": [
"size_t write_data(void *buffer, size_t size, size_t nmemb, void *userp) {\n",
" cout << \"[\" << size << \"] \" << string((char *)buffer, size*nmemb) << endl;\n",
" return size;\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [],
"source": [
"CURL *handle = 0;\n",
"CURLcode res;"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1] <!doctype html>\n",
"<html>\n",
"<head>\n",
" <title>Example Domain</title>\n",
"\n",
" <meta charset=\"utf-8\" />\n",
" <meta http-equiv=\"Content-type\" content=\"text/html; charset=utf-8\" />\n",
" <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\" />\n",
" <style type=\"text/css\">\n",
" body {\n",
" background-color: #f0f0f2;\n",
" margin: 0;\n",
" padding: 0;\n",
" font-family: \"Open Sans\", \"Helvetica Neue\", Helvetica, Arial, sans-serif;\n",
" \n",
" }\n",
" div {\n",
" width: 600px;\n",
" margin: 5em auto;\n",
" padding: 50px;\n",
" background-color: #fff;\n",
" border-radius: 1em;\n",
" }\n",
" a:link, a:visited {\n",
" color: #38488f;\n",
" text-decoration: none;\n",
" }\n",
" @media (max-width: 700px) {\n",
" body {\n",
" background-color: #fff;\n",
" }\n",
" div {\n",
" width: auto;\n",
" margin: 0 auto;\n",
" border-radius: 0;\n",
" padding: 1em;\n",
" }\n",
" }\n",
" </style> \n",
"</head>\n",
"\n",
"<body>\n",
"<div>\n",
" <h1>Example Domain</h1>\n",
" <p>This domain is established to be used for illustrative examples in documents. You may use this\n",
" domain in examples without prior co\n"
]
}
],
"source": [
"handle = curl_easy_init();\n",
"curl_easy_setopt(handle, CURLOPT_URL, \"http://example.com\");\n",
"curl_easy_setopt(handle, CURLOPT_FOLLOWLOCATION, 1L);\n",
"curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, write_data);\n",
"//curl_easy_setopt(easyhandle, CURLOPT_WRITEDATA, &internal_struct);\n",
"curl_easy_perform(handle);"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The above needs to be hooked up somehow to the libarchive library below using `archive_read_open` and appropriate callbacks. For exploring the APIs further, we're just going to use popen"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Archive"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Good introductory examples here: https://github.com/libarchive/libarchive/wiki/Examples"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" archive_read_open(a, mydata, NULL, myread, myclose);\n",
"\n",
" ssize_t\n",
" myread(struct archive *a, void *client_data, const void **buff)\n",
" {\n",
" struct mydata *mydata = client_data;\n",
" *buff = mydata->buff;\n",
" return (read(mydata->fd, mydata->buff, 10240));\n",
" }\n",
" int\n",
" myclose(struct archive *a, void *client_data)\n",
" {\n",
" struct mydata *mydata = client_data;\n",
" if (mydata->fd > 0)\n",
" close(mydata->fd);\n",
" free(mydata);\n",
" return (ARCHIVE_OK);\n",
" }\n"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Reading package lists...\n",
"Building dependency tree...\n",
"Reading state information...\n",
"libarchive-dev is already the newest version (3.2.2-3.1ubuntu0.1).\n",
"0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.\n"
]
}
],
"source": [
"!sudo apt-get install libarchive-dev"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [],
"source": [
"#include <archive.h>\n",
"#include <archive_entry.h>\n",
"#pragma cling load(\"libarchive.so\")"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [],
"source": [
"#include <stdio.h>\n",
"FILE *stream = 0;"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [],
"source": [
"stream = popen(\"curl -s http://storage.googleapis.com/lpr-ocr/uw3-lines.tgz\", \"r\");"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {},
"outputs": [],
"source": [
"struct archive *a;\n",
"struct archive *ext;\n",
"struct archive_entry *entry;\n",
"int flags;\n",
"int r;"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {},
"outputs": [],
"source": [
"a = archive_read_new();\n",
"archive_read_support_format_all(a);\n",
"archive_read_support_filter_all(a);"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 40,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"archive_read_open_FILE(a, stream); // really: use archive_read_open with ZMQ input functions"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {},
"outputs": [],
"source": [
"r = archive_read_next_header(a, &entry);\n",
"if (r != ARCHIVE_OK)\n",
" ;"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\"A001BIN.lines.png\""
]
},
"execution_count": 42,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"archive_entry_pathname(entry)"
]
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"512\n",
"512\n",
"512\n",
"512\n",
"512\n",
"512\n",
"512\n",
"512\n",
"512\n",
"512\n",
"512\n",
"512\n",
"512\n",
"512\n",
"512\n",
"512\n",
"512\n",
"512\n",
"512\n",
"512\n",
"512\n",
"233\n"
]
}
],
"source": [
"char buff[512];\n",
"for (;;) {\n",
" int size = archive_read_data(a, buff, 512);\n",
" if (size < 0) throw \"error\";\n",
" if (size == 0) break;\n",
" cout << size << endl;\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "C++17",
"language": "C++17",
"name": "xeus-cling-cpp17"
},
"language_info": {
"codemirror_mode": "text/x-c++src",
"file_extension": ".cpp",
"mimetype": "text/x-c++src",
"name": "c++",
"version": "-std=c++17"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment