source: scenarios/GerminationX/oak/src/oak/io.clj @ 898

Revision 898, 3.6 KB checked in by dave, 10 years ago (diff)

started fatima code and remote companion sprite update

Line 
1;; Copyright (C) 2010 FoAM vzw
2;; This program is free software: you can redistribute it and/or modify
3;; it under the terms of the GNU Affero General Public License as
4;; published by the Free Software Foundation, either version 3 of the
5;; License, or (at your option) any later version.
6;;
7;; This program is distributed in the hope that it will be useful,
8;; but WITHOUT ANY WARRANTY; without even the implied warranty of
9;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10;; GNU Affero General Public License for more details.
11;;
12;; You should have received a copy of the GNU Affero General Public License
13;; along with this program.  If not, see <http://www.gnu.org/licenses/>.
14
15(ns oak.io
16  (:use
17   clojure.xml
18   clojure.contrib.io)
19  (:import
20   java.io.File
21   java.io.FileInputStream
22   java.io.FileOutputStream
23   java.io.PushbackReader
24   java.io.FileReader
25   java.io.BufferedReader
26   java.io.InputStreamReader
27   java.net.Socket
28   java.nio.ByteBuffer
29   java.nio.CharBuffer
30   java.nio.channels.SocketChannel
31   java.nio.charset.Charset
32   java.io.IOException
33   java.io.ByteArrayInputStream))
34
35(defn parse-number [s]
36  (try (Integer/parseInt (.trim s))
37       (catch NumberFormatException e nil)))
38
39;(defn msg-waiting? [socket]
40;  (> (. (. socket getInputStream) available) 0))
41
42; (defn read-msg [socket]
43;  (defn read-bytes [rdr count]
44;   (let [result (. rdr read)]
45;     (if (= count 1)
46;       '()
47;       (cons (char result) (read-bytes rdr (- count 1))))))
48; (apply str (read-bytes
49;             (. socket getInputStream)
50;             (. (. socket getInputStream) available))))
51
52;(defn send-msg [socket msg]
53; (println msg)
54; (try
55;   (let [aux (str msg "\n")
56;         out (. socket getOutputStream)]
57;     (. out write (. aux getBytes "UTF-8"))
58;     (. out flush)
59;     true)
60;   (catch IOException e
61;     (. e printStackTrace)
62;     false)))
63
64(comment
65(defn serialise
66  "Print a data structure to a file so that we may read it in later."
67  [data-structure #^String filename]
68  (with-out-writer
69    (java.io.File. filename)
70    (binding [*print-dup* true] (prn data-structure))))
71
72;; This allows us to then read in the structure at a later time, like so:
73(defn deserialise [filename]
74  (with-open [r (PushbackReader. (FileReader. filename))]
75    (read r))))
76
77(defn serialise [o filename]
78  (with-open [outp (-> (java.io.File. filename) java.io.FileOutputStream. java.io.ObjectOutputStream.)]
79    (.writeObject outp o)))
80
81(defn deserialise [filename]
82  (with-open [inp (-> (java.io.File. filename) java.io.FileInputStream. java.io.ObjectInputStream.)]
83    (.readObject inp)))
84
85(comment (def buf (ByteBuffer/allocateDirect 4096))
86
87(defn read-msg [sc]
88  (.clear buf)
89  (let [r (.readLine sc buf)]
90    (if (> r 0)
91      (do
92        (.flip buf)
93        (let [bytearr (byte-array (.remaining buf))]
94          (.get buf bytearr)
95          (new String bytearr)))
96      false))))
97
98(defn read-msg [reader]
99  (let [r (.readLine reader)]
100    ;(println "<-----------------" r)
101    r))
102
103(defn send-msg [sc msg]
104  ;(println "-----------------------> sending:" msg)
105  (let [msg (str msg "\n")
106        enc (.newEncoder (Charset/forName "US-ASCII"))] 
107    (.write sc (.encode enc (CharBuffer/wrap msg)))))
108
109(defn load-object [fname]
110  (reduce
111   (fn [r t]
112     (let [toks (.split t " ")]
113       (assoc r (first toks) (second toks))))
114   {"name" fname}
115   (.split (slurp (str "data/objects/" fname ".txt")) "\r\n")))
116
117; for each object, look for a file and parse it into a hash-map
118(defn load-objects [objects]
119  (map
120   (fn [obj]
121     (load-object obj))
122   objects))
123
124(defn parse-xml [str]
125  (parse (ByteArrayInputStream. (.getBytes str "UTF-8"))))
Note: See TracBrowser for help on using the repository browser.