2023-02-24 14:40:28 +01:00
|
|
|
package net
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"forge.cadoles.com/arcad/edge/pkg/app"
|
|
|
|
"forge.cadoles.com/arcad/edge/pkg/bus"
|
2023-11-28 16:35:49 +01:00
|
|
|
edgehttp "forge.cadoles.com/arcad/edge/pkg/http"
|
2023-02-24 14:40:28 +01:00
|
|
|
"forge.cadoles.com/arcad/edge/pkg/module/util"
|
|
|
|
"github.com/dop251/goja"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"gitlab.com/wpetit/goweb/logger"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Module struct {
|
|
|
|
server *app.Server
|
|
|
|
bus bus.Bus
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Module) Name() string {
|
|
|
|
return "net"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Module) Export(export *goja.Object) {
|
|
|
|
if err := export.Set("broadcast", m.broadcast); err != nil {
|
|
|
|
panic(errors.Wrap(err, "could not set 'broadcast' function"))
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := export.Set("send", m.send); err != nil {
|
|
|
|
panic(errors.Wrap(err, "could not set 'send' function"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Module) broadcast(call goja.FunctionCall, rt *goja.Runtime) goja.Value {
|
|
|
|
if len(call.Arguments) < 1 {
|
|
|
|
panic(rt.ToValue(errors.New("invalid number of argument")))
|
|
|
|
}
|
|
|
|
|
|
|
|
data := call.Argument(0).Export()
|
|
|
|
|
2023-11-28 16:35:49 +01:00
|
|
|
env := edgehttp.NewOutgoingMessageEnvelope("", data)
|
|
|
|
if err := m.bus.Publish(env); err != nil {
|
2023-02-24 14:40:28 +01:00
|
|
|
panic(rt.ToValue(errors.WithStack(err)))
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Module) send(call goja.FunctionCall, rt *goja.Runtime) goja.Value {
|
|
|
|
if len(call.Arguments) < 2 {
|
|
|
|
panic(rt.ToValue(errors.New("invalid number of argument")))
|
|
|
|
}
|
|
|
|
|
|
|
|
firstArg := call.Argument(0)
|
|
|
|
|
|
|
|
sessionID, ok := firstArg.Export().(string)
|
2023-11-28 16:35:49 +01:00
|
|
|
if !ok {
|
|
|
|
ctx := util.AssertContext(firstArg, rt)
|
2023-11-30 19:09:51 +01:00
|
|
|
sessionID, ok = edgehttp.ContextSessionID(ctx)
|
|
|
|
if !ok {
|
|
|
|
panic(rt.ToValue(errors.New("could not find session id in context")))
|
|
|
|
}
|
2023-02-24 14:40:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
data := call.Argument(1).Export()
|
|
|
|
|
2023-11-28 16:35:49 +01:00
|
|
|
env := edgehttp.NewOutgoingMessageEnvelope(sessionID, data)
|
|
|
|
if err := m.bus.Publish(env); err != nil {
|
2023-02-24 14:40:28 +01:00
|
|
|
panic(rt.ToValue(errors.WithStack(err)))
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-11-28 16:35:49 +01:00
|
|
|
func (m *Module) handleIncomingMessages() {
|
2023-02-24 14:40:28 +01:00
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
logger.Debug(
|
|
|
|
ctx,
|
2023-11-28 16:35:49 +01:00
|
|
|
"subscribing to bus envelopes",
|
2023-02-24 14:40:28 +01:00
|
|
|
)
|
|
|
|
|
2023-11-28 16:35:49 +01:00
|
|
|
envelopes, err := m.bus.Subscribe(ctx, edgehttp.AddressIncomingMessage)
|
2023-02-24 14:40:28 +01:00
|
|
|
if err != nil {
|
|
|
|
panic(errors.WithStack(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
logger.Debug(
|
|
|
|
ctx,
|
2023-11-28 16:35:49 +01:00
|
|
|
"unsubscribing from bus envelopes",
|
2023-02-24 14:40:28 +01:00
|
|
|
)
|
|
|
|
|
2023-11-28 16:35:49 +01:00
|
|
|
m.bus.Unsubscribe(edgehttp.AddressIncomingMessage, envelopes)
|
2023-02-24 14:40:28 +01:00
|
|
|
}()
|
|
|
|
|
|
|
|
for {
|
|
|
|
logger.Debug(
|
|
|
|
ctx,
|
2023-11-28 16:35:49 +01:00
|
|
|
"waiting for next envelope",
|
2023-02-24 14:40:28 +01:00
|
|
|
)
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
logger.Debug(
|
|
|
|
ctx,
|
|
|
|
"context done",
|
|
|
|
)
|
|
|
|
|
|
|
|
return
|
|
|
|
|
2023-11-28 16:35:49 +01:00
|
|
|
case env := <-envelopes:
|
|
|
|
incomingMessage, ok := env.Message().(*edgehttp.IncomingMessage)
|
2023-02-24 14:40:28 +01:00
|
|
|
if !ok {
|
2023-10-24 22:52:33 +02:00
|
|
|
logger.Warn(
|
2023-02-24 14:40:28 +01:00
|
|
|
ctx,
|
|
|
|
"unexpected message type",
|
2023-11-28 16:35:49 +01:00
|
|
|
logger.F("message", env.Message()),
|
2023-02-24 14:40:28 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.Debug(
|
|
|
|
ctx,
|
2023-11-28 16:35:49 +01:00
|
|
|
"received incoming message",
|
|
|
|
logger.F("message", incomingMessage),
|
2023-02-24 14:40:28 +01:00
|
|
|
)
|
|
|
|
|
2023-11-28 16:35:49 +01:00
|
|
|
if _, err := m.server.ExecFuncByName(incomingMessage.Context, "onClientMessage", incomingMessage.Context, incomingMessage.Payload); err != nil {
|
2023-02-24 14:40:28 +01:00
|
|
|
if errors.Is(err, app.ErrFuncDoesNotExist) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.Error(
|
|
|
|
ctx,
|
|
|
|
"on client message error",
|
2023-10-19 21:47:09 +02:00
|
|
|
logger.CapturedE(errors.WithStack(err)),
|
2023-02-24 14:40:28 +01:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ModuleFactory(bus bus.Bus) app.ServerModuleFactory {
|
|
|
|
return func(server *app.Server) app.ServerModule {
|
|
|
|
module := &Module{
|
|
|
|
server: server,
|
|
|
|
bus: bus,
|
|
|
|
}
|
|
|
|
|
2023-11-28 16:35:49 +01:00
|
|
|
go module.handleIncomingMessages()
|
2023-02-24 14:40:28 +01:00
|
|
|
|
|
|
|
return module
|
|
|
|
}
|
|
|
|
}
|