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"
|
|
|
|
"cwtch.im/cwtch/storage"
|
2018-03-09 20:44:13 +00:00
|
|
|
"github.com/s-rah/go-ricochet/application"
|
|
|
|
"github.com/s-rah/go-ricochet/channels"
|
|
|
|
)
|
|
|
|
|
2018-05-16 21:11:04 +00:00
|
|
|
// Instance encapsulates the Ricochet application.
|
2018-03-14 22:23:35 +00:00
|
|
|
type Instance struct {
|
2018-03-09 20:44:13 +00:00
|
|
|
rai *application.ApplicationInstance
|
|
|
|
ra *application.RicochetApplication
|
|
|
|
msi storage.MessageStoreInterface
|
|
|
|
}
|
|
|
|
|
2018-05-16 21:11:04 +00:00
|
|
|
// Init sets up a Server Instance
|
2018-03-14 22:23:35 +00:00
|
|
|
func (si *Instance) Init(rai *application.ApplicationInstance, 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()
|
|
|
|
}
|
|
|
|
|
2018-05-16 21:11:04 +00:00
|
|
|
// HandleGroupMessage takes ina 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)
|
|
|
|
go si.ra.Broadcast(func(rai *application.ApplicationInstance) {
|
|
|
|
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
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|