2018-03-09 20:44:13 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2018-05-28 18:05:06 +00:00
|
|
|
"cwtch.im/cwtch/server/fetch"
|
|
|
|
"cwtch.im/cwtch/server/listen"
|
|
|
|
"cwtch.im/cwtch/server/send"
|
|
|
|
"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"
|
|
|
|
"log"
|
|
|
|
)
|
|
|
|
|
2018-05-16 21:11:04 +00:00
|
|
|
// Server encapsulates a complete, compliant Cwtch server.
|
2018-03-09 20:44:13 +00:00
|
|
|
type Server struct {
|
2018-05-30 18:42:17 +00:00
|
|
|
app *application.RicochetApplication
|
2018-06-18 22:57:21 +00:00
|
|
|
config *Config
|
2018-03-09 20:44:13 +00:00
|
|
|
}
|
|
|
|
|
2018-06-03 19:36:20 +00:00
|
|
|
// Run starts a server with the given privateKey
|
2018-05-16 21:11:04 +00:00
|
|
|
// TODO: surface errors
|
2018-06-18 22:57:21 +00:00
|
|
|
func (s *Server) Run(serverConfig *Config) {
|
|
|
|
s.config = serverConfig
|
2018-03-09 20:44:13 +00:00
|
|
|
cwtchserver := new(application.RicochetApplication)
|
|
|
|
|
2018-06-18 22:57:21 +00:00
|
|
|
l, err := application.SetupOnion("127.0.0.1:9051", "tcp4", "", s.config.PrivateKey(), 9878)
|
2018-03-09 20:44:13 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("error setting up onion service: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
af := application.ApplicationInstanceFactory{}
|
|
|
|
af.Init()
|
|
|
|
ms := new(storage.MessageStore)
|
2018-06-18 22:57:21 +00:00
|
|
|
ms.Init("cwtch.messages", s.config.MaxBufferLines)
|
2018-03-09 20:44:13 +00:00
|
|
|
af.AddHandler("im.cwtch.server.listen", func(rai *application.ApplicationInstance) func() channels.Handler {
|
2018-03-14 22:23:35 +00:00
|
|
|
si := new(Instance)
|
2018-03-09 20:44:13 +00:00
|
|
|
si.Init(rai, cwtchserver, ms)
|
|
|
|
return func() channels.Handler {
|
|
|
|
cslc := new(listen.CwtchServerListenChannel)
|
|
|
|
return cslc
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
af.AddHandler("im.cwtch.server.fetch", func(rai *application.ApplicationInstance) func() channels.Handler {
|
2018-03-14 22:23:35 +00:00
|
|
|
si := new(Instance)
|
2018-03-09 20:44:13 +00:00
|
|
|
si.Init(rai, cwtchserver, ms)
|
|
|
|
return func() channels.Handler {
|
|
|
|
cssc := new(fetch.CwtchServerFetchChannel)
|
|
|
|
cssc.Handler = si
|
|
|
|
return cssc
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
af.AddHandler("im.cwtch.server.send", func(rai *application.ApplicationInstance) func() channels.Handler {
|
2018-03-14 22:23:35 +00:00
|
|
|
si := new(Instance)
|
2018-03-09 20:44:13 +00:00
|
|
|
si.Init(rai, cwtchserver, ms)
|
|
|
|
return func() channels.Handler {
|
|
|
|
cssc := new(send.CwtchServerSendChannel)
|
|
|
|
cssc.Handler = si
|
|
|
|
return cssc
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2018-06-18 22:57:21 +00:00
|
|
|
cwtchserver.Init("cwtch server for "+l.Addr().String()[0:16], s.config.PrivateKey(), af, new(application.AcceptAllContactManager))
|
2018-03-30 21:16:51 +00:00
|
|
|
log.Printf("cwtch server running on cwtch:%s", l.Addr().String()[0:16])
|
2018-05-30 18:42:17 +00:00
|
|
|
s.app = cwtchserver
|
2018-06-18 22:57:21 +00:00
|
|
|
s.app.Run(l)
|
2018-03-09 20:44:13 +00:00
|
|
|
}
|
2018-05-30 18:42:17 +00:00
|
|
|
|
2018-06-15 16:21:07 +00:00
|
|
|
// Shutdown kills the app closing all connections and freeing all goroutines
|
2018-05-30 18:42:17 +00:00
|
|
|
func (s *Server) Shutdown() {
|
|
|
|
s.app.Shutdown()
|
|
|
|
}
|