Changeset 34230 in project


Ignore:
Timestamp:
07/12/17 15:42:37 (2 months ago)
Author:
svnwiki
Message:

Anonymous wiki edit for IP [213.52.73.118]: update nrepl documentation from latest readme.md

File:
1 edited

Legend:

Unmodified
Added
Removed
  • wiki/eggref/4/nrepl

    r30904 r34230  
    11== NREPL
    22[[toc:]]
    3 An blocking networked REPL for Chicken Scheme. Each new incoming
    4 connection runs in a new srfi-18 thread.
     3A networked REPL for Chicken Scheme. Each new incoming connection runs
     4in a new {{srfi-18}} thread.
    55
    66
    77=== Requirements
    8 None except the core tcp and srfi-18 units.
     8None except the {{tcp}} and {{srfi-18}} units from CHICKEN core.
    99
    1010
    1111=== API
    12 <procedure> (nrepl <port> [spawn])</procedure>
     12<procedure> (nrepl port [spawn])</procedure>
    1313
    14 Listen to TCP port <port> and wait for incoming connections, doing
    15 {{(spawn thunk)}} for each. {{spawn}} defaults to {{thread-start!}}.
     14Listen to TCP port {{port}} and (blockingly) wait for incoming
     15connections.  {{(spawn in out)}} is called for each incomming
     16connection, where {{in}} is the input port and {{out}} is the output port
     17for the new TCP session.
     18
     19You can use {{spawn}}, for example, for authentication:
     20
     21<enscript highlight="scheme">   
     22(nrepl 1234
     23       (lambda (in out)
     24         (thread-start! ;; otherwise accept-loop will be blocked
     25          (lambda ()
     26            (display ";; please enter an accept token: " out)
     27            (define token (read-line in))
     28            (if (equal? token "abc")
     29                (nrepl-loop in out)
     30                (begin (display ";; access denied\n" out)
     31                       (close-input-port in)
     32                       (close-output-port out)))))))
     33</enscript>
     34
     35blockquoteYou can use {{(tcp-addresses in)}} and {{(tcp-port-numbers in)}} to find
     36out where the new session is coming from.
     37
     38{{nrepl}} will loop for accepting incomming connections unless {{spawn}}
     39returns {{#f}}.
    1640
    1741
    18 === Example
    19 At the very beginning of your application, you can get network REPL
    20 access by doing this:
     42=== Practical use
     43Any source-code you send down a {{nrepl}} session will not be persisted
     44anywhere.  You can reset your program state by restarting your program
     45which may be useful sometimes.
     46
     47
     48==== Terminal users
     49Editing code directly from {{nc localhost 1234}} isn't
     50pleasant. Luckily, [[http://freecode.com/projects/rlwrap|rlwrap]] works along {{nrepl}} to improve this
     51experience:
     52
     53<enscript highlight="bash">   
     54 âž€ rlwrap nc localhost 1234
     55;; nrepl on (csi -s example.scm)
     56#;1> (define (hello) (print "this will be in my history"))
     57</enscript>
     58
     59[[http://freecode.com/projects/rlwrap|rlwrap]] will also save your read-line history for the next invokation
     60{{rlwrap nc localhost 1234}} which is handy!
     61
     62
     63==== [[https://www.gnu.org/software/emacs/|Emacs]] users
     64{{nrepl}} plays very nicely with [[https://www.gnu.org/software/emacs/|Emacs]]! If you're used to running {{M-x
     65run-scheme}} and sending source-code from buffers into your REPL, an
     66{{nrepl}} endpoint can be used as a Scheme "interpreter". Specify that
     67you want to run {{nc localhost 1234}} for your Scheme interpreter
     68instead of the usual {{csi}}, and you get the functionality you're used
     69to.
     70
     71Note that telling [[https://www.gnu.org/software/emacs/|Emacs]] that {{nc localhost 1234}} is your Scheme
     72interpreter is tricky because {{C-u M-x run-scheme}} will not let you
     73enter spaces. This can be solved by pressing {{C-q}} before pressing
     74space.
     75
     76
     77==== Example HTTP-server work-flow
     78A real-world use-case for {{nrepl}} might be something like the
     79following. Let's make a simple hello-world HTTP server using [[http://api.call-cc.org/doc/spiffy|spiffy]].
    2180
    2281<enscript highlight="scheme">   
     82(use spiffy nrepl)
     83
     84(define (app c)
     85  (send-response body: "hello world\n"))
     86
     87(thread-start!
     88 (lambda ()
     89   (vhost-map `((".*" . ,(lambda (c) (app c)))))
     90   (start-server)))
     91
     92(print "starting nrepl on port 1234")
     93(nrepl 1234)
     94</enscript>
     95
     96Now spiffy runs on port {{8080}}:
     97
     98<enscript highlight="bash">   
     99 âž€ curl localhost:8080
     100hello world
     101</enscript>
     102
     103What's nice about this is that, since {{app}} is a top-level variable,
     104it can be replaced from the REPL:
     105
     106<enscript highlight="bash">   
     107 âž€ nc localhost 1234
     108;; nrepl on (csi -s example.scm)
     109#;1> (define (app c) (send-response body: "repl hijack!\n"))
     110#;1> ^C
     111</enscript>
     112
     113Now {{spiffy}} will use our top-level {{app}} for its proceeding requests:
     114
     115<enscript highlight="bash">   
     116 âž€ curl localhost:8080
     117repl hijack!
     118</enscript>
     119
     120Note that {{app}} must be wrapped in a {{lambda}} for this to work,
     121because the REPL can only replace top-level variable definitions.
     122
     123The implications of this can be quite dramatic in terms of
     124work-flow. If you write your app in a REPL-friendly way like this, you
     125can modify you program behaviour on-the-fly from the REPL and never
     126have to restart your process and lose its state.
     127
     128
     129==== Example CPU-intensive main thread
     130{{nrepl}} can be used for live-coding interactive application such as
     131games. Adding {{(thread-start! (lambda () (nrepl 1234)))}} usually Just
     132Works, where you can redefine top-level function and game state
     133on-the-fly.
     134
     135However, if the game-loop is eating up a lot of scheduler-time, you
     136may find that your REPL becomes unresponsive. A good way to fix this
     137is to wrap both the REPL and the game-loop in a mutex. This has
     138another advantage in that it will ensure your REPL will not interfere
     139with game-state (or OpenGL state) during game-loop iteration.
     140
     141<enscript highlight="scheme">   
     142;;; wrapping nrepl eval in a mutex for responsiveness
     143;;; and game-loop thread-safety. running this and then doing:
     144;;;     echo '(thread-sleep! 1)' | rlwrap nc localhost 1234
     145;;; should pause the game-loop for 1 second
    23146(use nrepl)
    24 (define repl-thread (thread-start! (lambda () (nrepl 1234))))
     147
     148(define with-main-mutex
     149  (let ((main-mutex (make-mutex)))
     150    (lambda (proc)
     151      (dynamic-wind (lambda () (mutex-lock! main-mutex))
     152                    proc
     153                    (lambda () (mutex-unlock! main-mutex))))))
     154
     155(thread-start!
     156 (lambda ()
     157   (nrepl 1234
     158          (lambda (i o)
     159            (thread-start!
     160             (lambda ()
     161               (nrepl-loop
     162                i o (lambda (x) (with-main-mutex (lambda () (eval x)))))))))))
     163
     164(define (game-step)
     165  (print* "\r"  (current-milliseconds) "   ")
     166  (thread-sleep! 0.05))
     167
     168(let loop ()
     169  (with-main-mutex game-step)
     170  (loop))
    25171</enscript>
    26172
    27173
     174==== {{nrepl}} in compiled code
     175{{nrepl}} also works inside a compiled program. However, sometimes
     176[[http://api.call-cc.org/doc/chicken/modules|modules]] disappear due to compiler optimizations.
     177
     178
     179==== {{nrepl}} on Android
     180{{nrepl}} has been used successfully on Android target hardware for
     181remote interactive development.  Check
     182out [[https://github.com/chicken-mobile/chicken-android-template|this]]
     183Android example project.
     184
     185
    28186=== Source code repository
    29 You can find the source [[https://github.com/Adellica/chicken-nrepl|here]].
     187You can find the
     188source [[https://github.com/Adellica/chicken-nrepl|here]].
    30189
    31190
     
    36195=== License
    37196BSD
     197
Note: See TracChangeset for help on using the changeset viewer.