Skip to content
Browse files

small improvements

  • Loading branch information...
1 parent 7e79d4b commit bde795ed88d4eb6f1471246eaf45873f072f71cb @antoniogarrote committed May 26, 2010
Showing with 42 additions and 16 deletions.
  1. +15 −5 src/plaza/core.clj
  2. +7 −4 src/plaza/examples/chat.clj
  3. +3 −7 src/plaza/triple_spaces/server/rabbit.clj
  4. +12 −0 src/plaza/utils.clj
  5. +5 −0 test/plaza/utils_test.clj
View
20 src/plaza/core.clj
@@ -3,28 +3,38 @@
[plaza.triple-spaces server]
[plaza.triple-spaces core]
[plaza.rdf core]
- [plaza.rdf.implementations jena])
+ [plaza utils]
+ [plaza.rdf.implementations jena]
+ [plaza.examples chat])
(:gen-class :main true))
+(defn cmd-params-to-hash
+ ([args]
+ (apply hash-map (map cmd-param-to-keyword args))))
(defn start-triple-space [& args]
-
(init-jena-framework)
-
(let [name (first args)
port (Integer/parseInt (second args))
- args (apply array-map (rest (rest args)))]
+ args (apply array-map (cmd-params-to-hash (rest (rest args))))]
(apply plaza.triple-spaces.server/start-triple-server (cons name (cons port (cons (build-model :jena) args)) ))))
+(defn start-example [example-name & args]
+ (init-jena-framework)
+ (condp = example-name
+ "chat" (apply start-app (list (first args) (cmd-params-to-hash (rest args))))))
+
(defn process-command [command & args]
(condp = command
"start-triple-space" (apply start-triple-space args)
+ "examples" (apply start-example args)
:else (println (str "unknown command" command))))
(defn show-help []
(println "plaza-standalone syntax: java -jar plaza-standalone.jar COMMAND [ARGS]")
- (println "COMMAND: start-triple-space name port [:username rabbit-username :password rabbit-password :host rabbit-host :port rabbit-port :virtual-host rabbit-vh]"))
+ (println "COMMAND: start-triple-space name port [-username rabbit-username -password rabbit-password -host rabbit-host -port rabbit-port -virtual-host rabbit-vh]")
+ (println "COMMAND: examples chat nick [-ts-host host -ts-port port -username rabbit-username -password rabbit-password -host rabbit-host -port rabbit-port -virtual-host rabbit-vh]"))
(defn -main
[& args]
View
11 src/plaza/examples/chat.clj
@@ -85,7 +85,7 @@
#(let [author (literal-value (o (first (filter (tc (predicate? (uri? nick))) (first %1)))))]
(println (str "*** agent user-logout - got triples: " %1))
(send set-userlist-text (fn [ta] (let [old-text (clojure.contrib.str-utils2/split-lines (.getText ta))
- new-text (reduce (fn [ac nick] (if (= nick author) ac (str ac nick "\r\n"))) "" old-text)]
+ new-text (reduce (fn [ac some-nick] (if (not (= (.indexOf some-nick author) -1)) ac (str ac some-nick "\r\n"))) "" old-text)]
(.setText ta new-text) ta))))))
;; GUI
@@ -131,7 +131,6 @@
(.setLayout (BorderLayout. 0 1))
(.add chatArea BorderLayout/CENTER)
(.add bottomPanel BorderLayout/PAGE_END)
- ; (.setDefaultCloseOperation JFrame/EXIT_ON_CLOSE)
(.setResizable false)
(.setSize 417 318)
(.setVisible true))
@@ -148,11 +147,15 @@
(.setSize 200 250)
(.setVisible true))))
-(defn start-app [user-nick]
+(defonce *default-args* {:ts-host "localhost" :ts-port "7555" :host "localhost" :port "5672" :username "guest" :password "guest"})
+
+(defn start-app [user-nick orig-args]
;; setup the app
(send set-chat-text #(do (.setText %1 "") %1))
(dosync (ref-set *login* user-nick))
- (start-connection "localhost" 7555 "localhost" 5672 "guest" "guest")
+ (let [final-args (check-default-values orig-args *default-args*)]
+
+ (start-connection (:ts-host final-args) (Integer/parseInt (:ts-port final-args)) (:host final-args) (Integer/parseInt (:port final-args)) (:username final-args) (:password final-args)))
;; we insert our user in the triplespace and load the initial list of users
(out (ts :chat) [[(str "http://plaza.org/examples/chat/users/" @*login*) rdf:type user]
View
10 src/plaza/triple_spaces/server/rabbit.clj
@@ -3,7 +3,8 @@
;; @date 22.05.2010
(ns plaza.triple-spaces.server.rabbit
- (:use [clojure.contrib.logging :only [log]])
+ (:use [clojure.contrib.logging :only [log]]
+ [plaza utils])
(:require [clojure.contrib.string :as string])
(:import [java.util.concurrent LinkedBlockingQueue]
[com.rabbitmq.client
@@ -27,16 +28,11 @@
([new-parameters]
(alter-var-root #'*default-rabbit-parameters* (fn [_] new-parameters))))
-(defn check-default-values
- "Adds missing values from the default-rabbit-parameters map"
- ([opts] (merge-with #(if (nil? %2) %1 %2) *default-rabbit-parameters* opts))
- ([opts orig] (merge-with #(if (nil? %2) %1 %2) orig opts)))
-
(defn connect
"Connects to a RabbitMQ server.
Args: :username :password :host :port :virtual-host"
([& args]
- (let [{:keys [username password virtual-host port host]} (check-default-values (apply array-map args))
+ (let [{:keys [username password virtual-host port host]} (check-default-values (apply array-map args) *default-rabbit-parameters*)
#^ConnectionParameters params (doto (new ConnectionParameters)
(.setUsername username)
(.setPassword password)
View
12 src/plaza/utils.clj
@@ -102,3 +102,15 @@
(defn thread-id
"Returns the ID of the current thread"
([] (.getId (Thread/currentThread))))
+
+
+(defn cmd-param-to-keyword
+ "Transforms a command line argument (-something) into a keyword (:something)"
+ ([atom]
+ (if (keyword? atom)
+ atom
+ (if (.startsWith atom "-") (keyword (.substring atom 1)) atom))))
+
+(defn check-default-values
+ "Adds missing values from the default-rabbit-parameters map"
+ ([opts orig] (merge-with #(if (nil? %2) %1 %2) orig opts)))
View
5 test/plaza/utils_test.clj
@@ -14,3 +14,8 @@
(let [to-flatten (with-meta [:a [[:s :p :o] [:s :p :o]] :c [[:s :p :o]] :d] {:flatten true})
flattened (flatten-1 to-flatten)]
(is (= (meta to-flatten) (meta flattened)))))
+
+(deftest test-cmd-param-to-keywords
+ (is (= "hola" (cmd-param-to-keyword "hola")))
+ (is (= :hola (cmd-param-to-keyword :hola)))
+ (is (= :hola (cmd-param-to-keyword "-hola"))))

0 comments on commit bde795e

Please sign in to comment.
Something went wrong with that request. Please try again.