cwtch/server/server_instance.go

46 lines
1.3 KiB
Go
Raw Normal View History

2018-03-09 20:44:13 +00:00
package server
import (
2018-05-28 18:05:06 +00:00
"cwtch.im/cwtch/protocol"
"cwtch.im/cwtch/server/listen"
2019-01-21 20:11:40 +00:00
"cwtch.im/cwtch/server/storage"
2018-06-23 16:15:36 +00:00
"git.openprivacy.ca/openprivacy/libricochet-go/application"
"git.openprivacy.ca/openprivacy/libricochet-go/channels"
2018-03-09 20:44:13 +00:00
)
2018-05-16 21:11:04 +00:00
// Instance encapsulates the Ricochet application.
2018-03-14 22:23:35 +00:00
type Instance struct {
2019-01-23 20:50:53 +00:00
rai *application.Instance
2018-03-09 20:44:13 +00:00
ra *application.RicochetApplication
msi storage.MessageStoreInterface
}
2018-05-16 21:11:04 +00:00
// Init sets up a Server Instance
2019-01-23 20:50:53 +00:00
func (si *Instance) Init(rai *application.Instance, ra *application.RicochetApplication, msi storage.MessageStoreInterface) {
2018-03-09 20:44:13 +00:00
si.rai = rai
si.ra = ra
si.msi = msi
}
2018-05-16 21:11:04 +00:00
// HandleFetchRequest returns a list of all messages in the servers buffer
2018-03-14 22:23:35 +00:00
func (si *Instance) HandleFetchRequest() []*protocol.GroupMessage {
2018-03-09 20:44:13 +00:00
return si.msi.FetchMessages()
}
// HandleGroupMessage takes in a group message and distributes it to all listening peers
2018-03-14 22:23:35 +00:00
func (si *Instance) HandleGroupMessage(gm *protocol.GroupMessage) {
2018-03-09 20:44:13 +00:00
si.msi.AddMessage(*gm)
2019-01-23 20:50:53 +00:00
go si.ra.Broadcast(func(rai *application.Instance) {
2018-03-09 20:44:13 +00:00
rai.Connection.Do(func() error {
channel := rai.Connection.Channel("im.cwtch.server.listen", channels.Inbound)
if channel != nil {
cslc, ok := channel.Handler.(*listen.CwtchServerListenChannel)
if ok {
cslc.SendGroupMessage(gm)
}
}
return nil
})
})
}