Skip to content

Commit f31b825

Browse files
committed
Merge pull request #1 from shaolang/master
Make clj-dde more idiomatic
2 parents c2df6f5 + a15771d commit f31b825

File tree

4 files changed

+105
-113
lines changed

4 files changed

+105
-113
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
/checkouts
44
pom.xml
55
pom.xml.asc
6+
*.swp
67
*.jar
78
*.class
89
/.lein-*

README.md

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Your `project.clj` should include something like the following:
1818
(defproject foo "0.1.0"
1919
...
2020
:dependencies [[org.clojure/clojure "1.5.1"]
21-
[clj-dde "0.1.2"]]
21+
[clj-dde "0.2.0"]]
2222
...)
2323
```
2424

@@ -29,9 +29,21 @@ Work in Progress.
2929
When you need to connect with a DDE data source...
3030

3131

32-
`(dde/convesation)` to setup a connection.
33-
`(dde/connect "excel" "sheet1")` to connect to a datasource (excel)
34-
`(dde/request excel-conv "R1C1")` to 'poll' the data contained in cell R1C1
32+
```clojure
33+
(def excel-conv (dde/convesation)) ;; to setup a connection.
34+
(dde/connect excel-conv "excel" "sheet1") ;; to connect to a datasource (excel)
35+
(dde/request excel-conv "R1C1") ;; to 'poll' the data contained in cell R1C1
36+
```
37+
38+
The major difference between version 0.1.x and 0.2.x is that all functions
39+
that previously returned `nil` now returns the conversation object, thus
40+
making the functions chain-able:
41+
42+
```clojure
43+
(-> (dde/conversation)
44+
(dde/connect "excel" "sheet1")
45+
(dde/request "R1C1"))
46+
```
3547

3648
check out the dde-example folder for further examples and usage.
3749

project.clj

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
1-
(defproject clj-dde "0.1.2"
1+
(defproject clj-dde "0.2.0"
22
:description "clj-dde: for when you need to connect to a DDE (dynamic data exchange) source"
33
:url "http://github.com/tuddman/clj-dde"
44
:license {:name "Eclipse Public License"
55
:url "http://www.eclipse.org/legal/epl-v10.html"}
6-
:dependencies [[org.clojure/clojure "1.5.1"]]
7-
:java-source-paths ["src/java"])
8-
9-
10-
11-
12-
13-
6+
:java-source-paths ["src/java"]
7+
:profiles {:dev {:dependencies [[org.clojure/clojure "1.8.0"]]
8+
:jvm-opts ["-Djava.library.path=resources"]}})

src/clj_dde/core.clj

Lines changed: 84 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -1,100 +1,84 @@
1-
(ns clj-dde.core
2-
[:import
3-
[com.pretty_tools.dde.client DDEClientConversation]
4-
[com.pretty_tools.dde.client DDEClientEventListener]
5-
[com.pretty_tools.dde ClipboardFormat]
6-
[com.pretty_tools.dde DDEException]
7-
[com.pretty_tools.dde DDEMLException]])
8-
9-
#_(System/getProperty "java.library.path")
10-
11-
12-
13-
;; implement the DDEClientEventListener interface
14-
15-
(defn setEventListener
16-
[conv]
17-
(.setEventListener conv
18-
(reify DDEClientEventListener
19-
(onItemChanged [this topic item data]
20-
(prn (str "Item Changed: " topic " , " item " , " data )))
21-
(onDisconnect [this]
22-
(prn (str "onDisconnect() called."))))))
23-
24-
25-
(defn listen
26-
[conv]
27-
(.setEventListener conv
28-
(reify DDEClientEventListener
29-
(onItemChanged [this _ _ data]
30-
(data))
31-
(onDisconnect [this]
32-
(prn (str "onDisconnect() called."))))))
33-
34-
35-
(defn listen-and-print-data
36-
[conv]
37-
(.setEventListener conv
38-
(reify DDEClientEventListener
39-
(onItemChanged [this _ _ data]
40-
(prn (str data)))
41-
(onDisconnect [this]
42-
(prn (str "onDisconnect() called."))))))
43-
44-
45-
;; instantiate a DDEClientConversation
46-
47-
(defn conversation
48-
[]
49-
(DDEClientConversation.))
50-
51-
52-
;; Method Definitions for a DDEClientConversation. These are the 'direct' translations of java->clojure.
53-
;; TODO: define more 'idiomatic, syntactic sugar' usage methods.
54-
55-
(defn setTimeout
56-
[conv ms]
57-
(.setTimeout conv ms))
58-
59-
(defn getTimeout
60-
"returns timeout length in ms"
61-
[conv]
62-
(.getTimeout conv))
63-
64-
(defn connect
65-
[conv app topic]
66-
(.connect conv app topic))
67-
68-
(defn disconnect
69-
[conv]
70-
(.disconnect conv))
71-
72-
(defn startAdvice
73-
[conv item]
74-
(.startAdvice conv item))
75-
76-
(defn stopAdvice
77-
[conv item]
78-
(.stopAdvice conv item))
79-
80-
(defn poke
81-
[conv topic input]
82-
(.poke conv topic input))
83-
84-
(defn request
85-
[conv item]
86-
(.request conv item))
87-
88-
(defn execute
89-
[conv command]
90-
(.execute conv command))
91-
92-
(defn getService
93-
[conv]
94-
(.getService conv))
95-
96-
(defn getTopic
97-
[conv]
98-
(.getTopic conv))
99-
100-
1+
(ns clj-dde.core
2+
(:import [com.pretty_tools.dde.client DDEClientConversation
3+
DDEClientEventListener]
4+
[com.pretty_tools.dde ClipboardFormat
5+
DDEException
6+
DDEMLException]))
7+
8+
;; implement the DDEClientEventListener interface
9+
10+
(defn set-event-listener
11+
[conv & {:keys [on-disconnect on-item-changed]
12+
:or {on-disconnect (fn [_])
13+
on-item-changed (fn [_ _ _ _])}}]
14+
(doto conv
15+
(.setEventListener
16+
(reify DDEClientEventListener
17+
(onItemChanged [this topic item data] (on-item-changed this topic item data))
18+
(onDisconnect [this] (on-disconnect this))))))
19+
20+
(defn listen
21+
[conv]
22+
(set-event-listener conv
23+
:on-disconnect (fn [_] (prn (str "onDisconnect() called.")))))
24+
25+
(defn listen-and-print-data
26+
[conv]
27+
(set-event-listener conv
28+
:on-item-changed (fn [_ _ _ data] (prn (str data)))
29+
:on-disconnect (fn [_] (prn (str "onDisconnect() called.")))))
30+
31+
;; instantiate a DDEClientConversation
32+
33+
(defn conversation
34+
[]
35+
(DDEClientConversation.))
36+
37+
38+
;; Method Definitions for a DDEClientConversation. These are the 'direct' translations of java->clojure.
39+
;; TODO: define more 'idiomatic, syntactic sugar' usage methods.
40+
41+
(defn set-timeout
42+
[conv ms]
43+
(doto conv (.setTimeout ms)))
44+
45+
(defn get-timeout
46+
"returns timeout length in ms"
47+
[conv]
48+
(.getTimeout conv))
49+
50+
(defn connect
51+
[conv app topic]
52+
(doto conv (.connect app topic)))
53+
54+
(defn disconnect
55+
[conv]
56+
(doto conv .disconnect))
57+
58+
(defn start-advice
59+
[conv item]
60+
(doto conv (.startAdvice item)))
61+
62+
(defn stop-advice
63+
[conv item]
64+
(doto conv (.stopAdvice item)))
65+
66+
(defn poke
67+
[conv topic input]
68+
(doto conv (.poke topic input)))
69+
70+
(defn request
71+
[conv item]
72+
(.request conv item))
73+
74+
(defn execute
75+
[conv command]
76+
(doto conv (.execute command)))
77+
78+
(defn get-service
79+
[conv]
80+
(.getService conv))
81+
82+
(defn get-topic
83+
[conv]
84+
(.getTopic conv))

0 commit comments

Comments
 (0)