Session #4 C++ REST SDK 2.0 (Codename : Casablanca) 2014 년 3 월 C++ 클라우드환경을준비하다
Motivation : 재사용을통한생산성향상 기계어 어셈블리어 C 언어 C++ 언어 COM,DCOM 실행되는언어 읽을수있는언어 함수재사용 클래스재사용 바이너리재사용 2
Motivation : 원격지기능사용 3
Motivation : 미들웨어 http://middleware.smile.fr/concepts-des-moms-et-jms/qu-est-ce-qu-un-middleware 4
Motivation : WebService http://msdn.microsoft.com/ko-kr/library/azure/jj554228.asp 5
Motivation : 도메인지향데이터전송 http://www.programmableweb.com/news/3000-web-apis-trends-quickly-growing-directory/2011/03/08 http://superwebapps.dk/ 6
라이브러리선택 http://realtimelogic.com/products/ajax/ http://en.wikipedia.org/wiki/cloud_computing#mediaviewer/file:cloud_computing.svg 7
C++ REST SDK 특징 서버애플리케이션지원 IIS 호스팅 /Azure/stand-alone 방식의서버애플리케이션구현가능 클라이언트애플리케이션지원 통신라이브러리, 파서, 직렬화등필수적인라이브러리셋의모음 오픈소스 http://casablanca.codeplex.com 크로스플랫폼 ( 윈도우, 리눅스, OS X, ios, 윈폰, WinRT ) 모던 C++ 최신트렌드에맞는스타일의코드작성가능 간결하고유지보수하기쉬우며생산성높은코드작성가능 비주얼스튜디오의지원 8
C++ REST SDK 9
NuGet 라이브러리패키지관리 오픈소스라이브러리제공, 형상관리, 커뮤니티등토털에코시스템 15000 여개프로젝트, 11 만 6 천여개프로젝트패키지관리 하루평균약 23 만건정도의프로젝트다운로드 10
NuGet 호스팅 Nexus : http://www.sonatype.com/nexus/nexus-pro-features Artifactory : http://www.jfrog.com/home/v_artifactorypro_features Inedo ProGet : http://inedo.com/proget/overview MyGET : http://www.myget.org 11
NuGet 을이용한로컬패키지관리 12
13 http://www.cafex.com/wp-content/uploads/2012/09/demo_banner.png
C++ REST SDK 에포함된라이브러리 Task Programming Utility & Helper JSON Parser HTTP Listener Asynchronous Streams HTTP Client 14
Architecture Azure / IIS Host Windows 7 Host Binary Serializers Apps & Libraries Timers Bing Maps JSON Parser & Writer Azure Storage Windows Live Xbox Live Async File I/O HTTP Client & Listener TCP Client & Listener Web Sockets Client & Listener Possible 3 rd Party Service Host WinHTTP PPL Boost.Asio IOCP HTTP.sys Casablanca Lib Windows XP, Vista, 7, 8.x / Windows Phone 8.x / Linux / Mac OS X / ios Foundation 15
Task 클래스와 Continuations 패턴 task<t> taskobject_a([](){slow_operation_a()}); taskobject_a.then([](t i) { return slow_operation_b(i); }); bool isdone = taskobject_a.is_done(); taskobject_a.wait(); T A_return_value = taskobject_a.get(); http://msdn.microsoft.com/en-us/library/windows/apps/jj160321.aspx 16
Join 패턴, Choice 패턴구현 array<task<int>, 3> tasks = { create_task([]() -> int { return long_ret_1(); }), create_task([]() -> int { return middle_ret_2(); }), create_task([]() -> int { return short_ret_3(); }) }; when_any(begin(tasks), end(tasks)).then([](pair<int, size_t> result){ // 결과값과인덱스반환. [3, 2] }).wait(); when_all(begin(tasks), end(tasks)).then([](vector<int> results) { // 결과값반환. [1, 2, 3] }).wait(); 17
문자열처리 플랫폼마다다른문자열방식지원 utility::string_t, size64_t, char_t, istrinm_t, URI(Uniform Resource Identifiers) protocol : // server [: port] / path? query # fragment http://search.cpprestsdk.com:8080/search/total.jsp?id=1234&comment=rest#cosmos <->base64, <->utf8, <->utf16 18
URI 다루기 uri_builder b; b.set_scheme(u("http")); b.set_host(u("search.naver.com")); b.set_port(80); b.set_path(u("search.naver")); b.set_query(uri::encode_uri(u("query=a 가 1 나 "))); // http://search.naver.com:80/search.naver?query=a%ea%b0%801%eb%82%98 vector<string_t> paths = uri::split_path(u.to_string()); map<string_t, string_t> querys = uri::split_query(u.to_string()); 19
문자열변환관련유틸리티함수군 namespace Utility { namespace conversions { std::string cdecl utf16_to_utf8(const utf16string &w); utf16string cdecl utf8_to_utf16(const std::string &s); utf16string cdecl usascii_to_utf16(const std::string &s); utf16string cdecl latin1_to_utf16(const std::string &s); utf16string cdecl default_code_page_to_utf16(const std::string &s); utility::string_t cdecl to_string_t(std::string &&s); utility::string_t cdecl to_string_t(utf16string &&s); utility::string_t cdecl to_string_t(const std::string &s); utility::string_t cdecl to_string_t(const utf16string &s); utf16string cdecl to_utf16string(const std::string &value); utf16string cdecl to_utf16string(utf16string value); std::string cdecl to_utf8string(std::string value); std::string cdecl to_utf8string(const utf16string &value); utility::string_t cdecl to_base64(const std::vector<unsigned char>& data); utility::string_t cdecl to_base64(uint64_t data); std::vector<unsigned char> cdecl from_base64(const utility::string_t& str); template <typename Source> utility::string_t print_string(const Source &val) template <typename Target> Target scan_string(const utility::string_t &str) } } 20
http_listener var http = require('http'); http.createserver(function (request, response) { response.writehead(200, {'Content-Type': 'text/plain'}); response.end('hello World!'); }).listen(8080, '127.0.0.1'); task_status server() { http_listener listener(u("http://localhost:8080/test")); listener.support([&listener](http_request request)->void{ request.reply(status_codes::ok, L"Hello World"); }); listener.open().then([](){while (1) Sleep(10000);}).wait(); listener.close().wait(); } 21
http_client task_status foo() { http_client client(u("http://se.naver.com/index.html")); return client.request(methods::get).then([](http_response response) { std::wcout << response.extract_string().get() << std::endl; }).wait(); } int _tmain(int argc, _TCHAR* argv []) { foo(); return 0; } 22
23 http://www.cafex.com/wp-content/uploads/2012/09/demo_banner.png
JSON web::json::value v0 = web::json::value::null(); web::json::value v1 = web::json::value::number(17); web::json::value v2 = web::json::value::number(3.1415); web::json::value v3 = web::json::value::boolean(true); web::json::value v4 = web::json::value::string(u("hello Again!")); web::json::value v5 = web::json::value::object(); web::json::value v6 = web::json::value::array(); 24
field_map 을이용한 JSON 셋 web::json::value::field_map f; f.push_back(std::pair<web::json::value, web::json::value> (web::json::value::string(u("name1")), web::json::value::number(1))); f.push_back(std::pair<web::json::value, web::json::value> (web::json::value::string(u("name2")), web::json::value::string(u("hello")))); f.push_back(std::pair<web::json::value, web::json::value> (web::json::value::string(u("name3")), web::json::value::boolean(u("true")))); f.push_back(std::pair<web::json::value, web::json::value> (web::json::value::string(u("name4")), web::json::value::null())); web::json::value obj = web::json::value::object(f); std::wcout << obj.to_string() << std::endl; 출력값 : { "name1" : 1, "name2" : "hello", "name3" : true, "name4" : null } 25
object 를직접다루어 JSON 셋팅 web::json::value obj = web::json::value::object(); obj[u("name1")] = web::json::value::string(u("hi there")); obj[u("name2")] = web::json::value::number(77); std::wcout << obj.to_string() << std::endl; 출력값 : { "name1" : "hi there", "name2" : 77 } 26
as_* 함수를이용하여값조회 web::json::value v1 = web::json::value::number(17); web::json::value v2 = web::json::value::number(3.1415); web::json::value v3 = web::json::value::boolean(true); web::json::value v4 = web::json::value::string(u("hello Again!")); //... int32_t av1 = v1.as_integer(); double av2 = v2.as_double(); bool av3 = v3.as_bool(); utility::string_t av4 = v4.as_string(); 27
JSON 문자열파싱 web::json::value obj = web::json::value::object(); obj[u("name1")] = web::json::value::string(u("hi there")); obj[u("name2")] = web::json::value::number(77); utility::string_t tmp = obj[u("name1")].as_string(); for(web::json::value::const_iterator iter = obj.as_object().begin() ; iter!= obj.as_object().end(); ++iter) { std::wcout << iter->first.to_string() << L" : " << iter->second.to_string() << std::endl; } 출력값 : "name1" : "hi there" "name2" : 77 28
29 http://www.cafex.com/wp-content/uploads/2012/09/demo_banner.png
Reference C++ REST SDK (codename Casablanca ) https://casablanca.codeplex.com/ REST application programming http://www.ibm.com/developerworks/aix/library/auaem_rest/#resources Connecting C++ apps to the cloud via Casablanca Niklas Gustafsson & Artur Laksberg //build/ 3-003 30