Пример #1
0
/*
Load tries to load the given directory as a bootstrap object, allowing it to
connect to an existing network. To actually start bootstrapping call
Bootstrap.Start(address). NOTE: will fail if already connected to other peers!
*/
func Load(path string, f Success) (*Bootstrap, error) {
	// we need to do some logic to detect where we can load stuff from
	trusted, err := isLoadable(path)
	if err != nil {
		return nil, err
	}
	// create object
	boot := &Bootstrap{
		path:   path,
		onDone: f}
	boot.cInterface = createChanInterface(boot)
	// load self peer from correct location
	var toxPeerDump *shared.ToxPeerDump
	if trusted {
		toxPeerDump, err = shared.LoadToxDumpFrom(path + "/" + shared.STORETOXDUMPDIR)
	} else {
		toxPeerDump, err = shared.LoadToxDumpFrom(path + "/" + shared.LOCALDIR)
	}
	if err != nil {
		return nil, err
	}
	boot.peer = toxPeerDump.SelfPeer
	channel, err := channel.Create(boot.peer.Name, toxPeerDump.ToxData, boot.cInterface)
	if err != nil {
		return nil, err
	}
	boot.channel = channel
	// bg stuff
	boot.wg.Add(1)
	boot.stop = make(chan bool, 1)
	go boot.run()
	return boot, nil
}
Пример #2
0
/*
Load returns the Encrypted structure for an existing instance.
*/
func Load(path string, storage Storage) (*Encrypted, error) {
	// TODO missing check whether this is a valid path... FIXME
	// make missing dirs if path ok? createEncryptedDirectories(path)
	// ensure valid parameters
	if path == "" || storage == nil {
		return nil, shared.ErrIllegalParameters
	}
	// build structure
	encrypted := &Encrypted{
		RootPath: path,
		storage:  storage}
	// prepare interface
	encrypted.cInterface = createChanInterface(encrypted)
	// load data
	selfPeer, err := shared.LoadToxDumpFrom(path + "/" + shared.LOCALDIR)
	if err != nil {
		return nil, err
	}
	// set self peer
	encrypted.Peer = selfPeer.SelfPeer
	// build channel
	encrypted.channel, err = channel.Create(encrypted.Peer.Name, selfPeer.ToxData, encrypted.cInterface)
	if err != nil {
		return nil, err
	}
	// run background
	initialize(encrypted)
	// return instance
	return encrypted, nil
}