summaryrefslogtreecommitdiff
path: root/src/playground.lisp
blob: beab166b983c064996239906a67faa74325cb68d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10 -*-

;; Copyright 2009, NORDUnet A/S.
;;
;; This file is part of Eduroam-stats.
;;
;; Eduroam-stats is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 2 of the License, or
;; (at your option) any later version.
;;
;; Eduroam-stats is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with Eduroam-stats.  If not, see <http://www.gnu.org/licenses/>.


;;;; Parsing XML.
(require 'cxml)
(require 'usocket)
(require 'cl-xmlspam)
;; 
(klacks:with-open-source (s (cxml:make-source #p"s2.xml"))
  (loop
     for key = (klacks:peek s)
     while key
     do
       (case key
	 (:start-element
	  (format t "~A {" (klacks:current-qname s)))
	 (:end-element
	  (format t "}")))
       (klacks:consume s)))
;; 
(klacks:with-open-source (s (cxml:make-source
			     (usocket:socket-stream (usocket:socket-connect
						     "victoria.tug.nordu.net"
						     50001
						     :element-type '(unsigned-byte 8)))
			     :buffering nil))
  (loop
     for key = (klacks:peek-next s)
     while key
     do
       (case key
	 (:start-document (format t "	"))
	 (:start-element (format t "~A {" (klacks:current-qname s)))
	 (:end-element (format t "}"))
	 (:end-document (format t "~%")))
       (klacks:consume s)))
;; 
(use-package 'xspam)
(defparameter *ns* '((urn . "urn:ietf:params:xml:ns:xfb-0.1")))
(defun get-entry-xspam (sock)
  (with-xspam-source (make-xspam-source sock :buffering nil)
    (optional
     (element :bgp_message ;;|bgp_message|
       (text (format t "~A~%" _))))))
;;
(defun klacks-it (arr)
  (let ((s (cxml:make-source arr)))
    (loop
       for key = (klacks:peek s)
       while key 
       do
	 (case key
	   ;(:start-document (format t "*** start *** "))
	   ;(:end-document (format t "*** end ***~%"))
	   (:start-element (format t "~A {" (klacks:current-qname s)))
	   (:end-element (format t "}~%"))
	   (:characters (format t (klacks:current-characters s))))
	 (klacks:consume s))))

(defun klacks-it2 (arr)
  (let ((s (cxml:make-source arr)))
    (do ((key (klacks:peek s) (klacks:peek s)))
	((null key))
      (case key
	   ;(:start-document (format t "*** start *** "))
	   ;(:end-document (format t "*** end ***~%"))
	(:start-element (format t "~A {" (klacks:current-qname s)))
	(:end-element (format t "}~%"))
	(:characters (format t (klacks:current-characters s))))
      (klacks:consume s))))

;; Read stream from socket.
(let ((sock (usocket:socket-connect
	     "victoria.tug.nordu.net"
	     50001
	     :element-type '(unsigned-byte 8))))
  (loop for i from 1 to 5
     do
       (format t "~A: " i)
       (f (usocket:socket-stream sock))
       (format t "~%"))
  (format t "Closing socket.~%")
  (usocket:socket-close sock))

(defun play ()
  (let ((reader (new-reader "victoria.tug" 50001))
	(xmls-builder (cxml-xmls:make-xmls-builder)))
    (do ((e (next-xml-blurb reader "BGP_MESSAGE")
	    (next-xml-blurb reader "BGP_MESSAGE")))
	((null e))
      (print (cxml:parse e xmls-builder)))
    (close-reader)))
;;
(next-xml-blurb (new-reader "victoria.tug.nordu.net" 50001) "BGP_MESSAGE")

;;;; db
;; To connect to database, wrap your db call(s) in WITH-CONNECTION,
;; passing a spec on the form '(database user password host), see
;; http://common-lisp.net/project/postmodern/postmodern.html.

;; SQL
"select * from bgp_message where timestamp != 0 order by id desc limit 3;"

;; postmodern, lower level
;; http://common-lisp.net/project/postmodern/postmodern.html#querying
(query "select count(*) from bgp_message" :single)
(doquery (:select 'timestamp 'prefix 'label :from 'bgp-message) 
    (ts pref lbl)
  (format t "~A ~A ~A~%" ts pref lbl))

;; postmodern, higher level
;; http://common-lisp.net/project/postmodern/postmodern.html#tabledef
(dolist (m (select-dao 'bgp-message (:= 'label "NANN")))
  (format t "~A ~A ~A~%" (timestamp m) (prefix m) (label m)))

(with-connection '("bgpstore" "bgpstore" "bgpstore" "localhost" )
	   (length (select-dao 'bgp-message (:= 'prefix "91.206.67.0/24"))))
(length (query (:limit (:select 'prefix 
				:from 'bgp-message 
				:where (:= 'prefix "91.206.67.0/24"))
		       10)))
(query-dao 'bgp-message (:limit (:select 'prefix 
					 :from 'bgp-message 
					 :where (:= 'prefix "91.206.67.0/24"))
				13))

(length (select-dao 'bgp-message))