This repository has been archived by the owner on Jul 2, 2022. It is now read-only.
forked from MEAE-GOT/W3C_VehicleSignalInterfaceImpl
/
server.go
142 lines (114 loc) · 3.18 KB
/
server.go
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
package main
import (
"github.com/gorilla/websocket"
log "github.com/sirupsen/logrus"
"math/rand"
"net/http"
"strconv"
"strings"
)
// #include <stdlib.h>
// #include <stdio.h>
// #include <stdbool.h>
// #include "vssparserutilities.h"
import "C"
import "unsafe"
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
var nodeHandle C.long
func printInComingSession(r *http.Request) {
log.WithFields(log.Fields{
"Method": r.Method,
"Host":r.Host,
"Proto":r.Proto,
"Uri":r.RequestURI,
}).Info("http request fields")
log.WithFields(log.Fields{
"Header-Get":r.Header.Get("Upgrade"),
}).Trace(" upgraded to ")
}
func getSpeed() int {
return rand.Intn(250)
}
func getMatches(path string) int {
// call int VSSSimpleSearch(char* searchPath, long rootNode, bool wildcardAllDepths);
cpath := C.CString(path)
var matches C.int = C.VSSSimpleSearch(cpath, nodeHandle, false)
C.free(unsafe.Pointer(cpath))
return int(matches)
}
func wsClientSession(conn *websocket.Conn) {
defer conn.Close() // dispose when its all over
for {
// Read message from browser
msgType, msg, err := conn.ReadMessage()
if err != nil {
log.Error("read: ", err)
break
}
var matches int = getMatches(string(msg))
// log the message
log.Trace(" request: ", conn.RemoteAddr(), " ", string(msg))
// Write message back to browser
message := "Response:Nr of matches = " + strconv.Itoa(matches)
response := []byte(message)
err = conn.WriteMessage(msgType, response);
if err != nil {
log.Error("write:", err)
break
}
}
}
// removes initial slash, replaces following slashes with dot
func urlToPath(url string) string {
var path string = strings.TrimPrefix(strings.Replace(url, "/", ".", -1), ".")
return path
}
func rootServer(w http.ResponseWriter, r *http.Request) {
printInComingSession(r)
//check here if we should upgrade our connection to a websocket...
if r.Header.Get("Upgrade") == "websocket" {
log.Trace("we are upgrading to a websocket connection")
upgrader.CheckOrigin = func(r *http.Request) bool { return true }
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Error("upgrade:", err)
return
}
go wsClientSession(conn)
log.Trace("WS client session spawned.")
} else {
var path string = urlToPath(r.RequestURI)
var matches int = getMatches(path)
// build a JSON string of the response, makes it easier to test against.
message := `{"response":` + strconv.Itoa(matches) + "}"
response := []byte(message)
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Write(response)
log.Info("HTTP client request served for path = ", path)
}
}
func initVssFile() bool{
filePath := "vss_rel_1.0.cnative"
cfilePath := C.CString(filePath)
nodeHandle = C.VSSReadTree(cfilePath)
C.free(unsafe.Pointer(cfilePath))
if (nodeHandle == 0) {
log.Error("Tree file not found")
return false
}
nodeName := C.GoString(C.getName(nodeHandle))
log.Trace("Root node name = ", nodeName)
return true
}
func main() {
initLogger()
http.HandleFunc("/", rootServer) // register handler
if !initVssFile(){
log.Fatal(" Tree file not found")
return
}
log.Fatal(http.ListenAndServe("localhost:8080", nil)) // start server
}