Zhang Node, REPL Start-up, and Zhang Process Table
About ↟
The Zhang Agent is intended to perform several tasks for Zhang nodes:
- Start up a JInterface node (in the same way that an Erlang shell or an LFE REPL when the BEAM is started in distributed mode, e.g.,
-sname mynode
) - Create a process-tracking table
- Provide functions for maintaining processes in the process table, including querying the table
- Additionally, it closes the splash image that Clojang displays (if running in GUI mode)
Usage ↟
To use the agent, update your project.clj
(either top-level or one of your profiles) to include the following:
...
:dependencies [
[clojang/zhang "0.1.0-SNAPSHOT"]
[clojang/zhang-agent "0.2.0-SNAPSHOT"]
...]
:jvm-opts ["-Dnode.sname=zhang"]
:java-agents [[clojang/zhang-agent "0.2.0-SNAPSHOT"]]
:aot [zhang.agent.startup]
...
In your project's REPL, you can then do something like the following:
(require '[zhang.process :as process]
'[zhang.process.table :as process-table])
(defn counter
[cnt msg]
(case (:type msg)
:inc (partial counter (inc cnt))
:get (do (process/! (:to msg) cnt)
(partial counter cnt))))
(defn printer
[msg]
(println "Got:" msg)
printer)
(def printer-process (process/spawn printer))
(def counter-process (process/spawn (partial counter 0)))
Now that you've spawned some processes, list them:
zhang.dev=> (process-table/ls)
total 2
| :id | :fun | :chan |
|---------------------+-------------------------+----------------------------------------------------|
| <hostname:123:1234> | zhang.dev$printer$ptr | clojure.core.async.impl.channels.ManyToManyChannel |
| <hostname:234:2345> | clojure.core$partial$fn | clojure.core.async.impl.channels.ManyToManyChannel |
:ok
There is also a process-table/get-all
function that will return the process
table data structure. Alternatively, to get the data for a single process:
(process-table/lookup "<hostname:123:1234>")
And, of course, you can use your functions by sending them messages:
zhang.dev=> (process/! counter-process {:type :get :to printer-process})
true
Got:
0
zhang.dev=> (process/! counter-process {:type :inc})
true
zhang.dev=> (process/! counter-process {:type :get :to printer-process})
Got: 1
true
zhang.dev=> (process/! counter-process {:type :inc})
true
zhang.dev=> (process/! counter-process {:type :inc})
true
zhang.dev=> (process/! counter-process {:type :inc})
true
zhang.dev=> (process/! counter-process {:type :get :to printer-process})
Got: 4
true
Finally, termination:
zhang.dev=> (process/terminate counter-process)
:terminated
zhang.dev=> (process-table/ls)
total 1
| :id | :fun | :chan |
|---------------------+-------------------------+----------------------------------------------------|
| <hostname:123:1234> | zhang.dev$printer$ptr | clojure.core.async.impl.channels.ManyToManyChannel |
:ok
zhang.dev=> (process/terminate printer-process)
:terminated
zhang.dev=> (process-table/ls)
total 0
:ok
If you are sure you want to remove all processes from the table, you can use this method (useful for resetting state during development):
zhang.dev=> (process-table/remove-all)
Documentation ↟
The zhang-agent API reference is available here:
License ↟
Copyright © 2016-2017 Duncan McGreggor
Distributed under the Apache License Version 2.0.