-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsession.rkt
99 lines (82 loc) · 3.41 KB
/
session.rkt
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
;; The MIT License (MIT)
;;
;; Copyright (c) 2013 Matthew C. Jadud
;;
;; Permission is hereby granted, free of charge, to any person obtaining a copy
;; of this software and associated documentation files (the "Software"), to deal
;; in the Software without restriction, including without limitation the rights
;; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
;; copies of the Software, and to permit persons to whom the Software is
;; furnished to do so, subject to the following conditions:
;;
;; The above copyright notice and this permission notice shall be included in
;; all copies or substantial portions of the Software.
;;
;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
;; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
;; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
;; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
;; THE SOFTWARE.
#lang racket
(require net/url)
(require "seq.rkt"
"debug.rkt"
"util.rkt"
"response-handling.rkt")
(provide session%)
(define session%
(class object%
;; Need to provide these
(init [host "127.0.0.1"]
[port 9000])
;; These default to false
(field [id false]
[conf false]
[board false]
[compilation-result false]
)
(define/public (get-id) id)
(define/public (get-compilation-result)
compilation-result)
(define (start-session)
;; Create a new process object
(define p (new process% [context 'SESSION-START]))
;; Define a sequence of operations
(seq p
;; We should be in the initial state, and flag a generic error
;; in the event of problems.
[(initial? 'ERROR)
(debug 'START-SESSION "DEFAULT ERROR: ~a" (send p to-string))
(debug 'START-SESSION "SERVER URL: ~a"
(url->string
(make-server-url host port "start-session")))
;; Nothing should change as a result of this operation
NO-CHANGE]
;; We should still be in the initial state, and should
;; flag a bad connection if all goes wrong.
[(initial? 'ERROR-NO-CONNECTION)
(get-pure-port (make-server-url host port "start-session"))]
;; Now we should have a port, and flag a bad response if
;; things go pear shaped.
[(port? 'ERROR-PROCESS-RESPONSE)
(debug 'START-SESSION "PORT: ~a" (send p to-string))
(process-response (send p get))]
;; The response should give us a hash table; we'll pull
;; out the session ID.
[(hash? 'ERROR-BAD-RESPONSE)
(debug 'START-SESSION "RESPONSE: ~a" (send p to-string))
(hash-ref (send p get) 'sessionid)]
;; The session ID should be a symbol. We're just displaying
;; it as a debug here, so this step should yield no changes.
[(string? 'ERROR-SESSION-ID-NOT-A-STRING)
(debug 'START-SESSION "SESSION ID: ~a" (send p get))
NO-CHANGE])
;; Set the id according to what we retrieved
(set! id (send p get))
)
(define (init)
(start-session))
(super-new)
(init)))