-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmrclean.go
246 lines (210 loc) · 5.03 KB
/
mrclean.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package mrclean
import (
"encoding/json"
"fmt"
"image"
"io/ioutil"
"log"
"net"
"os"
"strings"
"code.google.com/p/go.net/ipv4"
)
//messgae type
const (
MInput = "input"
MCmd = "command"
)
const (
ImageMsg = "ImageData"
SortMsg = "SortData"
)
//ports for the components
const (
CoreAddr string = ":32123"
ChronicleAddr string = ":32124"
DisplayAddr string = ":32125"
)
var (
LB *net.UDPAddr = &net.UDPAddr{
IP: net.ParseIP("127.0.0.1"),
Port: 32123,
}
Mcast *net.UDPAddr = &net.UDPAddr{
IP: net.ParseIP("224.0.0.3"),
Port: 32123,
}
//Mcast string = "224.0.0.3:32123"
DisplayCloudWS = "ws://10.1.255.77:8088/ws_rpc_events"
)
type OutMessage struct {
Header string //so fat just a string indicating the kind of message
Content interface{} //json.RawMessage
}
type InMessage struct {
Header string //so fat just a string indicating the kind of message
Content json.RawMessage
}
//type InputMessage struct {
// Drag *we.MouseDrag
// Press *we.MousePress
// Release *we.MouseRelease
// Move *we.MouseMove
// PadPress *we.PadPress
// PadRelease *we.PadRelease
// PadMove *we.PadMove
// PadDrag *we.PadDrag
// AxisMove *we.AxisMove
// HandMove *we.HandMove
//}
type CmdMessage struct {
NewImage *ImageData //*string
Sort *SortData
}
type ImageData struct {
Name string
Size [2]int
URL string
//Meta []string
MetaData
}
type MetaData struct {
Task string
Approach string
Iteration string //int
Method string
}
func (m *MetaData) Parse(info string) error {
str := strings.Split(info, "/")
if len(str) < 4 {
return fmt.Errorf("Not enough info for metadata: %s", info)
}
m.Task = str[0]
m.Approach = str[1]
m.Iteration = str[2]
m.Method = str[3]
return nil
}
func (m *MetaData) String() string {
return fmt.Sprintf("%s/%s/%s/%s", m.Task, m.Approach, m.Iteration, m.Method)
}
//sorting interface stuff
type Images []*ImageData
//partial implementationn of sort.Interface the rest is implementes
//in the other types
func (i Images) Len() int { return len(i) }
func (i Images) Swap(p, q int) { i[p], i[q] = i[q], i[p] }
//sort by name
type ByName struct{ Images }
func (n ByName) Less(i, j int) bool { return n.Images[i].Name < n.Images[j].Name }
//sort by Iteration
type ByIteration struct{ Images }
func (n ByIteration) Less(i, j int) bool { return n.Images[i].Iteration < n.Images[j].Iteration }
//sort by Approach
type ByApproach struct{ Images }
func (n ByApproach) Less(i, j int) bool { return n.Images[i].Approach < n.Images[j].Approach }
//sort by Method
type ByMethod struct{ Images }
func (n ByMethod) Less(i, j int) bool { return n.Images[i].Method < n.Images[j].Method }
type SortData struct {
Order []string //int
}
func JoinMcast(group net.IP) (*net.UDPConn, error) {
conn, err := net.ListenPacket("udp4", Mcast.String())
if err != nil {
return nil, err
}
p := ipv4.NewPacketConn(conn)
//en, err := p.MulticastInterface()
//if err != nil {
// log.Println("multicast interface")
// return nil, err
//}
ifcs, err := McastInterfaces()
if err != nil {
return nil, err
}
for _, ifc := range ifcs {
if err := p.JoinGroup(&ifc, &net.UDPAddr{IP: group}); err != nil {
return nil, err
}
}
//err = conn.SetReadBuffer(1 << 12) //4Kb
//if err != nil {
// log.Fatal(err)
//}
log.Println("Connected to multicast: ")
udp, ok := conn.(*net.UDPConn)
if !ok {
return nil, fmt.Errorf("Cannot convert to *net.UDPConn")
}
return udp, nil
}
func McastInterfaces() ([]net.Interface, error) {
tt, err := net.Interfaces()
if err != nil {
return nil, err
}
ret := make([]net.Interface, 0, 1)
for _, t := range tt {
if t.Flags&(net.FlagUp) != 0 && (net.FlagMulticast) != 0 {
ret = append(ret, t)
}
}
if len(ret) == 0 {
return nil, fmt.Errorf("Cannot find a multicast interface")
}
return ret, nil
}
// A Gestue is a gesture performed by a user.
type Gesture struct {
ID int
Name string
Param []string
}
type VisualOrigins struct {
Vids []int
Origins [][]float64
}
func NewVisualOrigins() *VisualOrigins {
return &VisualOrigins{
Vids: make([]int, 0, 10),
Origins: make([][]float64, 0, 10),
}
}
// Visual represents a visual object to be displaied.
type Visual struct {
//The id of the visual
ID int
// The name of the visual
Name string
//The rectangle holding proportion and size for the visual in pixels
Rectangle image.Rectangle
//The url specifiyng where to find the hvisual
URL string
//commit number
Commit string
//Metadata associated with the visual
Meta []string
//Origin is the handle to move teh Visual
Origin []float64 //`json:"omitempty"`
//Size represent the size on screen
Size []float64 //`json:"omitempty"`
}
func ReadConfig(fname string) (map[string]interface{}, error) {
file, err := os.Open(fname)
if err != nil {
return nil, err
}
buff, err := ioutil.ReadAll(file)
if err != nil {
return nil, err
}
var config map[string]interface{}
err = json.Unmarshal(buff, &config)
if err != nil {
return nil, err
}
log.Printf("Got configuration: %+v\n", config)
return config, nil
}