* add Router.Start()

* remove Router.Run()
This commit is contained in:
Jeff Becker
2017-08-27 11:02:37 -04:00
parent aeb33c27aa
commit 993c006de3
2 changed files with 22 additions and 3 deletions

View File

@ -12,6 +12,7 @@ type Router struct {
cfg *config.RouterConfig
ndb netdb.StdNetDB
closeChnl chan bool
running bool
}
// create router with default configuration
@ -29,20 +30,37 @@ func FromConfig(c *config.RouterConfig) (r *Router, err error) {
return
}
// Wait blocks until router is fully stopped
func (r *Router) Wait() {
<-r.closeChnl
}
// Stop starts stopping internal state of router
func (r *Router) Stop() {
r.closeChnl <- true
r.running = false
}
// Close closes any internal state and finallizes router resources so that nothing can start up again
func (r *Router) Close() error {
return nil
}
// Start starts router mainloop
func (r *Router) Start() {
if r.running {
log.WithFields(log.Fields{
"at": "(Router) Start",
"reason": "router is already running",
}).Error("Error Starting router")
return
}
r.running = true
go r.mainloop()
}
// run i2p router mainloop
func (r *Router) Run() {
func (r *Router) mainloop() {
r.ndb = netdb.StdNetDB(r.cfg.NetDb.Path)
// make sure the netdb is ready
err := r.ndb.Ensure()
@ -60,5 +78,6 @@ func (r *Router) Run() {
"at": "(Router) Run",
"reason": err.Error(),
}).Error("Netdb Startup failed")
r.Stop()
}
}

View File

@ -20,9 +20,9 @@ func main() {
// TODO: graceful shutdown
r.Stop()
})
go r.Run()
defer r.Close()
r.Start()
r.Wait()
r.Close()
} else {
log.Errorf("failed to create i2p router: %s", err)
}