ãZeroMQãZeroMQã䜿çšããŠãããŸããŸãªã¡ãã»ãŒãžãã¿ãŒã³ãé©çšããæ¹æ³ãåŠã³ãŸãããããšããæ¬ã®ç¡æ翻蚳ãå§ããããšæããŸãã å€ãã®äººããã®èå³æ·±ãã©ã€ãã©ãªã«å¯ŸåŠããããšæãã§ãããã
å 容
- 第1ç« ïŒã¯ããã«
- 第2ç« ïŒãœã±ããã®çŽ¹ä»
- 第3ç« ïŒãœã±ããããããžã®äœ¿çš
- 第4ç« ïŒè¿œå ã®ãã¿ãŒã³
ZeroMQãžããããïŒ ãã®ç« ã¯ZeroMQã®æŠèŠã§ãããèªè ã«ã¡ãã»ãŒãžãã¥ãŒã·ã¹ãã ãšã¯äœã§ãããããããŠæãéèŠãªããšãšããŠZeroMQãšã¯äœãã«ã€ããŠã®äžè¬çãªèããæäŸããŸãã ãã®ç« ã§ã¯ã次ã®ãããã¯ã«ã€ããŠèª¬æããŸãã
- ã¡ãã»ãŒãžãã¥ãŒãæ§æãããã®ã®æŠèŠ
- ZeroMQã䜿çšããçç±ãšä»ã®ã¡ãã»ãŒãžãã¥ãŒã€ã³ã°ãã¯ãããžãŒãšã®éã
- ã¯ã©ã€ã¢ã³ã/ãµãŒããŒã¢ãŒããã¯ãã£ã®åºæ¬
- æåã®ãã¿ãŒã³ãèæ ®ããŠãã ããïŒèŠæ±-å¿ç
- Cã§æååãåŠçããæ¹æ³
- ã€ã³ã¹ããŒã«ãããŠããZeroMQããŒãžã§ã³ã®ç¢ºèª
éå§ãã
ç涯ãéããŠäººã ã¯ç€ŸäŒçã«çžäºäœçšããŸãã ããã°ã©ã ã¯ãã®ããã«åäœããŸãã ç§ãã¡ã¯ã€ã³ã¿ãŒãããã®äžçã«äœãã§ããã®ã§ãããã°ã©ã ã¯å¥ã®ããã°ã©ã ãšéä¿¡ããå¿ èŠããããŸãã UDPãTCPãHTTPãIPXãWebSocketãããã³ã¢ããªã±ãŒã·ã§ã³ãæ¥ç¶ããããã®ä»ã®ãããã³ã«ããããŸãã
ããã«ããããããããã®ãããªäœã¬ãã«ã®ã¢ãããŒãã¯ç§ãã¡ã®ç掻ãè€éã«ããŸã;ç§ãã¡ã¯ãã£ãšã·ã³ãã«ã§ããéãäœããå¿ èŠãšããŸãã é«ã¬ãã«ã®æœè±¡åã¯é床ãšæè»æ§ãç ç²ã«ããŸãããäœã¬ãã«ã®è©³çŽ°ãçŽæ¥æ±ãããšã¯å®¹æã§ã¯ãããŸããã ZeroMQã¯ããœãªã¥ãŒã·ã§ã³ãäœã§ãããã瀺ããäœã¬ãã«ã®ã¢ãããŒãã®é床ã§é«ã¬ãã«ã®ã¡ãœããã䜿çšãã䟿å©ããæäŸããŸãã
ZeroMQã®ç解ãå§ããåã«ããŸãã¡ãã»ãŒãžãã¥ãŒã€ã³ã°ã®äžè¬çãªæŠå¿µãèŠãŠã¿ãŸãããã
ã¡ãã»ãŒãžãã¥ãŒ
ã¡ãã»ãŒãžãã¥ãŒããŸãã¯æè¡çã«ã¯FIFOïŒå å ¥ãå åºãïŒã¯ãåºæ¬çã§ããç 究ãããããŒã¿æ§é ã®1ã€ã§ãã åªå 床ãã¥ãŒãåæ¹åãã¥ãŒãªã©ãããŸããŸãªããããã£ãæã€ããŸããŸãªãã¥ãŒã®å®è£ ããããŸãããäžè¬çãªèãæ¹ã¯ãããŒã¿ã衚瀺ãããããçºä¿¡è ã®æºåãæŽã£ããšãã«ããŒã¿ããã¥ãŒã«è¿œå ããããšããããšã§ãã
ãã ããã¡ãã»ãŒãžãã¥ãŒã¯ãäœãçºçããŠãã¡ãã»ãŒãžãé ä¿¡ãããããšãä¿èšŒããŸãã ã¡ãã»ãŒãžãã¥ãŒã«ãããççµåã³ã³ããŒãã³ãéã®éåæã®çžäºäœçšãå¯èœã«ãªããå³å¯ãªãã¥ãŒã·ãŒã±ã³ã¹ãæäŸãããŸãã éä¿¡ãããããŒã¿ãããã«åŠçã§ããªããªãœãŒã¹ãäžè¶³ããŠããå Žåã¯ããããããµãŒããŒäžã®ã¡ãã»ãŒãžãã¥ãŒã«å ¥ããŠãã¯ã©ã€ã¢ã³ãã®æºåãæŽããŸã§ããŒã¿ãä¿åã§ããŸãã
ã¡ãã»ãŒãžãã¥ãŒã¯ãéåæéä¿¡ããµããŒããããããåæ£ã·ã¹ãã ã®ã¹ã±ãŒãªã³ã°ã«ãããŠéèŠãªåœ¹å²ãæãããŸãã åæã·ã¹ãã ãšéåæã·ã¹ãã ã®éãã«ã€ããŠç°¡åã«èª¬æããŸãã
éåžžã®åæã·ã¹ãã ã§ã¯ãã¿ã¹ã¯ã¯äžåºŠã«1ã€ãã€åŠçãããŸãã ã¿ã¹ã¯ã¯ãåŠçãå®äºãããŸã§åŠçãããªããšèŠãªãããŸãã ããã¯ãäœæ¥ãæŽçããæãç°¡åãªæ¹æ³ã§ãã
ãã®ã·ã¹ãã ã«ã¹ã¬ãããå®è£ ããããšãã§ããŸãã ãã®å Žåãåã¿ã¹ã¯ã®åŠçã¯äžŠè¡ããŠå®è¡ãããŸãã
ãã«ãã¹ã¬ããã¢ãã«ã§ã¯ãã¹ã¬ããã¯ãåäžã®ããã»ããµãŸãã¯è€æ°ã®ããã»ããµ/ã³ã¢äžã®ãªãã¬ãŒãã£ã³ã°ã·ã¹ãã èªäœã«ãã£ãŠå¶åŸ¡ãããŸãã
éåæI / OïŒAIOïŒã«ãããããã°ã©ã ã¯I / OèŠæ±ã®åŠçäžã«å®è¡ãç¶ç¶ã§ããŸãã AIOã¯ããªã¢ã«ã¿ã€ã ã¢ããªã±ãŒã·ã§ã³ã«ã¯äžå¯æ¬ ã§ãã AIOã䜿çšãããšã1ã€ã®ã¹ã¬ããã§è€æ°ã®ã¿ã¹ã¯ãåŠçã§ããŸãã
ããã¯åŸæ¥ã®ããã°ã©ãã³ã°æ¹æ³ã§ãããããã»ã¹ã®éå§åŸããã®å®äºãåŸ ã¡ãŸãã ãã®ã¢ãããŒãã®æ¬ ç¹ã¯ãã¿ã¹ã¯ã®åŠçäžã«ããã°ã©ã ã®å®è¡ããããã¯ããããšã§ãã ããããå¥ã®AIOã¢ãããŒãããããŸãã AIOã§ã¯ãããã»ã¹ã«äŸåããªãã¿ã¹ã¯ããŸã é²è¡äžã§ãã 2çªç®ã®ç« ã§ã¯ãAIOã詳ããèŠãŠãZeroMQã§äœ¿çšããŸãã
ã·ã³ã°ã«ã¹ã¬ãããŸãã¯ãã«ãã¹ã¬ããã®ã¢ãããŒãã§ããã¹ãŠã®ã¹ã¬ãããåŠçããã®ã§ã¯ãªããã¡ãã»ãŒãžãã¥ãŒã䜿çšããå¿ èŠãããã®ãââçåã«æããããããŸããã Google Imagesã«äŒŒãWebã¢ããªã±ãŒã·ã§ã³ãããããŠãŒã¶ãŒãããã€ãã®URLãå ¥åã§ããããã«ããŠããç¶æ³ãèŠãŠã¿ãŸãããã ãã©ãŒã ãéä¿¡ãããšãã¢ããªã±ãŒã·ã§ã³ã¯æå®ãããURLã®ãã¹ãŠã®ç»åã衚瀺ããŸãã ãã ãïŒ
- ã·ã³ã°ã«ã¹ã¬ãããã¥ãŒã䜿çšããå Žåãå€æ°ã®ãŠãŒã¶ãŒã®å Žåãããã°ã©ã ã¯ãã¹ãŠã®URLã®ããŒã¿ãåŠçã§ããŸããã
- ãã¥ãŒã«ãã«ãã¹ã¬ããã¢ãããŒãã䜿çšãããšãã¢ããªã±ãŒã·ã§ã³ãDDoSæ»æãåããå¯èœæ§ããããŸã
- ããŒããŠã§ã¢é害ãçºçãããšããã¹ãŠã®URLã®ããŒã¿ã倱ãããŸã
ãã®å ŽåãURLããŒã¿ããã¥ãŒã«è¿œå ããŠåŠçããå¿ èŠãããããšãããããŸãã ãã®ãããã¡ãã»ãŒãžãã¥ãŒã·ã¹ãã ãå¿ èŠã§ãã
ZeroMQã®æŠèŠ
ãããŸã§ã¯ãã¡ãã»ãŒãžãã¥ãŒãšã¯äœããæ€èšããããããã®æ¬ã®ç®æšãã€ãŸãZeroMQã«è³ããŸããã ZeroMQ Societyã¯ãããããã¹ããã€ããœã±ããããšå®çŸ©ããŠããŸãã æ£åŒã«ã¯ãZeroMQã¯ãéçºè ãåæ£ããã³äžŠåã¢ããªã±ãŒã·ã§ã³ãäœæããã®ã«åœ¹ç«ã€ã¡ãã»ãŒãžã©ã€ãã©ãªãšããŠå®çŸ©ãããŠããŸãã
ZeroMQã«ã€ããŠæåã«åŠã¶ã¹ãããšã¯ãActiveMQãWebSphereMQãRabbitMQãªã©ã®åŸæ¥ã®ã¡ãã»ãŒãžãã¥ãŒã·ã¹ãã ã§ã¯ãªããšããããšã§ãã ZeroMQã¯ç°ãªããŸãã 圌女ã¯ãç¬èªã®ã¡ãã»ãŒãžãã¥ãŒã·ã¹ãã ãäœæããããã®ããŒã«ãæäŸããŸãã ããã¯ã©ã€ãã©ãªã§ãã
ARMããItââaniumãŸã§ã®ããŸããŸãªã¢ãŒããã¯ãã£ã§å®è¡ããã20以äžã®ããã°ã©ãã³ã°èšèªã§ãµããŒããããŠããŸãã
ã·ã³ãã«ã
ZeroMQã¯ç°¡åã§ãã éåæI / Oãå®è¡ã§ããŸãããŸããZeroMQã¯I / Oã¹ããªãŒã ããã®ã¡ãã»ãŒãžããã¥ãŒã«å ¥ããããšãã§ããŸãã ZeroMQ I / Oã¹ããªãŒã ã¯ããããã¯ãŒã¯ãã©ãã£ãã¯ãšã®éä¿¡æã«éåæã§åäœãããããæ®ãã¯ããªãã®ããã«è¡ãããšãã§ããŸãã 以åã«ãœã±ããã䜿çšããããšãããå Žåãããã¯ç°¡åã§ã¯ãªãããšãç¥ã£ãŠããå¿ èŠããããŸãã ãã ããZeroMQã䜿çšãããšãããããç°¡åã«æäœã§ããŸãã
æ§èœ
ZeroMQã¯é«éã§ãã Second Lifeã®Webãµã€ãã§ã¯ã13.4ãã€ã¯ãç§ã®é£ç¶é 延ãš1ç§ãããæ倧4,100,000ã¡ãã»ãŒãžãååŸã§ããŸããã ZeroMQã¯ããã«ããã£ã¹ã転éãããã³ã«ã䜿çšã§ããŸããããã¯ãããŸããŸãªæ¹åã«ããŒã¿ãéä¿¡ããããã®å¹æçãªæ¹æ³ã§ãã
ãããŒã¯ãŒã«ã
ã¡ãã»ãŒãžãã¥ãŒãšã¯äœããZeroMQãã©ã®ãããªãã®ããç解ããããã³ãŒãã®èšè¿°ãéå§ããŸãã ãããŠãã¡ãããæåãªHello Worldããã°ã©ã ããå§ããŸãã
ãµãŒããŒãšã¯ã©ã€ã¢ã³ããããç¶æ³ãèŠãŠã¿ãŸãããã ãµãŒããŒã¯ãã¯ã©ã€ã¢ã³ããã
hello
ã¡ãã»ãŒãžãåä¿¡ãããã³ã«
world
å¿çããŸãã ãµãŒããŒã¯ããŒã4040ã§å®è¡ãããŸãããã¯ã©ã€ã¢ã³ãã¯ããã«å¿ããŠåãããŒãã«ã¡ãã»ãŒãžãéä¿¡ããŸãã
以äžã¯ã
world
ã¡ãã»ãŒãžãã¯ã©ã€ã¢ã³ãã«éä¿¡ãããµãŒããŒã³ãŒãã§ãã
#include <string.h> #include <stdio.h> #include <unistd.h> #include "zmq.h" int main (int argc, char const *argv[]) { void* context = zmq_ctx_new(); void* respond = zmq_socket(context, ZMQ_REP); zmq_bind(respond, "tcp://*:4040"); printf("StartingâŠ\n"); for(;;) { zmq_msg_t request; zmq_msg_init(&request); zmq_msg_recv(&request, respond, 0); printf("Received: hello\n"); zmq_msg_close(&request); sleep(1); // sleep one second zmq_msg_t reply; zmq_msg_init_size(&reply, strlen("world")); memcpy(zmq_msg_data(&reply), "world", 5); zmq_msg_send(&reply, respond, 0); zmq_msg_close(&reply); } zmq_close(respond); zmq_ctx_destroy(context); return 0; }
以äžã¯ã
hello
ã¡ãã»ãŒãžããµãŒããŒã«éä¿¡ããã¯ã©ã€ã¢ã³ãã³ãŒãã§ãã
#include <string.h> #include <stdio.h> #include <unistd.h> #include "zmq.h" int main (int argc, char const *argv[]) { void* context = zmq_ctx_new(); printf("Client StartingâŠ.\n"); void* request = zmq_socket(context, ZMQ_REQ); zmq_connect(request, "tcp://localhost:4040"); int count = 0; for(;;) { zmq_msg_t req; zmq_msg_init_size(&req, strlen("hello")); memcpy(zmq_msg_data(&req), "hello", 5); printf("Sending: hello - %d\n", count); zmq_msg_send(&req, request, 0); zmq_msg_close(&req); zmq_msg_t reply; zmq_msg_init(&reply); zmq_msg_recv(&reply, request, 0); printf("Received: hello - %d\n", count); zmq_msg_close(&reply); count++; } // We never get here though. zmq_close(request); zmq_ctx_destroy(context); return 0; }
次ã®å³ã«ç€ºãããã«ãæåã®åºæ¬ã¢ãŒããã¯ãã£ã§ããrequest-responseããããŸãã
ã³ãŒãã詳ããèŠãŠãã©ã®ããã«æ©èœããããç解ããŸãããã
ãŸããã³ã³ããã¹ããšãœã±ãããäœæããŸãã
zmq_ctx_new()
ã¡ãœããã¯æ°ããã³ã³ããã¹ããäœæããŸãã ã¹ã¬ããã»ãŒããªã®ã§ãã³ã³ããã¹ãã¯è€æ°ã®ã¹ã¬ããã§äœ¿çšã§ããŸãã
zmq_socket(2)
ã¯ãç¹å®ã®ã³ã³ããã¹ãã§æ°ãããœã±ãããäœæããŸãã ZeroMQãœã±ããã¯ã¹ã¬ããã»ãŒãã§ã¯ãªããããäœæãããã¹ã¬ããã§ã®ã¿äœ¿çšããå¿ èŠããããŸãã åŸæ¥ã®ãœã±ããã¯åæã§ãããZeroMQãœã±ããã¯ã¯ã©ã€ã¢ã³ãåŽãšãµãŒããŒåŽã«1ã€ã®ãã¥ãŒãäœæããéåæã§èŠæ±/å¿çãã¿ãŒã³ãå¶åŸ¡ã§ããŸãã ZeroMQã¯ãæ¥ç¶èšå®ãåæ¥ç¶ãåæãã³ã³ãã³ãé ä¿¡ãèªåçã«æŽçããŸãã 第3ç« ã§ã¯ãåŸæ¥ã®ãœã±ãããšZeroMQãœã±ããã®éããããã«è©³ãã調ã¹ãŸãã
ãµãŒããŒã¯ã
ZMQ_REP
ãœã±ãããš
ZMQ_REP
ããŒãããã€ã³ãããã¡ãã»ãŒãžãåä¿¡ãããã³ã«èŠæ±ã®åŸ æ©ãšå¿çãéå§ããŸãã
ãã®åçŽãªhello worldããã°ã©ã ã¯ãæåã®èŠæ±/å¿çãã¿ãŒã³ã®äœ¿çšäŸã瀺ããŠããŸãã
èŠæ±/å¿çãã¿ãŒã³
èŠæ±/å¿çãã³ãã¬ãŒãïŒãã¿ãŒã³ïŒã䜿çšããŠãã¯ã©ã€ã¢ã³ããã1ã€ä»¥äžã®ãµãŒããŒã«ã¡ãã»ãŒãžãéä¿¡ããéä¿¡ãããåã¡ãã»ãŒãžã«å¯Ÿããå¿çãååŸããŸãã ããã¯ãZeroMQã䜿çšããæãç°¡åãªæ¹æ³ã§ãã åçã®ãªã¯ãšã¹ãã¯é©åãªãã®ã§ãªããã°ãªããŸããã
çã
èŠæ±-å¿çãã¿ãŒã³ã®å¿çéšåã¯æ¬¡ã®ãšããã§ãã
void* context = zmq_ctx_new(); void* respond = zmq_socket(context, ZMQ_REP); zmq_bind(respond, "tcp://*:4040");
ãµãŒããŒã¯
ZMQ_REP
ãœã±ããã䜿çšããŠãã€ã³ã¿ãŒãããããã¡ãã»ãŒãžãåä¿¡ããã¯ã©ã€ã¢ã³ãã«å¿çãéä¿¡ããŸãã
fair-queue
æŠç¥ã¯
ZMQ_REP
ããã®çä¿¡ã¡ãã»ãŒãžã®ã«ãŒãã£ã³ã°ã«æå¹ã§ããã
last-peer
çºä¿¡ã¡ãã»ãŒãžã«æå¹ã§ãã
ãã§ã¢ãã¥ãŒæŠç¥
ãã®æ¬ã¯å®å šã«ãã¥ãŒå°çšã§ãã
fair-queue
æŠç¥ã«ã€ããŠè©±ããšããç§ãã¡ãäœãæå³ããã®ããç¥ã£ãŠé©ããããããŸããã ããã¯ãå®çŸ©äžå ¬å¹³ãªãªãœãŒã¹ãèšç»ããã³å²ãåœãŠãããã®ã¢ã«ãŽãªãºã ã§ãã
ä»çµã¿ãç解ããããã«ãåã®å³ã®ã¹ããªãŒã ã¯ãããã1ç§ããã16ã2ã6ãããã³8ãã±ãããéä¿¡ããŸãããåºåã§ã¯1ç§ããã12ãã±ããããåŠçã§ããªããšããŸãããã ãã®å Žåã1ç§ããã4ãã±ãããéä¿¡ã§ããŸãããã¹ããªãŒã 2ã¯1ç§ããã2ãã±ããããéä¿¡ããŸããã ãã§ã¢ãã¥ãŒã«ãŒã«ã§ã¯ããã¹ãŠã®åºå£ãåæã«ç©ºããŠããªãéããç¡æã®åºå£ã¯ååšã§ããŸããã ãããã£ãŠãStream 2ã§1ç§ããã2ãã±ããã転éããæ®ãã®10åãæ®ãã®ã¹ã¬ããã«åå²ããããšãã§ããŸãã
ãã®çä¿¡ã¡ãã»ãŒãžã«ãŒãã£ã³ã°æŠç¥ã¯
ZMQ_REP
䜿çšã
ZMQ_REP
ã 埪ç°èšç»ã¯ãFairãã¥ãŒæŠç¥ãå®è£ ããæãç°¡åãªæ¹æ³ã§ãããZeroMQã§ã䜿çšãããŸãã
ãªã¯ãšã¹ã
以äžã¯ãèŠæ±/å¿çãã¿ãŒã³ã®ã¯ãšãªéšåã§ãã
void* context = zmq_ctx_new(); printf("Client StartingâŠ.\n"); void* request = zmq_socket(context, ZMQ_REQ); zmq_connect(request, "tcp://localhost:4040");
ã¯ã©ã€ã¢ã³ãã¯
ZMQ_REQ
ã䜿çšããŠã¡ãã»ãŒãžãéä¿¡ãããµãŒããŒããå¿çãåä¿¡ããŸãã ãã¹ãŠã®ã¡ãã»ãŒãžã¯ã
round-robin
ã«ãŒãã£ã³ã°æŠç¥ã䜿çšããŠéä¿¡ãããŸãã çä¿¡ã«ãŒãã£ã³ã°æŠç¥ã¯
last-peer
ã§ãã
ZMQ_REQ
ã¯ã¡ãã»ãŒãžãã¹ããŒããŸããã ã¡ãã»ãŒãžã®éä¿¡ã«äœ¿çšã§ãããµãŒãã¹ããªãå ŽåããŸãã¯ãã¹ãŠã®ãµãŒãã¹ãããžãŒã®å Žåã
zmq_send(3)
æäœãéä¿¡ããããµãŒããŒã®1ã€ãã¡ãã»ãŒãžã®éä¿¡ã«äœ¿çšå¯èœã«ãªããŸã§ãããã¯ãããŸãã
ZMQ_REQ
ãã¿ã€ã
ZMQ_REP
ããã³
ZMQ_REQ
äºææ§ããããŸãã 第4ç« ã§ã¯ã
ZMQ_ROUTER
ãŸãã
ã¡ãã»ãŒãžéä¿¡
ãã®ããŒãã§ã¯ããªã¯ãšã¹ããšã¬ã¹ãã³ã¹ã®ã»ã¯ã·ã§ã³ãçµã¿åãããŠã誰ãã®ãªã¯ãšã¹ãã«å¯Ÿããã¬ã¹ãã³ã¹ãã©ã®ããã«çºçããã©ã®ããã«ã¬ã¹ãã³ã¹ãããã瀺ããŸãã
printf("Sending: hello - %d\n", count); zmq_msg_send(&req, request, 0); zmq_msg_close(&req);
ã¯ã©ã€ã¢ã³ãã¯ã
zmq_msg_send(3)
ã䜿çšããŠãµãŒããŒã«ã¡ãã»ãŒãžãéä¿¡ããŸãã ããã¯å¥ã®ã¡ãã»ãŒãžã§ããããœã±ããã«éä¿¡ããŸãã
int zmq_send_msg(zmq_msg_t *msg, void *socket, int flags)
zmq_msg_send
ã¯ãã¡ãã»ãŒãžããœã±ãããããã³ãã©ã°ã®3ã€ã®ãã©ã¡ãŒã¿ãŒãåãå ¥ããŸãã
- ã¡ãã»ãŒãžãã©ã¡ãŒã¿ã¯ãªã¯ãšã¹ãäžã«ãªã»ããããããããè€æ°ã®ãœã±ããã«ã¡ãã»ãŒãžãéä¿¡ããå Žåã¯ãã³ããŒããå¿ èŠããããŸãã
- æåãã
zmq_msg_send()
ãªã¯ãšã¹ãã¯ãã¡ãã»ãŒãžããããã¯ãŒã¯çµç±ã§éä¿¡ããããã©ããã瀺ããŸããã - ãã©ã°ãã©ã¡ãŒã¿ã¯ã
ZMQ_DONTWAIT
ãŸãã¯ZMQ_SNDMORE. ZMQ_DONTWAIT
ããããZMQ_SNDMORE. ZMQ_DONTWAIT
ZMQ_SNDMORE. ZMQ_DONTWAIT
ã¯ãã¡ãã»ãŒãžãéåæçã«éä¿¡ããå¿ èŠãããããšã瀺ããŸããZMQ_SNDMORE
ã¯ãã¡ãã»ãŒãžãåæãããæ®ãã®ã¡ãã»ãŒãžãéä¿¡äžã§ããããšã瀺ããŸãã
ã¡ãã»ãŒãžãéä¿¡ããåŸãã¯ã©ã€ã¢ã³ãã¯å¿çãåä¿¡ãããŸã§åŸ æ©ããŸãã ããã¯
zmq_msg_recv(3)
ã䜿çšããŠè¡ãããŸãã
zmq_msg_recv(&reply, request, 0); printf("Received: hello - %d\n", count); zmq_msg_close(&reply);
zmq_msg_recv(3)
ã¯ã
socket
ãã©ã¡ãŒã¿ãŒã§æå®ãããŠããããã«ããœã±ããããã¡ãã»ãŒãžã®äžéšãåä¿¡ããå¿çã
message
ãã©ã¡ãŒã¿ãŒã«ä¿åããŸãã
int zmq_msg_recv (zmq_msg_t *msg, void *socket, int flags)
zmq_msg_recv
ã¯ãã¡ãã»ãŒãžããœã±ããããã©ã°ã®3ã€ã®ãã©ã¡ãŒã¿ãŒãåãå ¥ããŸãã
- 以åã«åä¿¡ããã¡ãã»ãŒãžïŒããå ŽåïŒã¯ç¡å¹ã§ã
- ãã©ã°ãã©ã¡ãŒã¿ãŒã¯
ZMQ_DONTWAIT
ã«ããããšãã§ããŸããããã¯ãæäœãéåæçã«å®è¡ããå¿ èŠãããããšã瀺ããŸã
Cã§æååãæäœãã
åããã°ã©ãã³ã°èšèªã«ã¯ãæååãåŠçããç¬èªã®ã¢ãããŒãããããŸãã éåžžãErlangã«ã¯æååèªäœã¯ãããŸããïŒæååãšããŠè¡šãããŸãïŒã Cããã°ã©ãã³ã°èšèªã§ã¯ãæ«å°ŸããŒãã®æååã Cã®æååã¯åºæ¬çã«æåé åã§ããã\ 0ãã¯æååã®çµãããæå³ããŸãã æååãšã©ãŒã¯äžè¬çã§ãããå€ãã®è匱æ§ãåå ã§ãã
Miller et alãïŒ1995ïŒã«ãããšãUnixã®è匱æ§ã®65ïŒ ã¯ãŒããã€ãããããã¡ãªãŒããŒãããŒãªã©ã®æååãšã©ãŒã«ãããã®ã§ãããããCã§ã®æåååŠçã¯æ éã«è¡ãå¿ èŠããããŸãã
ZeroMQã䜿çšããå Žåãããã¯ããªãã®è©ã«ããã£ãŠãä»ã®ã¢ããªã±ãŒã·ã§ã³ãèªãããšãã§ããããã«ã¡ãã»ãŒãžã確å®ã«ãã©ãŒããããããŸãã ZeroMQã¯ã¡ãã»ãŒãžã®ãµã€ãºã®ã¿ãç¥ã£ãŠããŸãã
éåžžã1ã€ã®ã¢ããªã±ãŒã·ã§ã³ã§ç°ãªãããã°ã©ãã³ã°èšèªã䜿çšããå Žåã§ãã è¡ã®æåŸã«ãã«ãã€ããè¿œå ããªãããã°ã©ãã³ã°èšèªã§æžãããã¢ããªã±ãŒã·ã§ã³ããäœããã®æ¹æ³ã§Cã§æžãããã¢ããªã±ãŒã·ã§ã³ãšéä¿¡ããå¿ èŠãããå Žåãå¥åŠãªçµæãåŸãããŸãã
äŸã®ããã«ã0ãã€ãã®worldãªã©ã®ã¡ãã»ãŒãžãéä¿¡ã§ããŸãã
zmq_msg_init_data_(&request, "world", 6, NULL, NULL);
ãã ããErlangã§ã¯ã次ã®ããã«åãã¡ãã»ãŒãžãéä¿¡ããŸãã
erlzmq:send(Request, <<"world">>)
Cã§æžãããã¯ã©ã€ã¢ã³ããErlangã§æžããããµãŒãã¹ã§ããZeroMQã«æ¥ç¶ãããã®ãµãŒãã¹ã«ã¡ãã»ãŒãžã
world
éä¿¡ãããšããŸãã ãã®å ŽåãErlangã¯
world
ãåºåã
world
ã nullãã€ãã®ã¡ãã»ãŒãžãéä¿¡ãããšãErlangã¯æ¬¡ã®
[119,111,114,108,100,0]
ãåºåããŸãã ç®çã®è¡ã®ä»£ããã«ãããã€ãã®æ°åãå«ããªã¹ãããããŸããããããã¯ASCIIã³ãŒãã§ãã ãã ããããã¯æååãšããŠè§£éãããªããªããŸããã
ZeroMQã®è¡ã¯é·ããåºå®ãããŠããããŒããã€ããªãã§éä¿¡ãããŸãã ãããã£ãŠãZeroMQæååã¯ãé·ããšãšãã«æ°ãã€ãïŒãã®äŸã§ã¯æååèªäœïŒãšããŠéä¿¡ãããŸãã
ZeroMQããŒãžã§ã³ã®ç¢ºèª
䜿çšããŠããZeroMQã®ããŒãžã§ã³ãç¥ãããšã¯éåžžã«äŸ¿å©ã§ãã å Žåã«ãã£ãŠã¯ãäžèŠãªé©ããé¿ããããã«ãæ£ç¢ºãªããŒãžã§ã³ãç¥ãå¿ èŠããããŸãã ããšãã°ãZeroMQ 2.xãšZeroMQ 3.xã«ã¯ãåŸæ¥ã®æ¹æ³ã䜿çšãããªã©ãããã€ãã®éãããããŸãã ãããã£ãŠãã³ã³ãã¥ãŒã¿ãŒã«ã€ã³ã¹ããŒã«ãããŠããZeroMQã®æ£ç¢ºãªããŒãžã§ã³ãããã£ãŠããå Žåã¯ãå»æ¢ãããã¡ãœããã®äœ¿çšãé¿ããããšãã§ããŸãã
#include <stdio.h> #include "zmq.h" int main (int argc, char const *argv[]) { int major, minor, patch; zmq_version(&major, &minor, &patch); printf("Installed ZeroMQ version: %d.%d.%d\n", major, minor, patch); return 0; }
ãŸãšã
ãã®ç« ã§ã¯ãã¡ãã»ãŒãžãã¥ãŒãšã¯äœãã調ã¹ãZeroMQã«ã€ããŠãç°¡åã«çŽ¹ä»ããŸããã ZeroMQãæååãšæåã®ãã¿ãŒã³ïŒèŠæ±/å¿çïŒãåŠçããæ¹æ³ã調ã¹ãŸããã ãŸãã圌ãã¯æåã®hello worldã¢ããªãäœæããŸããã
ãªã³ã¯ãããã®èšäºã®ãªãœãŒã¹ãããŠã³ããŒãã§ããŸã
ãæž èŽããããšãããããŸããïŒ