This repository has been archived by the owner on Mar 29, 2022. It is now read-only.
/
server.go
179 lines (138 loc) · 3.36 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
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
package server
import (
"errors"
"fmt"
"sync"
"github.com/NeowayLabs/wabbit"
"github.com/NeowayLabs/wabbit/utils"
)
const (
MaxChannels int = 2 << 10
)
var (
servers map[string]*AMQPServer
mu *sync.Mutex
)
func init() {
servers = make(map[string]*AMQPServer)
mu = &sync.Mutex{}
}
// AMQPServer is a fake AMQP server. It handle the fake TCP connection
type AMQPServer struct {
running bool
amqpuri string
notifyChans map[string]*utils.ErrBroadcast
channels map[string][]*Channel
vhost *VHost
muChannels *sync.RWMutex
}
// NewServer returns a new fake amqp server
func newServer(amqpuri string) *AMQPServer {
return &AMQPServer{
amqpuri: amqpuri,
notifyChans: make(map[string]*utils.ErrBroadcast),
channels: make(map[string][]*Channel),
vhost: NewVHost("/"),
muChannels: &sync.RWMutex{},
}
}
// CreateChannel returns a new fresh channel
func (s *AMQPServer) CreateChannel(connID string, conn wabbit.Conn) (wabbit.Channel, error) {
s.muChannels.Lock()
defer s.muChannels.Unlock()
if _, ok := s.channels[connID]; !ok {
s.channels[connID] = make([]*Channel, 0, MaxChannels)
}
channels := s.channels[connID]
if len(channels) >= MaxChannels {
return nil, fmt.Errorf("Channel quota exceeded, Wabbit"+
" supports only %d fake channels for tests.", MaxChannels)
}
ch := NewChannel(s.vhost)
channels = append(channels, ch)
s.channels[connID] = channels
tempCh := make(chan wabbit.Error)
conn.NotifyClose(tempCh)
go func() {
for err := range tempCh {
ch.errSpread.Write(err)
}
close(tempCh)
}()
return ch, nil
}
// Start a new AMQP server fake-listening on host:port
func (s *AMQPServer) Start() error {
mu.Lock()
defer mu.Unlock()
s.running = true
return nil
}
// Stop the fake server
func (s *AMQPServer) Stop() error {
mu.Lock()
defer mu.Unlock()
s.running = false
for _, c := range s.notifyChans {
c.Write(utils.NewError(
utils.ChannelError,
"channel/connection is not open",
false,
false,
))
}
s.notifyChans = make(map[string]*utils.ErrBroadcast)
for _, chanMap := range s.channels {
for _, eachChan := range chanMap {
eachChan.Close()
}
}
return nil
}
// NewServer starts a new fake server
func NewServer(amqpuri string) *AMQPServer {
var amqpServer *AMQPServer
mu.Lock()
defer mu.Unlock()
amqpServer = servers[amqpuri]
if amqpServer == nil {
amqpServer = newServer(amqpuri)
servers[amqpuri] = amqpServer
}
return amqpServer
}
func (s *AMQPServer) addNotify(connID string, nchan *utils.ErrBroadcast) {
mu.Lock()
defer mu.Unlock()
s.notifyChans[connID] = nchan
}
func (s *AMQPServer) delNotify(connID string) {
mu.Lock()
defer mu.Unlock()
delete(s.notifyChans, connID)
}
func getServer(amqpuri string) (*AMQPServer, error) {
mu.Lock()
defer mu.Unlock()
amqpServer := servers[amqpuri]
if amqpServer == nil || amqpServer.running == false {
return nil, errors.New("Network unreachable")
}
return amqpServer, nil
}
func Connect(amqpuri string, connID string, errBroadcast *utils.ErrBroadcast) (*AMQPServer, error) {
amqpServer, err := getServer(amqpuri)
if err != nil {
return nil, err
}
amqpServer.addNotify(connID, errBroadcast)
return amqpServer, nil
}
func Close(amqpuri string, connID string) error {
amqpServer, err := getServer(amqpuri)
if err != nil {
return errors.New("Failed to close connection")
}
amqpServer.delNotify(connID)
return nil
}