5
5
"fmt"
6
6
"io"
7
7
"log"
8
- "regexp"
9
- "strings"
10
8
"sync"
11
9
12
10
"golang.org/x/net/websocket"
16
14
messageCount = 0
17
15
)
18
16
17
+ type Base struct {
18
+ Op string `json:"op"`
19
+ Id string `json:"id"`
20
+ }
21
+
19
22
type Ros struct {
20
23
origin string
21
24
url string
@@ -55,6 +58,21 @@ func (ros *Ros) getServiceResponse(service *ServiceCall) *ServiceResponse {
55
58
return serviceResponse .(* ServiceResponse )
56
59
}
57
60
61
+ func (ros * Ros ) getTopicResponse (topic * Topic ) * interface {} {
62
+ response := make (chan interface {})
63
+ ros .receivedMapMutex .Lock ()
64
+ ros .receivedMap [topic .Id ] = response
65
+ ros .receivedMapMutex .Unlock ()
66
+ err := websocket .JSON .Send (ros .ws , topic )
67
+ if err != nil {
68
+ fmt .Println ("Couldn't send msg" )
69
+ }
70
+ log .Println (ros .receivedMap )
71
+
72
+ topicResponse := <- response
73
+ return & topicResponse
74
+ }
75
+
58
76
func (ros * Ros ) returnToAppropriateChannel (id string , data interface {}) {
59
77
ros .receivedMapMutex .Lock ()
60
78
ros .receivedMap [id ] <- data
@@ -73,34 +91,34 @@ func (ros *Ros) handleIncoming() {
73
91
break
74
92
}
75
93
76
- opRegex , err := regexp .Compile (`"op"\s*:\s*"[[:alpha:],_]*` )
77
- if err != nil {
78
- log .Println (err )
79
- }
80
- opString := opRegex .FindString (string (msg ))
81
- splitOpString := strings .Split (opString , "\" " )
82
- operation := splitOpString [len (splitOpString )- 1 ]
83
-
84
- //log.Println(operation)
85
-
86
94
/*
87
- var data map[string]interface{}
88
- jsonErr := json.Unmarshal(msg, &data)
89
- //fmt.Printf("Received from server: %s\n", data)
90
- if jsonErr != nil {
91
- panic(jsonErr)
95
+ opRegex, err := regexp.Compile(`"op"\s*:\s*"[[:alpha:],_]*`)
96
+ if err != nil {
97
+ log.Println(err)
92
98
}
93
-
94
- ros.receivedMapMutex.Lock()
95
- ros.receivedMap[data["id"].(string)] <- data
96
- ros.receivedMapMutex.Unlock()
99
+ opString := opRegex.FindString(string(msg))
100
+ splitOpString := strings.Split(opString, "\"")
101
+ operation := splitOpString[len(splitOpString)-1]
97
102
*/
98
- if operation == "service_response" {
103
+
104
+ var base Base
105
+ json .Unmarshal (msg , & base )
106
+
107
+ log .Println (base )
108
+
109
+ if base .Op == "service_response" {
99
110
var serviceResponse ServiceResponse
100
111
json .Unmarshal (msg , & serviceResponse )
101
112
ros .receivedMapMutex .Lock ()
102
113
ros .receivedMap [serviceResponse .Id ] <- & serviceResponse
103
114
ros .receivedMapMutex .Unlock ()
115
+ } else if base .Op == "publish" {
116
+ log .Println (base )
117
+ var topic Topic
118
+ json .Unmarshal (msg , & topic )
119
+ ros .receivedMapMutex .Lock ()
120
+ ros .receivedMap [topic .Topic ] <- & topic
121
+ ros .receivedMapMutex .Unlock ()
104
122
}
105
123
}
106
124
}
@@ -112,6 +130,22 @@ func (ros *Ros) GetTopics() []string {
112
130
return topics
113
131
}
114
132
115
- func (ros * Ros ) Subscribe (topic * Topic ) {
133
+ func (ros * Ros ) Subscribe (topicName string , callback TopicCallback ) {
134
+ //topicResponse := ros.getTopicResponse(topic)
135
+ topic := NewTopic (topicName )
116
136
137
+ response := make (chan interface {})
138
+ ros .receivedMapMutex .Lock ()
139
+ ros .receivedMap [topic .Topic ] = response
140
+ ros .receivedMapMutex .Unlock ()
141
+ err := websocket .JSON .Send (ros .ws , topic )
142
+ if err != nil {
143
+ fmt .Println ("Couldn't send msg" )
144
+ }
145
+
146
+ go func () {
147
+ for {
148
+ callback (& (<- response ).(* Topic ).Msg )
149
+ }
150
+ }()
117
151
}
0 commit comments