-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsession.go
More file actions
125 lines (102 loc) · 2.67 KB
/
Copy pathsession.go
File metadata and controls
125 lines (102 loc) · 2.67 KB
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
package node
import (
"fmt"
"time"
)
// Auth levels
// Low means agent presented a pdid but no credentials.
// High means agent presented valid credentials.
// TODO: Remove once all agents are required to be authenticated.
const (
AUTH_NONE = iota
AUTH_LOW = iota
AUTH_HIGH = iota
)
type Session struct {
Peer
Id ID
pdid URI
// authid is the highest domain the agent has been authenticated as,
// so it is the one we should use for permissions checking.
authid string
// TODO: Remove once authentication is enabled for all agents.
authLevel int
// Session freezing and resuming
// canFreeze: true if the session is capable of being frozen
// resumeFrom: non-zero session ID if resuming from a previous session
canFreeze bool
resumeFrom ID
messageCounts map[string]int64
kill chan URI
}
func (s Session) String() string {
return fmt.Sprintf("%s", s.pdid)
}
// Test if session is a local (built-in) peer.
func (s *Session) isLocal() bool {
_, ok := s.Peer.(*localPeer)
return ok
}
// localPipe creates two linked sessions. Messages sent to one will
// appear in the Receive of the other. This is useful for implementing
// client sessions
func localPipe() (*localPeer, *localPeer) {
aToB := make(chan Message, 10)
bToA := make(chan Message, 10)
a := &localPeer{
incoming: bToA,
outgoing: aToB,
}
b := &localPeer{
incoming: aToB,
outgoing: bToA,
}
return a, b
}
type localPeer struct {
outgoing chan<- Message
incoming <-chan Message
}
func (s *localPeer) Receive() <-chan Message {
return s.incoming
}
func (s *localPeer) Send(msg Message) error {
s.outgoing <- msg
return nil
}
func (s *localPeer) Close() error {
close(s.outgoing)
return nil
}
////////////////////////////////////////
// Contents of old 'peer.go' file
////////////////////////////////////////
// A Sender can send a message to its peer.
//
// For clients, this sends a message to the Node, and for Nodes,
// this sends a message to the client.
type Sender interface {
// Send a message to the peer
Send(Message) error
}
// Peer is the interface that must be implemented by all WAMP peers.
type Peer interface {
Sender
// Closes the peer connection and any channel returned from Receive().
// Multiple calls to Close() will have no effect.
Close() error
// Receive returns a channel of messages coming from the peer.
Receive() <-chan Message
}
// Convenience function to get a single message from a peer
func GetMessageTimeout(p Peer, t time.Duration) (Message, error) {
select {
case msg, open := <-p.Receive():
if !open {
return nil, fmt.Errorf("receive channel closed")
}
return msg, nil
case <-time.After(t):
return nil, fmt.Errorf("timeout waiting for message")
}
}