Debian Codegraph @

amqptest/server/vhost.go

raw
1 package server
2
3 import (
4 "fmt"
5 "sync"
6
7 "github.com/NeowayLabs/wabbit"
8 )
9
10 // VHost is a fake AMQP virtual host
11 type VHost struct {
12 name string
13 mu sync.Mutex // Protects exchanges and queues.
14 exchanges map[string]Exchange
15 queues map[string]*Queue
16 }
17
18 // NewVHost create a new fake AMQP Virtual Host
19 func NewVHost(name string) *VHost {
20 vh := VHost{
21 name: name,
22 queues: make(map[string]*Queue),
23 exchanges: make(map[string]Exchange),
24 }
25
26 vh.createDefaultExchanges()
27 return &vh
28 }
29
30 func (v *VHost) createDefaultExchanges() {
31 exchs := make(map[string]Exchange)
32 exchs["amq.topic"] = NewTopicExchange("amq.topic")
33 exchs["amq.direct"] = NewDirectExchange("amq.direct")
34 exchs["topic"] = NewTopicExchange("topic")
35 exchs["direct"] = NewDirectExchange("direct")
36 exchs[""] = NewDirectExchange("amq.direct")
37
38 v.mu.Lock()
39 v.exchanges = exchs
40 v.mu.Unlock()
41 }
42
43 func (v *VHost) Cancel(consumer string, noWait bool) error {
44 return nil
45 }
46
47 // Qos isn't implemented in the fake server
48 func (v *VHost) Qos(prefetchCount, prefetchSize int, global bool) error {
49 // do nothing. It's a implementation-specific tuning
50 return nil
51 }
52
53 func (v *VHost) ExchangeDeclare(name, kind string, opt wabbit.Option) error {
54 v.mu.Lock()
55 defer v.mu.Unlock()
56
57 return v.exchangeDeclare(name, kind, false, opt)
58 }
59
60 func (v *VHost) ExchangeDeclarePassive(name, kind string, opt wabbit.Option) error {
61 v.mu.Lock()
62 defer v.mu.Unlock()
63
64 return v.exchangeDeclare(name, kind, true, opt)
65 }
66
67 func (v *VHost) exchangeDeclare(name, kind string, passive bool, opt wabbit.Option) error {
68 if _, ok := v.exchanges[name]; ok {
69 // TODO: We need review this. If the application is trying to re-create an exchange
70 // using other options we shall not return NIL because this indicates success,
71 // but we didn't declared anything.
72 // The AMQP 0.9.1 spec says nothing about that. It only says that AMQP uses the
73 // "declare" concept instead of the "create" concept. If something is already
74 // declared it's no problem...
75 return nil
76 }
77
78 if passive {
79 return fmt.Errorf("exception (404) Reason: \"NOT_FOUND - no exchange '%s' in vhost '%s'\"", name, v.name)
80 }
81
82 switch kind {
83 case "topic":
84 v.exchanges[name] = NewTopicExchange(name)
85 case "direct":
86 v.exchanges[name] = NewDirectExchange(name)
87 case "headers":
88 v.exchanges[name] = NewHeadersExchange(name)
89 default:
90 return fmt.Errorf("invalid exchange type: %s", kind)
91 }
92
93 return nil
94 }
95
96 func (v *VHost) QueueDeclare(name string, args wabbit.Option) (wabbit.Queue, error) {
97 v.mu.Lock()
98 defer v.mu.Unlock()
99
100 return v.queueDeclare(name, false, args)
101 }
102
103 func (v *VHost) QueueDeclarePassive(name string, args wabbit.Option) (wabbit.Queue, error) {
104 v.mu.Lock()
105 defer v.mu.Unlock()
106
107 return v.queueDeclare(name, true, args)
108 }
109
110 func (v *VHost) queueDeclare(name string, passive bool, args wabbit.Option) (wabbit.Queue, error) {
111 if q, ok := v.queues[name]; ok {
112 return q, nil
113 }
114
115 if passive {
116 return nil, fmt.Errorf("exception (404) Reason: \"NOT_FOUND - no queue '%s' in vhost '%s'\"", name, v.name)
117 }
118
119 q := NewQueue(name)
120
121 v.queues[name] = q
122
123 err := v.queueBind(name, name, "", nil)
124
125 if err != nil {
126 return nil, err
127 }
128
129 return q, nil
130 }
131
132 func (v *VHost) QueueDelete(name string, args wabbit.Option) (int, error) {
133 v.mu.Lock()
134 delete(v.queues, name)
135 v.mu.Unlock()
136
137 return 0, nil
138 }
139
140 func (v *VHost) QueueBind(name, key, exchange string, options wabbit.Option) error {
141 v.mu.Lock()
142 defer v.mu.Unlock()
143
144 return v.queueBind(name, key, exchange, options)
145 }
146
147 func (v *VHost) queueBind(name, key, exchange string, _ wabbit.Option) error {
148 var (
149 exch Exchange
150 q *Queue
151 ok bool
152 )
153
154 if exch, ok = v.exchanges[exchange]; !ok {
155 return fmt.Errorf("unknown exchange '%s'", exchange)
156 }
157
158 if q, ok = v.queues[name]; !ok {
159 return fmt.Errorf("unknown queue '%s'", name)
160 }
161
162 exch.addBinding(key, &BindingsMap{q, nil})
163 return nil
164 }
165
166 func (v *VHost) QueueUnbind(name, key, exchange string, options wabbit.Option) error {
167 v.mu.Lock()
168 defer v.mu.Unlock()
169
170 return v.queueUnbind(name, key, exchange, options)
171 }
172
173 func (v *VHost) queueUnbind(name, key, exchange string, _ wabbit.Option) error {
174 var (
175 exch Exchange
176 ok bool
177 )
178
179 if exch, ok = v.exchanges[exchange]; !ok {
180 return fmt.Errorf("unknown exchange '%s'", exchange)
181 }
182
183 if _, ok = v.queues[name]; !ok {
184 return fmt.Errorf("unknown queue '%s'", name)
185 }
186
187 exch.delBinding(key)
188 return nil
189 }
190
191 // Publish push a new message to queue data channel.
192 // The queue data channel is a buffered channel of length `QueueMaxLen`. If
193 // the queue is full, this method will block until some messages are consumed.
194 func (v *VHost) Publish(exc, route string, d *Delivery, options wabbit.Option) error {
195 v.mu.Lock()
196 defer v.mu.Unlock()
197
198 return v.publish(exc, route, d, options)
199 }
200
201 func (v *VHost) publish(exc, route string, d *Delivery, _ wabbit.Option) error {
202 var (
203 exch Exchange
204 ok bool
205 err error
206 )
207
208 if exch, ok = v.exchanges[exc]; !ok {
209 return fmt.Errorf("unknow exchange '%s'", exc)
210 }
211
212 err = exch.route(route, d)
213
214 if err != nil {
215 return err
216 }
217
218 return nil
219 }