summaryrefslogtreecommitdiff
path: root/plugins/websockets/executor/executor.go
blob: 9ef5e40a093253b953f6b0e03f641e3dc6526647 (plain)
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
package executor

import (
	"github.com/fasthttp/websocket"
	json "github.com/json-iterator/go"
	"github.com/spiral/roadrunner/v2/pkg/pubsub"
	"github.com/spiral/roadrunner/v2/plugins/logger"
	"github.com/spiral/roadrunner/v2/plugins/websockets/commands"
	"github.com/spiral/roadrunner/v2/plugins/websockets/connection"
	"github.com/spiral/roadrunner/v2/plugins/websockets/storage"
)

type Response struct {
	Topic   string   `json:"topic"`
	Payload []string `json:"payload"`
}

type Executor struct {
	conn    *connection.Connection
	storage *storage.Storage
	log     logger.Logger

	// associated connection ID
	connID string
	pubsub map[string]pubsub.PubSub
}

// NewExecutor creates protected connection and starts command loop
func NewExecutor(conn *connection.Connection, log logger.Logger, bst *storage.Storage, connID string, pubsubs map[string]pubsub.PubSub) *Executor {
	return &Executor{
		conn:    conn,
		connID:  connID,
		storage: bst,
		log:     log,
		pubsub:  pubsubs,
	}
}

func (e *Executor) StartCommandLoop() error {
	for {
		mt, data, err := e.conn.Read()
		if err != nil {
			if mt == -1 {
				return err
			}

			return err
		}

		msg := &pubsub.Msg{}

		err = json.Unmarshal(data, msg)
		if err != nil {
			e.log.Error("error unmarshal message", "error", err)
			continue
		}

		switch msg.Command() {
		// handle leave
		case commands.Join:
			// associate connection with topics
			e.storage.Store(e.connID, msg.Topics())

			resp := &Response{
				Topic:   "@join",
				Payload: msg.Topics(),
			}

			packet, err := json.Marshal(resp)
			if err != nil {
				e.log.Error("error marshal the body", "error", err)
				continue
			}

			err = e.conn.Write(websocket.BinaryMessage, packet)
			if err != nil {
				e.log.Error("error writing payload to the connection", "payload", packet, "error", err)
				continue
			}

			err = e.pubsub[msg.Broker()].Subscribe(msg.Topics()...)
			if err != nil {
				e.log.Error("error subscribing to the provided topics", "topics", msg.Topics(), "error", err.Error())
				continue
			}

		// handle leave
		case commands.Leave:
			// remove associated connections from the storage
			e.storage.Remove(e.connID, msg.Topics())

			resp := &Response{
				Topic:   "@leave",
				Payload: msg.Topics(),
			}

			packet, err := json.Marshal(resp)
			if err != nil {
				e.log.Error("error marshal the body", "error", err)
				continue
			}

			err = e.pubsub[msg.Broker()].Unsubscribe(msg.Topics()...)
			if err != nil {
				e.log.Error("error subscribing to the provided topics", "topics", msg.Topics(), "error", err.Error())
				continue
			}

			err = e.conn.Write(websocket.BinaryMessage, packet)
			if err != nil {
				e.log.Error("error writing payload to the connection", "payload", packet, "error", err)
				continue
			}

		case commands.Headers:

		default:
			e.log.Warn("unknown command", "command", msg.Command())
		}
	}
}