-
Notifications
You must be signed in to change notification settings - Fork 14
/
mother.go
188 lines (155 loc) · 4.08 KB
/
mother.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
/*
* Copyright 2021 Comcast Cable Communications Management, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
package dsl
import (
"encoding/json"
"fmt"
"time"
"github.com/Comcast/plax/subst"
)
// MotherRequest is the structure for a request to Mother.
//
// Every MotherRequest will get exactly one MotherResponse.
type MotherRequest struct {
Make *MotherMakeRequest `json:"make"`
}
// MotherMakeRequest is the structure for a request to make a new
// channel.
type MotherMakeRequest struct {
// Name is the requested name for the channel to be created.
Name string `json:"name"`
// Type is something like 'mqtt', 'httpclient', or 'sqs' (the
// types that are registered with a (or The) ChannelRegistry).
Type ChanKind `json:"type"`
// Config is the configuration (if any) for the requested channel.
Config interface{} `json:"config,omitempty"`
}
// MotherResponse is the structure of the generic response to a
// request.
type MotherResponse struct {
// Request is the request the provoked this response.
Request *MotherRequest `json:"request"`
// Success reports whether the request succeeded.
Success bool `json:"success"`
// Error, if not zero, is an error message for a failed
// request.
Error string `json:"error,omitempty"`
}
// Mother is the mother of all (other) channels.
//
// Mother ('mother') can make channels, and Mother is itself a
// Channel.
type Mother struct {
t *Test
c chan Msg
}
func NewMother(ctx *Ctx, _ interface{}) (*Mother, error) {
return &Mother{
c: make(chan Msg, 1024),
}, nil
}
func (c *Mother) DocSpec() *DocSpec {
return &DocSpec{
Chan: &Mother{},
Input: &MotherRequest{},
Output: &MotherResponse{},
}
}
func (c *Mother) Kind() ChanKind {
return "mother"
}
func (c *Mother) Open(ctx *Ctx) error {
return nil
}
func (c *Mother) Close(ctx *Ctx) error {
return nil
}
func (c *Mother) Sub(ctx *Ctx, topic string) error {
ctx.Logf("Mother.Sub %s", topic)
return nil
}
// Pub sends a request to Mother.
//
// The message payload should represent a MotherRequest in JSON.
func (c *Mother) Pub(ctx *Ctx, m Msg) error {
ctx.Logf("Mother.Pub %T %v", m.Payload, m.Payload)
var (
req MotherRequest
resp MotherResponse
)
punt := func(err error) error {
if err != nil {
resp.Success = false
resp.Error = err.Error()
}
js, err := subst.JSONMarshal(&resp)
if err != nil {
return err
}
return c.To(ctx, Msg{
Payload: string(js),
})
}
// Parse the payload as a MotherRequest.
if err := json.Unmarshal([]byte(m.Payload), &req); err != nil {
return punt(err)
}
resp.Request = &req
// Handle the request.
if req.Make == nil {
return punt(fmt.Errorf("Only 'make' supported"))
}
if _, have := c.t.Chans[req.Make.Name]; have {
return punt(fmt.Errorf("Already have chan '%s'", req.Make.Name))
}
// Special cases
switch req.Make.Type {
case "cmd":
if m, is := req.Make.Config.(map[string]interface{}); is {
m["name"] = req.Make.Name
}
}
ch, err := c.t.makeChan(ctx, req.Make.Type, req.Make.Config)
if err != nil {
return punt(err)
}
if err := ch.Open(ctx); err != nil {
return punt(err)
}
resp.Success = true
c.t.Chans[req.Make.Name] = ch
return punt(nil)
}
func (c *Mother) Recv(ctx *Ctx) chan Msg {
ctx.Logf("Mother.Recv")
return c.c
}
func (c *Mother) Kill(ctx *Ctx) error {
return fmt.Errorf("Kill is not supported by a %T", c)
}
func (c *Mother) To(ctx *Ctx, m Msg) error {
ctx.Logf("Mother To %s", m.Payload)
m.ReceivedAt = time.Now().UTC()
select {
case <-ctx.Done():
case c.c <- m:
default:
panic("Warning: Mother channel full")
}
return nil
}