Skip to content

Commit

Permalink
joining coders and workspace with cursor
Browse files Browse the repository at this point in the history
  • Loading branch information
prismofeverything committed Dec 11, 2011
1 parent be95ae4 commit d65efe9
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 25 deletions.
2 changes: 1 addition & 1 deletion project.clj
Expand Up @@ -14,6 +14,6 @@
:jvm-opts ["-agentlib:jdwp=transport=dt_socket,server=y,suspend=n"]
:main triceratops.core
:repositories {"sonatype-oss-public" "https://oss.sonatype.org/content/groups/public/"
;; "sonatype-oss-snapshots" "https://oss.sonatype.org/content/repositories/snapshots/"}
;; "sonatype-oss-snapshots" "https://oss.sonatype.org/content/repositories/snapshots/"}
}
)
56 changes: 32 additions & 24 deletions src/triceratops/core.clj
@@ -1,5 +1,6 @@
(ns triceratops.core
(:use [clojure.string :only (split join trim)]
(:use triceratops.debug
[clojure.string :only (split join trim)]
[cheshire.core :only (generate-string parse-string)]
[compojure.core :only (defroutes GET)]
[hiccup.core :only (html)]
Expand Down Expand Up @@ -40,7 +41,7 @@
(select-keys workspace [:name :coders]))

(defn coder-connect
"Registers the coder with the system based on the given request."
"Adheres the coder given by request to the channel ch."
[ch request]
(let [nick (keyword (request :nick))
color (keyword (request :color))
Expand All @@ -58,28 +59,37 @@
(defn add-coder-to-workspace
[workspace coder cursor]
(dosync
(alter workspaces assoc-in [(:name workspace) :cursors (:nick coder)] cursor)
(if ((:name workspace) @workspaces)
(alter workspaces assoc-in [(:name workspace) :cursors (:nick coder)] cursor)
(let [joined (assoc-in workspace [:cursors (:nick coder)] cursor)]
(alter workspaces assoc (:name workspace) joined)))
(alter coders assoc-in [(:nick coder) :cursors (:name workspace)] cursor)))

(defn remove-coder-from-workspace
[workspace coder]
(dosync
(alter workspaces update-in [(:name workspace) :cursors] #(dissoc % (:nick coder)))
(alter coders update-in [(:nick coder) :cursors] #(dissoc % (:name workspace)))))

(defn coder-join
"Joins the coder to the given workspace"
"Joins the coder to the given workspace."
[ch request]
(let [name (keyword (request :workspace))
(let [workspace-name (keyword (request :workspace))
nick (keyword (request :nick))
coder (-> @coders nick)
pos {:line 0 :ch 0}
color (-> @coders nick :color)
cursor (ref (Cursor. name nick pos color))
workspace (or (-> @workspaces name)
(Workspace. (permanent-channel) name "" {} [] {}))
coder-ch (fork (-> @coders nick :ch))
workspace-ch (fork (-> workspace :ch))
color (:color coder)
cursor (ref (Cursor. workspace-name nick pos color))
workspace (or (workspace-name @workspaces)
(Workspace. (permanent-channel) workspace-name "" {} [] {}))
coder-ch (fork (:ch coder))
workspace-ch (fork (:ch workspace))
process (map* (respond-to workspace-commands [coder-ch workspace-ch]) coder-ch)]
(siphon (filter* identity process) (-> workspace :ch))
(siphon workspace-ch (-> @coders nick :ch))
(siphon (filter* identity process) (:ch workspace))
(siphon workspace-ch (:ch coder))
(add-coder-to-workspace workspace coder cursor)
(encode {:op :join
:coder (clean-coder (@coders nick))
:coder (clean-coder coder)
:workspace (clean-workspace workspace)})))

(defn coder-say
Expand All @@ -88,11 +98,11 @@

(defn coder-cursor
[coder-ch workspace-ch request]
(let [name (keyword (request :workspace))
(let [workspace-name (keyword (request :workspace))
nick (keyword (request :nick))
coder (-> @coders nick)]
coder (nick @coders)]
(dosync
(alter (-> @coders nick :cursors name) #(request :cursor)))
(alter (-> @coders nick :cursors workspace-name) #(request :cursor)))
(encode request)))

(defn coder-change
Expand All @@ -102,17 +112,15 @@
(defn coder-leave
"Removes the given coder from the workspace"
[coder-ch workspace-ch request]
(let [name (keyword (request :workspace))
(let [workspace-name (keyword (request :workspace))
nick (keyword (request :nick))
workspace (-> @workspaces name)
removed (assoc workspace :coders (dissoc (-> workspace :coders) nick))]
coder (nick @coders)
workspace (workspace-name @workspaces)]
(remove-coder-from-workspace workspace coder)
(close coder-ch)
(close workspace-ch)
(encode {:op :leave :nick nick})))

(defn remove-coder-from-workspace
[]

(defn coder-disconnect
"Removes the given coder from the map and notifies all clients."
[ch request]
Expand All @@ -121,7 +129,7 @@
(dosync
(alter coders dissoc (keyword (request :nick))))
(close ch)
(encode request))
(encode request)))

(defn respond-to
[commands channels]
Expand Down
31 changes: 31 additions & 0 deletions src/triceratops/debug.clj
@@ -0,0 +1,31 @@
(ns triceratops.debug)

(defmacro debug [x]
`(let [x# ~x] (println "debug:" '~x " -> " x#) x#))

(defmacro log [j x]
`(let [x# ~x] (println (str ~(name j) ":") x#) x#))

(defmacro local-bindings
"Produces a map of the names of local bindings to their values."
[]
(let [symbols (map key @clojure.lang.Compiler/LOCAL_ENV)]
(zipmap (map (fn [sym] `(quote ~sym)) symbols) symbols)))

(declare ^:dynamic *locals*)
(defn eval-with-locals
"Evals a form with given locals. The locals should be a map of symbols to
values."
[locals form]
(binding [*locals* locals]
(eval
`(let ~(vec (mapcat #(list % `(*locals* '~%)) (keys locals)))
~form))))

(defmacro repl
"Starts a REPL with the local bindings available."
[]
`(clojure.main/repl
:prompt #(print "debug => ")
:eval (partial eval-with-locals (local-bindings))))

0 comments on commit d65efe9

Please sign in to comment.