/
tuplespace_alt.go
667 lines (491 loc) · 16.2 KB
/
tuplespace_alt.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
package space
import (
"bytes"
"encoding/gob"
"log"
"net"
"strings"
"sync"
"time"
"github.com/pspaces/gospace/container"
"github.com/pspaces/gospace/function"
"github.com/pspaces/gospace/policy"
"github.com/pspaces/gospace/protocol"
"github.com/pspaces/gospace/space/uri"
)
// Constants for logging errors occuring in this file.
var (
tsAltBuf bytes.Buffer
tsAltLogger = log.New(&tsAltBuf, "gospace: ", log.Ldate|log.Ltime|log.Lmicroseconds|log.LUTC|log.Lshortfile)
)
// tsAltLog logs errors occuring in this file.
func tsAltLog(fun interface{}, e *error) {
if *e != nil {
tsAltLogger.Printf("%s: %s\n", function.Name(fun), *e)
}
}
// TODO: This is a part of hack, don't touch this.
// TODO: This can be removed once rearchitecting begins.
var localChanMap = new(sync.Map)
// NewSpaceAlt creates a representation of a new tuple space.
func NewSpaceAlt(url string, cp ...*policy.Composable) (ptp *protocol.PointToPoint, ts *TupleSpace) {
registerTypes()
u, err := uri.NewSpaceURI(url)
if err == nil {
// TODO: Exchange capabilities instead and
// TODO: make a mechanism capable of doing that.
if function.GlobalRegistry == nil {
fr := function.NewRegistry()
function.GlobalRegistry = &fr
}
funcReg := *function.GlobalRegistry
// TODO: Create a better condition if a hosts name resolves to a local address.
ips, err := net.LookupIP(u.Hostname())
// Test if we are connecting locally to avoid TCP port issue.
localhost := false
for _, a := range ips {
localhost = localhost || a.IsLoopback()
}
/*l, lerr := net.Listen("tcp4", ":"+u.Port())
if lerr == nil {
l.Close()
}*/
// TODO: Embrace the following hack, and remove it with architecting differently.
connc := make(chan *net.Conn)
val, exists := localChanMap.LoadOrStore(u, connc)
//if exists {
//close(connc)
//connc = (val.(chan *net.Conn))
//}
if !exists && err == nil {
muTuples := new(sync.RWMutex)
muWaitingClients := new(sync.Mutex)
tuples := []container.Tuple{}
addr := strings.Join([]string{u.Hostname(), u.Port()}, ":")
ts = &TupleSpace{
muTuples: muTuples,
muWaitingClients: muWaitingClients,
tuples: tuples,
pol: nil,
funReg: &funcReg,
port: addr,
connc: connc,
}
if len(cp) == 1 {
(*ts).pol = cp[0]
}
go ts.Listen()
ptp = protocol.CreatePointToPoint(u.Space(), u.Hostname(), "0", connc, &funcReg)
} else {
for localhost && !exists {
val, exists = localChanMap.Load(u)
if exists {
connc = (val.(chan *net.Conn))
break
}
}
var port string
if !localhost {
port = u.Port()
} else {
port = "0"
}
ptp = protocol.CreatePointToPoint(u.Space(), u.Hostname(), port, connc, &funcReg)
}
} else {
ts = nil
ptp = nil
}
return ptp, ts
}
// NewRemoteSpaceAlt creates a remote representation of a tuple space.
func NewRemoteSpaceAlt(url string, cp ...*policy.Composable) (ptp *protocol.PointToPoint, ts *TupleSpace) {
registerTypes()
u, err := uri.NewSpaceURI(url)
if err == nil {
// TODO: Exchange capabilities instead and
// TODO: make a mechanism capable of doing that.
if function.GlobalRegistry == nil {
fr := function.NewRegistry()
function.GlobalRegistry = &fr
}
funcReg := *function.GlobalRegistry
// TODO: Create a better condition if a hosts name resolves to a local address.
ips, _ := net.LookupIP(u.Hostname())
// Test if we are connecting locally to avoid TCP port issue.
localhost := false
for _, a := range ips {
localhost = localhost || a.IsLoopback()
}
var connc chan *net.Conn
for localhost {
val, exists := localChanMap.Load(u)
if exists {
connc = (val.(chan *net.Conn))
break
}
}
var port string
if !localhost {
port = u.Port()
} else {
port = "0"
}
// NOTE: It is not possible to connect to localhost as a remote space
// NOTE: or if the port is taken.
ptp = protocol.CreatePointToPoint(u.Space(), u.Hostname(), port, connc, &funcReg)
} else {
ts = nil
ptp = nil
}
return ptp, ts
}
// registerTypes registers all the types necessary for the implementation.
func registerTypes() {
gob.Register(container.Label{})
gob.Register(container.Labels{})
gob.Register(container.Template{})
gob.Register(container.Tuple{})
gob.Register(container.TypeField{})
gob.Register([]interface{}{})
}
// Size will open a TCP connection to the PointToPoint and request the size of the tuple space.
func Size(ptp protocol.PointToPoint) (sz int, b bool) {
var conn *net.Conn
var err error
defer tsAltLog(Size, &err)
sz = -1
b = false
conn, err = establishConnection(ptp)
if err != nil {
return sz, b
}
defer (*conn).Close()
err = sendMessage(conn, protocol.SizeRequest, "")
if err != nil {
return sz, b
}
sz, err = receiveMessageInt(conn)
if err != nil {
sz = -1
} else {
b = true
}
return sz, b
}
// Put will open a TCP connection to the PointToPoint and send the message,
// which includes the type of operation and tuple specified by the user.
// The method returns a boolean to inform if the operation was carried out with
// success or not.
func Put(ptp protocol.PointToPoint, tupleFields ...interface{}) (t container.Tuple, b bool) {
var conn *net.Conn
var err error
defer tsAltLog(Put, &err)
b = false
t = container.NewTuple(tupleFields...)
// Never time out and block until connection will be established.
conn, err = establishConnection(ptp)
// TODO: Yes this is a bad idea, and we are doing it for now until semantics
// TODO: for what it means to block is established.
for {
if err == nil {
break
}
if *conn != nil {
(*conn).Close()
}
conn, err = establishConnection(ptp)
}
defer (*conn).Close()
funcEncode(ptp.GetRegistry(), &t)
defer funcDecode(ptp.GetRegistry(), &t)
err = sendMessage(conn, protocol.PutRequest, t)
if err != nil {
return container.NewTuple(nil), b
}
b, err = receiveMessageBool(conn)
if err != nil {
b = false
return container.NewTuple(nil), b
}
return t, b
}
// PutP will open a TCP connection to the PointToPoint and send the message,
// which includes the type of operation and tuple specified by the user.
// As the method is nonblocking it wont wait for a response whether or not the
// operation was successful.
// The method returns a boolean to inform if the operation was carried out with
// any errors with communication.
func PutP(ptp protocol.PointToPoint, tupleFields ...interface{}) (t container.Tuple, b bool) {
var conn *net.Conn
var err error
defer tsAltLog(PutP, &err)
b = false
t = container.NewTuple(tupleFields...)
conn, err = establishConnection(ptp)
if err != nil {
return container.NewTuple(nil), b
}
defer (*conn).Close()
funcEncode(ptp.GetRegistry(), &t)
defer funcDecode(ptp.GetRegistry(), &t)
err = sendMessage(conn, protocol.PutPRequest, t)
if err != nil {
return container.NewTuple(nil), b
}
b = true
return t, b
}
// Get will open a TCP connection to the PointToPoint and send the message,
// which includes the type of operation and template specified by the user.
// The method returns a boolean to inform if the operation was carried out with
// any errors with communication.
func Get(ptp protocol.PointToPoint, tempFields ...interface{}) (t container.Tuple, b bool) {
t, b = getAndQuery(ptp, protocol.GetRequest, tempFields...)
return t, b
}
// Query will open a TCP connection to the PointToPoint and send the message,
// which includes the type of operation and template specified by the user.
// The method returns a boolean to inform if the operation was carried out with
// any errors with communication.
func Query(ptp protocol.PointToPoint, tempFields ...interface{}) (t container.Tuple, b bool) {
t, b = getAndQuery(ptp, protocol.QueryRequest, tempFields...)
return t, b
}
func getAndQuery(ptp protocol.PointToPoint, operation string, tempFields ...interface{}) (t container.Tuple, b bool) {
var conn *net.Conn
var err error
defer tsAltLog(getAndQuery, &err)
b = false
tp := container.NewTemplate(tempFields...)
// Never time out and block until connection will be established.
conn, err = establishConnection(ptp)
// Busy loop until a successful connection is established.
// TODO: Yes this is a bad idea, and we are doing it for now until semantics
// TODO: for what it means to block is established.
for {
if err == nil {
break
}
if *conn != nil {
(*conn).Close()
}
conn, err = establishConnection(ptp)
}
defer (*conn).Close()
funcEncode(ptp.GetRegistry(), &tp)
defer funcDecode(ptp.GetRegistry(), &t)
err = sendMessage(conn, operation, tp)
if err != nil {
return container.NewTuple(nil), b
}
t, err = receiveMessageTuple(conn)
if err != nil {
return container.NewTuple(nil), b
}
b = true
funcDecode(ptp.GetRegistry(), &t)
return t, b
}
// GetP will open a TCP connection to the PointToPoint and send the message,
// which includes the type of operation and template specified by the user.
// The function will return two bool values. The first denotes if a tuple was
// found, the second if there were any erors with communication.
func GetP(ptp protocol.PointToPoint, tempFields ...interface{}) (container.Tuple, bool, bool) {
return getPAndQueryP(ptp, protocol.GetPRequest, tempFields...)
}
// QueryP will open a TCP connection to the PointToPoint and send the message,
// which includes the type of operation and template specified by the user.
// The function will return two bool values. The first denotes if a tuple was
// found, the second if there were any erors with communication.
func QueryP(ptp protocol.PointToPoint, tempFields ...interface{}) (container.Tuple, bool, bool) {
return getPAndQueryP(ptp, protocol.QueryPRequest, tempFields...)
}
func getPAndQueryP(ptp protocol.PointToPoint, operation string, tempFields ...interface{}) (t container.Tuple, tb bool, sb bool) {
var conn *net.Conn
var err error
defer tsAltLog(getPAndQueryP, &err)
tb = false
sb = false
tp := container.NewTemplate(tempFields...)
conn, err = establishConnection(ptp)
if err != nil {
return container.NewTuple(nil), tb, sb
}
defer (*conn).Close()
funcEncode(ptp.GetRegistry(), &tp)
defer funcDecode(ptp.GetRegistry(), &tp)
err = sendMessage(conn, operation, tp)
if err != nil {
return container.NewTuple(nil), tb, sb
}
tb, t, err = receiveMessageBoolAndTuple(conn)
if err != nil {
return container.NewTuple(nil), tb, sb
}
sb = true
return t, tb, sb
}
// GetAll will open a TCP connection to the PointToPoint and send the message,
// which includes the type of operation specified by the user.
// The method is nonblocking and will return all tuples found in the tuple
// space as well as a bool to denote if there were any errors with the
// communication.
func GetAll(ptp protocol.PointToPoint, tempFields ...interface{}) (ts []container.Tuple, b bool) {
ts, b = getAllAndQueryAll(ptp, protocol.GetAllRequest, tempFields...)
return ts, b
}
// QueryAll will open a TCP connection to the PointToPoint and send the message,
// which includes the type of operation specified by the user.
// The method is nonblocking and will return all tuples found in the tuple
// space as well as a bool to denote if there were any errors with the
// communication.
func QueryAll(ptp protocol.PointToPoint, tempFields ...interface{}) (ts []container.Tuple, b bool) {
ts, b = getAllAndQueryAll(ptp, protocol.QueryAllRequest, tempFields...)
return ts, b
}
func getAllAndQueryAll(ptp protocol.PointToPoint, operation string, tempFields ...interface{}) (ts []container.Tuple, b bool) {
var conn *net.Conn
var err error
defer tsAltLog(getAllAndQueryAll, &err)
ts = []container.Tuple{}
b = false
tp := container.NewTemplate(tempFields...)
conn, err = establishConnection(ptp)
if err != nil {
return ts, b
}
defer (*conn).Close()
funcEncode(ptp.GetRegistry(), &tp)
err = sendMessage(conn, operation, tp)
if err != nil {
return ts, b
}
ts, err = receiveMessageTupleList(conn)
if err != nil {
return ts, b
}
b = true
for _, t := range ts {
funcDecode(ptp.GetRegistry(), &t)
}
return ts, b
}
// PutAgg will connect to a space and aggregate on matched tuples from the space according to a template.
// This method is nonblocking and will return a tuple and boolean state to denote if there were any errors
// with the communication. The tuple returned is the aggregation of tuples in the space.
// If no tuples are found it will create and put a new tuple from the template itself.
func PutAgg(ptp protocol.PointToPoint, fun interface{}, tempFields ...interface{}) (t container.Tuple, b bool) {
t, b = aggOperation(ptp, protocol.PutAggRequest, fun, tempFields...)
return t, b
}
// GetAgg will connect to a space and aggregate on matched tuples from the space.
// This method is nonblocking and will return a tuple perfomed by the aggregation
// as well as a boolean state to denote if there were any errors with the communication.
// The resulting tuple is empty if no matching occurs or the aggregation function can not aggregate the matched tuples.
func GetAgg(ptp protocol.PointToPoint, fun interface{}, tempFields ...interface{}) (t container.Tuple, b bool) {
t, b = aggOperation(ptp, protocol.GetAggRequest, fun, tempFields...)
return t, b
}
// QueryAgg will connect to a space and aggregated on matched tuples from the space.
// which includes the type of operation specified by the user.
// The method is nonblocking and will return a tuple found by aggregating the matched typles.
// The resulting tuple is empty if no matching occurs or the aggregation function can not aggregate the matched tuples.
func QueryAgg(ptp protocol.PointToPoint, fun interface{}, tempFields ...interface{}) (t container.Tuple, b bool) {
t, b = aggOperation(ptp, protocol.QueryAggRequest, fun, tempFields...)
return t, b
}
func aggOperation(ptp protocol.PointToPoint, operation string, fun interface{}, tempFields ...interface{}) (t container.Tuple, b bool) {
var conn *net.Conn
var err error
defer tsAltLog(aggOperation, &err)
t = container.NewTuple()
b = false
fields := make([]interface{}, len(tempFields)+1)
fields[0] = fun
copy(fields[1:], tempFields)
tp := container.NewTemplate(fields...)
conn, err = establishConnection(ptp)
if err != nil {
return t, b
}
defer (*conn).Close()
funcEncode(ptp.GetRegistry(), &tp)
err = sendMessage(conn, operation, tp)
if err != nil {
return t, b
}
t, err = receiveMessageTuple(conn)
if err != nil {
return t, b
}
b = true
funcDecode(ptp.GetRegistry(), &t)
return t, b
}
// establishConnection will establish a connection to the PointToPoint ptp and
// return the Conn and error.
func establishConnection(ptp protocol.PointToPoint, timeout ...time.Duration) (*net.Conn, error) {
var conn net.Conn
var err error
addr := ptp.GetAddress()
host, _, err := net.SplitHostPort(addr)
proto := "tcp4"
ips, err := net.LookupIP(host)
if err == nil {
// Test if we are connecting locally to avoid TCP port issue.
localhost := false
for _, a := range ips {
localhost = localhost || a.IsLoopback()
}
connc := ptp.GetConnectionChannel()
if localhost && connc != nil {
r, w := net.Pipe()
conn = r
(*connc) <- &w
} else {
if len(timeout) == 0 {
conn, err = net.Dial(proto, addr)
} else {
conn, err = net.DialTimeout(proto, addr, timeout[0])
}
}
}
return &conn, err
}
func sendMessage(conn *net.Conn, operation string, t interface{}) (err error) {
gob.Register(t)
gob.Register(container.TypeField{})
enc := gob.NewEncoder(*conn)
message := protocol.CreateMessage(operation, t)
err = enc.Encode(message)
return err
}
func receiveMessageBool(conn *net.Conn) (b bool, err error) {
dec := gob.NewDecoder(*conn)
err = dec.Decode(&b)
return b, err
}
func receiveMessageInt(conn *net.Conn) (i int, err error) {
dec := gob.NewDecoder(*conn)
err = dec.Decode(&i)
return i, err
}
func receiveMessageTuple(conn *net.Conn) (t container.Tuple, err error) {
dec := gob.NewDecoder(*conn)
err = dec.Decode(&t)
return t, err
}
func receiveMessageBoolAndTuple(conn *net.Conn) (b bool, t container.Tuple, err error) {
dec := gob.NewDecoder(*conn)
var result []interface{}
err = dec.Decode(&result)
b = result[0].(bool)
t = result[1].(container.Tuple)
return b, t, err
}
func receiveMessageTupleList(conn *net.Conn) (ts []container.Tuple, err error) {
dec := gob.NewDecoder(*conn)
err = dec.Decode(&ts)
return ts, err
}