Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Setup base computer list functions.

  • Loading branch information...
commit e5354a5c8bbb691d7b5139c729ec75b355374c01 1 parent 5ae7b0e
jwinnenb authored
4 src/kh/client/checksum.clj
@@ -17,8 +17,8 @@
17 17 ;;
18 18
19 19 (ns kh.client.checksum
20   - (:use clojure.contrib.io)
  20 + (:use [clojure.contrib.io :only (input-stream file)])
21 21 (:import org.apache.commons.codec.digest.DigestUtils))
22 22
23 23 (defn checksum [cookie path]
24   - (DigestUtils/md5Hex (input-stream (file path))))
  24 + (DigestUtils/md5Hex (input-stream (file path))))
2  src/kh/client/download.clj
@@ -17,7 +17,7 @@
17 17 ;;
18 18
19 19 (ns kh.client.download
20   - (:use clojure.contrib.io))
  20 + (:use [clojure.contrib.io :only (input-stream file)]))
21 21
22 22
23 23 (defn download [cookie path]
2  src/kh/client/upload.clj
@@ -17,7 +17,7 @@
17 17 ;;
18 18
19 19 (ns kh.client.upload
20   - (:use clojure.contrib.duck-streams))
  20 + (:use [clojure.contrib.io :only (copy file-str)]))
21 21
22 22 (defn upload [file dst name]
23 23 (copy (:tempfile file) (file-str dst name)))
19 src/kh/common/computer.clj
... ... @@ -0,0 +1,19 @@
  1 +(ns kh.common.computer
  2 + (:use kh.common.sql))
  3 +
  4 +(defn add-computer [name ip]
  5 + (c "insert khcomputer set cname='%s',cip='%s'" name ip))
  6 +(defn delete-computer [name]
  7 + (c "delete from khcomputer where cname='%s'" name))
  8 +(defn get-computer [name]
  9 + (q "select * from fhcomputer where cname='%s'" name))
  10 +(defn list-computers []
  11 + (q "select * from khcomputer"))
  12 +(defn rename-computer [name new-name]
  13 + (c "update computer set cname='%s' where cname='%s'" new-name name))
  14 +(defn change-computer-ip [name new-ip]
  15 + (c "update computer set cip='%s' where cname='%s'" new-ip name))
  16 +(defn computer-on [name]
  17 + (c "update computer set cavail='1' where cname='%s'" name))
  18 +(defn computer-off [name]
  19 + (c "update computer set cavail='0' where cname='%s'" name))
10 src/kh/common/html.clj
... ... @@ -0,0 +1,10 @@
  1 +(ns kh.common.html
  2 + (:use [hiccup core page-helpers]))
  3 +
  4 +(defn default-page [title & body]
  5 + (html
  6 + [:head
  7 + [:title title]]
  8 + [:body
  9 + [:h1 title]
  10 + body]))
29 src/kh/common/sql.clj
@@ -28,24 +28,27 @@
28 28 (def db-ref (ref {}))
29 29
30 30 (defn con-db
31   - "Pushes the specified user information in to the db ref variable, simply provides the connection information, but doesn't really connect to the DB."
32   - [user pass host db]
33   - (dosync
34   - (ref-set db-ref
35   - {:classname "com.mysql.jdbc.Driver"
36   - :subprotocol "mysql"
37   - :subname (format "//%s:3306/%s" host db)
38   - :user user
39   - :password pass})))
  31 + "Pushes the specified user information in to the db ref variable, simply provides the connection information, but doesn't really connect to the DB. Currently operates on MySQL and Derby."
  32 + ([path]
  33 + (dosync
  34 + (ref-set db-ref
  35 + {:classname "org.apache.derby.jdbc.EmbeddedDriver"
  36 + :subprotocol "derby"
  37 + :subname path
  38 + :create true})))
  39 + ([user pass host db]
  40 + (dosync
  41 + (ref-set db-ref
  42 + {:classname "com.mysql.jdbc.Driver"
  43 + :subprotocol "mysql"
  44 + :subname (format "//%s:3306/%s" host db)
  45 + :user user
  46 + :password pass}))))
40 47
41 48 (defn escape-sql
42 49 "A simple wrapper for Apache Commons escapeSQL function."
43 50 [s] (StringEscapeUtils/escapeSql s))
44 51
45   -(defn unescape-sql
46   - "A simple wrapper for Apache Commons unescapeSQL function."
47   - [s] (StringEscapeUtils/unescapeSQL s))
48   -
49 52 (defn q
50 53 "Sends query string s to the database specified by db and returns the result."
51 54 ([#^String q-str & rest]
24 src/kh/server/computer_list.clj
... ... @@ -0,0 +1,24 @@
  1 +(ns kh.server.computer-list
  2 + (:use [hiccup core page-helpers form-helpers]
  3 + [kh.common computer html]))
  4 +
  5 +(defn computer-list-page []
  6 + (default-page
  7 + "Computer List"
  8 + (html
  9 + [:table
  10 + [:tr
  11 + [:th "State"]
  12 + [:th "Name"]
  13 + [:th "IP"]
  14 + [:th "Control"]]
  15 + (mapq
  16 + (fn [cell]
  17 + (html
  18 + [:tr
  19 + [:td (:cavail cell)]
  20 + [:td (:cname cell)]
  21 + [:td (:cip cell)]
  22 + [:td (link-to (str "https://" (:cip cell) ":8999/client") "Control")]]))
  23 + "select * from khcomputer")])))
  24 +
3  src/kh/server/core.clj
@@ -18,11 +18,12 @@
18 18
19 19 (ns kh.server.core
20 20 (:use [compojure core route]
  21 + [kh.server login-page]
21 22 ring.adapter.jetty))
22 23
23 24 (defroutes server-core
24 25 (GET "/" [] nil)
25   - (GET "/login" [user pass] nil)
  26 + (GET "/login" [user pass] (login-page user pass))
26 27 (GET "/user/new" [user pass] nil)
27 28 (GET "/user/list" [] nil)
28 29 (GET "/user/edit" [] nil)
17 src/kh/server/login_page.clj
@@ -18,24 +18,17 @@
18 18
19 19 (ns kh.server.login-page
20 20 (:use
21   - [clojure.contrib string]
  21 + [clojure.contrib.string :only (blank?)]
22 22 [hiccup core form-helpers]
23   - [kh.common user))
  23 + [kh.common html user]))
24 24
25   -(defn default-page [title & body]
26   - (html
27   - [:head
28   - [:title title]]
29   - [:body
30   - [:h1 title]
31   - body]))
32 25
33 26 (defn login-page [user pass]
34 27 (cond
35 28 (good-pass? user pass) (let [cookie (str user (new-cookie user))]
36   - ;; Send the cookie.
37   - ;; Forward to comp/list
38   - )
  29 + {:headers {"Set-Cookie" "khcookie=%s"
  30 + "Location" "/comp/list"}
  31 + :status 302})
39 32 (or (blank? user)
40 33 (blank? pass)) (default-page
41 34 "Login"
7 src/sql/kh.sql
@@ -4,3 +4,10 @@ create table khuser (
4 4 khpass varchar (40),
5 5 khcookie varchar (40)
6 6 );
  7 +
  8 +drop table if exists khcomputer;
  9 +create table khcomputer (
  10 + cname varchar (255) primary key,
  11 + cip int (4),
  12 + cavail int(1)
  13 +);

0 comments on commit e5354a5

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