1 /** 2 Nanomsg Example: Filesend 3 */ 4 module nanomsg.examples.filesend; 5 6 import nanomsg; 7 import std.stdio; 8 import std.conv; 9 import std.file; 10 import std.string:toStringz; 11 12 /// 13 enum NODE0 ="node0"; 14 /// 15 enum NODE1 ="node1"; 16 17 /// 18 int node0 (string xurl) 19 { 20 int sock = nn_socket (AF_SP, NN_PULL); 21 auto url=cast(char*)xurl; 22 assert(sock >= 0); 23 assert(nn_bind (sock, url) >= 0); 24 while (1) 25 { 26 char* buf = cast(char*)0; 27 int bytes = nn_recv (sock, &buf, NN_MSG, 0); 28 assert (bytes >= 0); 29 writefln("NODE0: RECEIVED %s bytes: \"%s\"", bytes,to!string(buf)); 30 nn_freemsg (buf); 31 } 32 return 0; 33 } 34 35 /// 36 int sendfile(string url, string filename) 37 { 38 auto msg=cast(char[])read(filename); 39 int sz_msg = cast(int)msg.length + 1; // '\0' too 40 int sock = nn_socket (AF_SP, NN_PUSH); 41 assert(sock >= 0); 42 assert(nn_connect(sock, url.toStringz) >= 0); 43 writefln("NODE1: SENDING \"%s\"", msg.to!string); 44 int bytes = nn_send(sock, msg.toStringz, sz_msg, 0); 45 assert(bytes == sz_msg); 46 return nn_shutdown(sock, 0); 47 } 48 49 /// 50 int main (string[] argv) 51 { 52 if (argv.length>1) 53 { 54 if (NODE0==argv[1]) 55 return node0(argv[2]); 56 else if (argv.length>2) 57 if (NODE1==argv[1]) 58 return sendfile(argv[2], argv[3]); 59 } else 60 { 61 writefln("Usage: filesend %s|%s <URL> <ARG> ...'",NODE0, NODE1); 62 return 1; 63 } 64 return 0; 65 }