コード例 #1
0
ファイル: main.go プロジェクト: Tinzenite/bootstrap
/*
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
ファイル: main.go プロジェクト: Tinzenite/encrypted
/*
Create returns a new Encrypted instance, ready to be connected to an existing
network.
*/
func Create(path, peerName string, storage Storage) (*Encrypted, error) {
	// must start on empty directory
	if empty, err := shared.IsDirectoryEmpty(path); !empty {
		if err != nil {
			return nil, err
		}
		return nil, ErrNonEmpty
	}
	// ensure valid parameters
	if path == "" || peerName == "" || storage == nil {
		return nil, shared.ErrIllegalParameters
	}
	// flag whether we need to clen up after us
	var failed bool
	// make directories
	err := shared.MakeEncryptedDir(path)
	if err != nil {
		return nil, err
	}
	// if failed was set --> clean up by removing everything
	defer func() {
		if failed {
			shared.RemoveDirContents(path)
		}
	}()
	// build
	encrypted := &Encrypted{
		RootPath: path, // rootPath for storing root
		storage:  storage}
	// prepare chaninterface
	encrypted.cInterface = createChanInterface(encrypted)
	// build channel
	channel, err := channel.Create(peerName, nil, encrypted.cInterface)
	if err != nil {
		failed = true
		return nil, err
	}
	encrypted.channel = channel
	// get address for peer
	address, err := encrypted.channel.Address()
	if err != nil {
		failed = true
		return nil, err
	}
	// make peer with name, address, and set trusted to false
	peer, err := shared.CreatePeer(peerName, address, false)
	if err != nil {
		failed = true
		return nil, err
	}
	encrypted.Peer = peer
	// run background
	initialize(encrypted)
	// store initial copy
	err = encrypted.Store()
	if err != nil {
		failed = true
		return nil, err
	}
	// return instance
	return encrypted, nil
}