Artifact
c0c7d25b62046b229f22de0c46c253b5565a9761:
- Executable file
nanomsg/remote-repl
— part of check-in
[cd7a31d87b]
at
2017-05-03 18:01:41
on branch trunk
— many fixes to usb.sls
(user:
aldo
size: 2288)
0000: 23 21 20 2f 75 73 72 2f 62 69 6e 2f 65 6e 76 20 #! /usr/bin/env
0010: 73 63 68 65 6d 65 2d 73 63 72 69 70 74 0a 3b 3b scheme-script.;;
0020: 20 2d 2a 2d 20 6d 6f 64 65 3a 20 73 63 68 65 6d -*- mode: schem
0030: 65 20 2d 2a 2d 0a 3b 3b 0a 3b 3b 20 43 6f 70 79 e -*-.;;.;; Copy
0040: 72 69 67 68 74 20 32 30 31 36 20 41 6c 64 6f 20 right 2016 Aldo
0050: 4e 69 63 6f 6c 61 73 20 42 72 75 6e 6f 0a 3b 3b Nicolas Bruno.;;
0060: 0a 3b 3b 20 4c 69 63 65 6e 73 65 64 20 75 6e 64 .;; Licensed und
0070: 65 72 20 74 68 65 20 41 70 61 63 68 65 20 4c 69 er the Apache Li
0080: 63 65 6e 73 65 2c 20 56 65 72 73 69 6f 6e 20 32 cense, Version 2
0090: 2e 30 20 28 74 68 65 20 22 4c 69 63 65 6e 73 65 .0 (the "License
00a0: 22 29 3b 0a 3b 3b 20 79 6f 75 20 6d 61 79 20 6e ");.;; you may n
00b0: 6f 74 20 75 73 65 20 74 68 69 73 20 66 69 6c 65 ot use this file
00c0: 20 65 78 63 65 70 74 20 69 6e 20 63 6f 6d 70 6c except in compl
00d0: 69 61 6e 63 65 20 77 69 74 68 20 74 68 65 20 4c iance with the L
00e0: 69 63 65 6e 73 65 2e 0a 3b 3b 20 59 6f 75 20 6d icense..;; You m
00f0: 61 79 20 6f 62 74 61 69 6e 20 61 20 63 6f 70 79 ay obtain a copy
0100: 20 6f 66 20 74 68 65 20 4c 69 63 65 6e 73 65 20 of the License
0110: 61 74 0a 3b 3b 0a 3b 3b 20 20 20 20 20 68 74 74 at.;;.;; htt
0120: 70 3a 2f 2f 77 77 77 2e 61 70 61 63 68 65 2e 6f p://www.apache.o
0130: 72 67 2f 6c 69 63 65 6e 73 65 73 2f 4c 49 43 45 rg/licenses/LICE
0140: 4e 53 45 2d 32 2e 30 0a 3b 3b 0a 3b 3b 20 55 6e NSE-2.0.;;.;; Un
0150: 6c 65 73 73 20 72 65 71 75 69 72 65 64 20 62 79 less required by
0160: 20 61 70 70 6c 69 63 61 62 6c 65 20 6c 61 77 20 applicable law
0170: 6f 72 20 61 67 72 65 65 64 20 74 6f 20 69 6e 20 or agreed to in
0180: 77 72 69 74 69 6e 67 2c 20 73 6f 66 74 77 61 72 writing, softwar
0190: 65 0a 3b 3b 20 64 69 73 74 72 69 62 75 74 65 64 e.;; distributed
01a0: 20 75 6e 64 65 72 20 74 68 65 20 4c 69 63 65 6e under the Licen
01b0: 73 65 20 69 73 20 64 69 73 74 72 69 62 75 74 65 se is distribute
01c0: 64 20 6f 6e 20 61 6e 20 22 41 53 20 49 53 22 20 d on an "AS IS"
01d0: 42 41 53 49 53 2c 0a 3b 3b 20 57 49 54 48 4f 55 BASIS,.;; WITHOU
01e0: 54 20 57 41 52 52 41 4e 54 49 45 53 20 4f 52 20 T WARRANTIES OR
01f0: 43 4f 4e 44 49 54 49 4f 4e 53 20 4f 46 20 41 4e CONDITIONS OF AN
0200: 59 20 4b 49 4e 44 2c 20 65 69 74 68 65 72 20 65 Y KIND, either e
0210: 78 70 72 65 73 73 20 6f 72 20 69 6d 70 6c 69 65 xpress or implie
0220: 64 2e 0a 3b 3b 20 53 65 65 20 74 68 65 20 4c 69 d..;; See the Li
0230: 63 65 6e 73 65 20 66 6f 72 20 74 68 65 20 73 70 cense for the sp
0240: 65 63 69 66 69 63 20 6c 61 6e 67 75 61 67 65 20 ecific language
0250: 67 6f 76 65 72 6e 69 6e 67 20 70 65 72 6d 69 73 governing permis
0260: 73 69 6f 6e 73 20 61 6e 64 0a 3b 3b 20 6c 69 6d sions and.;; lim
0270: 69 74 61 74 69 6f 6e 73 20 75 6e 64 65 72 20 74 itations under t
0280: 68 65 20 4c 69 63 65 6e 73 65 2e 0a 0a 3b 20 65 he License...; e
0290: 78 61 6d 70 6c 65 20 6f 66 20 72 65 6d 6f 74 65 xample of remote
02a0: 20 52 45 50 4c 0a 3b 20 79 6f 75 20 63 61 6e 20 REPL.; you can
02b0: 75 73 65 20 61 6e 79 20 74 72 61 6e 73 70 6f 72 use any transpor
02c0: 74 2c 20 74 63 70 3a 2f 2f 20 69 70 63 3a 2f 2f t, tcp:// ipc://
02d0: 20 77 65 62 73 6f 63 6b 65 74 20 69 6e 70 72 6f websocket inpro
02e0: 63 0a 3b 20 73 65 65 20 68 74 74 70 3a 2f 2f 6e c.; see http://n
02f0: 61 6e 6f 6d 73 67 2e 6f 72 67 2f 76 31 2e 30 2e anomsg.org/v1.0.
0300: 30 2f 6e 61 6e 6f 6d 73 67 2e 37 2e 68 74 6d 6c 0/nanomsg.7.html
0310: 0a 3b 20 61 6e 64 20 6e 61 6e 6f 6d 73 67 2e 73 .; and nanomsg.s
0320: 6c 73 20 66 6f 72 20 64 65 74 61 69 6c 73 0a 3b ls for details.;
0330: 0a 3b 20 2e 2f 6c 6f 63 61 6c 2d 72 65 70 6c 20 .; ./local-repl
0340: 74 63 70 3a 2f 2f 31 32 37 2e 30 2e 30 2e 31 3a tcp://127.0.0.1:
0350: 31 32 33 34 35 0a 3b 20 41 42 43 0a 3b 0a 3b 20 12345.; ABC.;.;
0360: 6d 65 61 6e 77 68 69 6c 65 20 69 6e 20 61 6e 6f meanwhile in ano
0370: 74 68 65 72 20 74 65 72 6d 69 6e 61 6c 2e 2e 2e ther terminal...
0380: 0a 3b 20 2e 2f 72 65 6d 6f 74 65 2d 72 65 70 6c .; ./remote-repl
0390: 20 74 63 70 3a 2f 2f 31 32 37 2e 30 2e 30 2e 31 tcp://127.0.0.1
03a0: 3a 31 32 33 34 35 0a 3b 20 3e 20 28 2b 20 31 20 :12345.; > (+ 1
03b0: 32 29 0a 3b 20 33 0a 3b 20 3e 20 28 70 72 69 6e 2).; 3.; > (prin
03c0: 74 66 20 22 41 42 43 7e 6e 22 29 20 2d 2d 3e 20 tf "ABC~n") -->
03d0: 77 69 6c 6c 20 70 72 69 6e 74 20 41 42 43 20 6f will print ABC o
03e0: 6e 20 74 68 65 20 6c 6f 63 61 6c 20 72 65 70 6c n the local repl
03f0: 20 70 72 6f 63 65 73 73 0a 3b 0a 3b 20 54 68 65 process.;.; The
0400: 20 6e 69 63 65 20 74 68 69 6e 67 20 69 73 20 74 nice thing is t
0410: 68 61 74 20 79 6f 75 20 63 61 6e 20 73 63 61 6c hat you can scal
0420: 65 20 74 6f 20 61 6e 79 20 70 72 6f 74 6f 63 6f e to any protoco
0430: 6c 20 61 6e 64 0a 3b 20 74 68 61 74 20 69 74 20 l and.; that it
0440: 63 61 6e 20 62 65 20 69 6e 74 65 67 72 61 74 65 can be integrate
0450: 64 20 69 6e 20 61 6e 20 65 76 65 6e 74 2f 6d 61 d in an event/ma
0460: 69 6e 20 6c 6f 6f 70 0a 3b 20 69 74 20 75 73 65 in loop.; it use
0470: 73 20 6e 6f 6e 2d 62 6c 6f 63 6b 69 6e 67 20 28 s non-blocking (
0480: 72 65 61 64 29 20 61 6e 64 20 79 6f 75 20 69 6e read) and you in
0490: 76 6f 6b 65 20 28 6d 79 2d 6c 6f 63 61 6c 2d 72 voke (my-local-r
04a0: 65 70 6c 29 20 75 6e 74 69 6c 20 3d 3e 20 23 66 epl) until => #f
04b0: 0a 3b 0a 3b 20 54 4f 44 4f 3a 20 61 64 64 20 61 .;.; TODO: add a
04c0: 20 63 6f 6d 6d 61 6e 64 20 66 6f 72 20 6c 6f 63 command for loc
04d0: 61 6c 20 65 78 69 74 2e 20 23 21 65 6f 66 20 6f al exit. #!eof o
04e0: 62 6a 65 63 74 20 77 69 6c 6c 20 6f 6e 6c 79 20 bject will only
04f0: 63 6c 6f 73 65 20 74 68 65 20 72 65 6d 6f 74 65 close the remote
0500: 20 0a 3b 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 2e .; connection..
0510: 2e 2e 0a 0a 23 21 63 68 65 7a 73 63 68 65 6d 65 ....#!chezscheme
0520: 0a 0a 28 69 6d 70 6f 72 74 20 28 63 68 65 7a 73 ..(import (chezs
0530: 63 68 65 6d 65 29 20 28 6e 61 6e 6f 6d 73 67 29 cheme) (nanomsg)
0540: 29 0a 28 6e 61 6e 6f 6d 73 67 2d 6c 69 62 72 61 ).(nanomsg-libra
0550: 72 79 2d 69 6e 69 74 29 0a 0a 28 64 65 66 69 6e ry-init)..(defin
0560: 65 20 61 72 67 76 20 28 63 6f 6d 6d 61 6e 64 2d e argv (command-
0570: 6c 69 6e 65 2d 61 72 67 75 6d 65 6e 74 73 29 29 line-arguments))
0580: 0a 0a 28 64 65 66 69 6e 65 20 73 6f 63 6b 20 28 ..(define sock (
0590: 6e 6e 2d 73 6f 63 6b 65 74 20 41 46 5f 53 50 20 nn-socket AF_SP
05a0: 4e 4e 5f 52 45 51 29 29 0a 28 64 65 66 69 6e 65 NN_REQ)).(define
05b0: 20 65 69 64 20 28 63 6f 6e 64 20 5b 28 6e 75 6c eid (cond [(nul
05c0: 6c 3f 20 61 72 67 76 29 0a 09 09 20 20 20 28 6e l? argv)... (n
05d0: 6e 2d 63 6f 6e 6e 65 63 74 20 73 6f 63 6b 20 22 n-connect sock "
05e0: 74 63 70 3a 2f 2f 6c 6f 63 61 6c 68 6f 73 74 3a tcp://localhost:
05f0: 39 38 38 38 22 29 5d 0a 09 09 20 20 5b 65 6c 73 9888")]... [els
0600: 65 0a 09 09 20 20 20 28 6e 6e 2d 63 6f 6e 6e 65 e... (nn-conne
0610: 63 74 20 73 6f 63 6b 20 28 63 61 72 20 61 72 67 ct sock (car arg
0620: 76 29 29 5d 29 29 0a 0a 28 63 61 6c 6c 2f 63 63 v))]))..(call/cc
0630: 20 0a 20 28 6c 61 6d 62 64 61 20 28 72 65 74 75 . (lambda (retu
0640: 72 6e 29 0a 20 20 20 28 6c 65 74 20 6c 6f 6f 70 rn). (let loop
0650: 20 28 29 0a 20 20 20 20 20 28 67 75 61 72 64 20 (). (guard
0660: 0a 20 20 20 20 20 20 28 65 20 28 65 6c 73 65 20 . (e (else
0670: 28 70 72 69 6e 74 66 20 22 65 72 72 6f 72 20 69 (printf "error i
0680: 6e 20 72 65 6d 6f 74 65 2d 72 65 70 6c 3a 20 6f n remote-repl: o
0690: 6e 20 7e 64 3a 20 7e 64 20 77 69 74 68 20 69 72 n ~d: ~d with ir
06a0: 72 69 74 61 6e 74 73 20 7e 64 7e 6e 22 20 0a 09 ritants ~d~n" ..
06b0: 09 20 20 20 20 20 20 20 28 69 66 20 28 77 68 6f . (if (who
06c0: 2d 63 6f 6e 64 69 74 69 6f 6e 3f 20 65 29 20 28 -condition? e) (
06d0: 63 6f 6e 64 69 74 69 6f 6e 2d 77 68 6f 20 65 29 condition-who e)
06e0: 20 27 75 6e 6b 6e 6f 77 6e 29 0a 09 09 20 20 20 'unknown)...
06f0: 20 20 20 20 28 69 66 20 28 6d 65 73 73 61 67 65 (if (message
0700: 2d 63 6f 6e 64 69 74 69 6f 6e 3f 20 65 29 20 28 -condition? e) (
0710: 63 6f 6e 64 69 74 69 6f 6e 2d 6d 65 73 73 61 67 condition-messag
0720: 65 20 65 29 20 22 22 29 0a 09 09 20 20 20 20 20 e e) "")...
0730: 20 20 28 69 66 20 28 69 72 72 69 74 61 6e 74 73 (if (irritants
0740: 2d 63 6f 6e 64 69 74 69 6f 6e 3f 20 65 29 20 28 -condition? e) (
0750: 63 6f 6e 64 69 74 69 6f 6e 2d 69 72 72 69 74 61 condition-irrita
0760: 6e 74 73 20 65 29 20 22 22 29 29 29 29 0a 20 20 nts e) "")))).
0770: 20 20 20 20 28 70 72 69 6e 74 66 20 22 3e 20 22 (printf "> "
0780: 29 0a 20 20 20 20 20 20 28 6e 6e 2d 73 65 6e 64 ). (nn-send
0790: 20 73 6f 63 6b 20 28 73 74 72 69 6e 67 2d 3e 75 sock (string->u
07a0: 74 66 38 20 0a 09 09 20 20 20 20 20 28 63 61 6c tf8 ... (cal
07b0: 6c 2d 77 69 74 68 2d 73 74 72 69 6e 67 2d 6f 75 l-with-string-ou
07c0: 74 70 75 74 2d 70 6f 72 74 0a 09 09 20 20 20 20 tput-port...
07d0: 20 20 28 6c 61 6d 62 64 61 20 28 70 29 0a 09 09 (lambda (p)...
07e0: 09 28 6c 65 74 20 28 5b 74 6f 6b 65 6e 20 28 72 .(let ([token (r
07f0: 65 61 64 29 5d 29 0a 09 09 09 20 20 28 69 66 20 ead)]).... (if
0800: 28 65 6f 66 2d 6f 62 6a 65 63 74 3f 20 74 6f 6b (eof-object? tok
0810: 65 6e 29 0a 09 09 09 20 20 20 20 20 20 28 72 65 en).... (re
0820: 74 75 72 6e 20 23 66 29 0a 09 09 09 20 20 20 20 turn #f)....
0830: 20 20 28 77 72 69 74 65 20 74 6f 6b 65 6e 20 70 (write token p
0840: 29 29 29 29 29 29 20 30 29 0a 20 20 20 20 20 20 )))))) 0).
0850: 28 6c 65 74 20 28 5b 62 75 66 20 28 62 6f 78 20 (let ([buf (box
0860: 23 74 29 5d 29 0a 09 28 6e 6e 2d 72 65 63 76 20 #t)])..(nn-recv
0870: 73 6f 63 6b 20 62 75 66 20 4e 4e 5f 4d 53 47 20 sock buf NN_MSG
0880: 30 29 0a 09 28 6c 65 74 20 28 5b 73 20 28 75 74 0)..(let ([s (ut
0890: 66 38 2d 3e 73 74 72 69 6e 67 20 28 75 6e 62 6f f8->string (unbo
08a0: 78 20 62 75 66 29 29 5d 29 0a 09 20 20 28 70 72 x buf))]).. (pr
08b0: 69 6e 74 66 20 22 7e 64 22 20 28 69 66 20 28 73 intf "~d" (if (s
08c0: 74 72 69 6e 67 3d 3f 20 22 23 3c 76 6f 69 64 3e tring=? "#<void>
08d0: 5c 6e 22 20 73 29 20 22 22 20 73 29 29 29 29 29 \n" s) "" s)))))
08e0: 0a 20 20 20 20 20 28 6c 6f 6f 70 29 29 29 29 0a . (loop)))).