示例#1
0
/*
Create returns a struct that will allow to bootstrap to an existing Tinzenite
network. To actually start bootstrapping call Bootstrap.Start(address).

Path: the absolute path to the directory. localPeerName: the user defined name
of this peer. trusted: whether this should be a trusted peer or an encrypted
one. f: the callback to call once the bootstrap has successfully run.
*/
func Create(path, localPeerName string, trusted bool, f Success) (*Bootstrap, error) {
	if shared.IsTinzenite(path) {
		return nil, shared.ErrIsTinzenite
	}
	// build structure
	var err error
	if trusted {
		err = shared.MakeTinzeniteDir(path)
	} else {
		err = shared.MakeEncryptedDir(path)
	}
	// creation of structure error
	if err != nil {
		return nil, err
	}
	// create object
	boot := &Bootstrap{
		path:   path,
		onDone: f}
	boot.cInterface = createChanInterface(boot)
	channel, err := channel.Create(localPeerName, nil, boot.cInterface)
	if err != nil {
		return nil, err
	}
	boot.channel = channel
	// get address for peer
	address, err := boot.channel.Address()
	if err != nil {
		return nil, err
	}
	// make peer (at correct location!)
	peer, err := shared.CreatePeer(localPeerName, address, trusted)
	if err != nil {
		return nil, err
	}
	boot.peer = peer
	// bg stuff
	boot.wg.Add(1)
	boot.stop = make(chan bool, 1)
	go boot.run()
	return boot, nil
}
示例#2
0
/*
Store writes a bootstrapped .TINZENITEDIR to disk. Call this if you want
persistant bootstrapping (and why wouldn't you?).
*/
func (b *Bootstrap) Store() error {
	trusted := b.IsTrusted()
	var err error
	if trusted {
		err = shared.MakeTinzeniteDir(b.path)
	} else {
		err = shared.MakeEncryptedDir(b.path)
	}
	if err != nil {
		return err
	}
	// write self peer if TRUSTED peer. Encrypted don't write their own peer.
	if trusted {
		err = b.peer.StoreTo(b.path + "/" + shared.STOREPEERDIR)
	}
	if err != nil {
		return err
	}
	// store local peer info with toxdata
	toxData, err := b.channel.ToxData()
	if err != nil {
		return err
	}
	toxPeerDump := &shared.ToxPeerDump{
		SelfPeer: b.peer,
		ToxData:  toxData}
	// write toxpeerdump
	if trusted {
		err = toxPeerDump.StoreTo(b.path + "/" + shared.STORETOXDUMPDIR)
	} else {
		err = toxPeerDump.StoreTo(b.path + "/" + shared.LOCALDIR)
	}
	if err != nil {
		return err
	}
	return nil
}