make it compile

This commit is contained in:
Jeff Becker
2016-01-28 10:46:09 -05:00
parent 67d49585af
commit 5cf389a79d
8 changed files with 110 additions and 32 deletions

View File

@ -1,11 +0,0 @@
package bootstrap
import (
"github.com/bounce-chat/go-i2p/lib/stdi2p"
)
type Reseed interface {
// do reseed, return nil on success otherwise error
// sends down all retrieved SU3 files down chan
Reseed(chnl chan *stdi2p.SU3) error
}

View File

@ -1,12 +0,0 @@
package bootstrap
import (
"github.com/bounce-chat/go-i2p/lib/stdi2p"
)
type HTTPSReseed string
func (r HTTPSReseed) Reseed(chnl chan *stdi2p.SU3) (err error) {
return
}

14
lib/netdb/bootstrap.go Normal file
View File

@ -0,0 +1,14 @@
package netdb
type Reseed interface {
// do reseed, return nil on success otherwise error
// sends down all Netdb entries down chan
// closes channel when done
Reseed(chnl chan *Entry) error
}
func GetRandomReseed() Reseed {
// TODO: hardcoded value
return HTTPSReseed("https://i2p.rocks:445/")
}

View File

@ -1 +1,18 @@
package netdb
import (
"io"
"path/filepath"
)
type Entry struct {
fname string
}
func (e *Entry) FilePath(n StdNetDB) (str string) {
return filepath.Join(string(n), e.fname)
}
func (e *Entry) WriteTo(w io.Writer) (err error) {
return
}

8
lib/netdb/reseed.go Normal file
View File

@ -0,0 +1,8 @@
package netdb
type HTTPSReseed string
func (r HTTPSReseed) Reseed(chnl chan *Entry) (err error) {
close(chnl)
return
}

View File

@ -1,10 +1,10 @@
package netdb
import (
"github.com/golang/glog"
"github.com/majestrate/goi2p/lib/common"
log "github.com/golang/glog"
"github.com/bounce-chat/go-i2p/lib/common"
"io"
"os"
"time"
)
// standard network database implementation
@ -28,9 +28,67 @@ func (db StdNetDB) Exists() bool {
return common.FileExists(db.Path())
}
func (db StdNetDB) SaveEntry(e *Entry) (err error) {
var f io.WriteCloser
f, err = os.OpenFile(e.FilePath(db), os.O_WRONLY, 0600)
if err == nil {
err = e.WriteTo(f)
if err != nil {
log.Errorf("failed to write netdb entry: %s", err.Error())
}
f.Close()
} else {
log.Errorf("failed to save netdb entry: %s", err.Error())
}
return
}
// reseed if we have less than minRouters known routers
// returns error if reseed failed
func (db StdNetDB) Reseed(minRouters int) (err error) {
current := db.KnownPeerCount()
if current <= minRouters {
// we need to reseed
rs := GetRandomReseed()
log.Infof("Reseeding from %s", rs)
chnl := make(chan *Entry)
// receive entries from reseed
go func(c chan *Entry) {
count := 0
for {
e, ok := <- c
if ok {
// got an entry
// save it to our netdb
err := db.SaveEntry(e)
if err == nil {
count ++
}
}
}
}(chnl) // call
err = rs.Reseed(chnl)
}
return
}
// ensure that the network database exists and is seeded with a minimum number of routers
func (db StdNetDB) Ensure(minRouters int) (err error) {
if ! db.Exists() {
err = db.Create()
}
if err == nil {
// database directory ensured
// try to reseed
err = db.Reseed(minRouters)
}
return
}
// create base network database directory
func (db StdNetDB) Create() (err error) {
glog.Infof("Create network database in %s", db.Path())
log.Infof("Create network database in %s", db.Path())
err = os.Mkdir(db.Path(), 0600)
return
}

View File

@ -23,5 +23,9 @@ func CreateRouter() (r *Router, err error) {
// run i2p router mainloop
func (r *Router) Run() {
r.ndb.Run()
// make sure the netdb is ready
err := r.ndb.Ensure(r.cfg.Bootstrap.LowPeerThreshold)
if err == nil {
// netdb ready
}
}

View File

@ -4,7 +4,7 @@ import (
"github.com/bounce-chat/go-i2p/lib/router"
"github.com/golang/glog"
log "github.com/golang/glog"
"flag"
)
@ -14,13 +14,13 @@ func main() {
flag.Parse()
glog.Info("parsing i2p router configuration")
log.Info("parsing i2p router configuration")
glog.Info("starting up i2p router")
log.Info("starting up i2p router")
r, err := router.CreateRouter()
if err == nil {
r.Run()
} else {
glog.Errorf("failed to create i2p router: %s", err)
log.Errorf("failed to create i2p router: %s", err)
}
}