Esempio n. 1
0
File: self.go Progetto: utamaro/core
//NewSelf make repo if needed ,starts daemon and returns Self obj.
func NewSelf(cfg *config.Config, rootPath string) *Self {
	InitRepo(cfg.IpfsRepo)
	n := &merkledag.Node{}
	k, err := n.Key()
	log.IfFatal(err)
	//workaround "ERROR    bitswap: failed to find any peer in table"
	i := 0
	var node *core.IpfsNode
	var ctx context.Context
	var cancel context.CancelFunc
	for i = 0; i < 10; i++ {
		log.Println("setting up node...")
		r, err := fsrepo.Open(cfg.IpfsRepo)
		log.IfFatal(err)
		ctx, cancel = context.WithCancel(context.Background())
		node, err = core.NewNode(ctx, &core.BuildCfg{
			Repo:   r,
			Online: true,
		})
		log.IfFatal(err)
		if err := node.Routing.Provide(ctx, k); log.If(err) {
			cancel()
			log.If(node.Close())
			log.Println("retrying...")
			continue
		}
		break
	}
	if i == 10 {
		log.Fatal("cannot provide a key to network")
	}
	self := &Self{
		RootPath: rootPath,
		ipfsNode: node,
		ctx:      ctx,
		cancel:   cancel,
		cfg:      cfg,
	}
	self.follow = FromStringSlice(cfg.FollowPeers, self)
	parent, err := self.ToPeer().GetDAGNode("")
	if log.If(err) {
		parent = &merkledag.Node{}
	}
	self.myIpns, err = parent.Key()
	log.IfFatal(err)
	if _, err = parent.GetNodeLink(rootPath); err != nil {
		log.Println("initializing DAGs for saving status")
		self.makeInitNodes(parent)
	}
	return self
}
Esempio n. 2
0
File: self.go Progetto: utamaro/core
//LoadInitPeers loads init peers from the setting.
func (m *Self) LoadInitPeers() Peers {
	peers := make(Peers)
	for _, p := range m.cfg.InitPeers.GetData() {
		pe, errr := New(p, m)
		log.Println("peer name", p)
		log.IfFatal(errr)
		peers.Add(pe)
	}
	log.Println("loaded init peers")
	if len(peers) == 0 {
		log.Println("Warning: no peers in init_peers.txt")
	}
	err := m.WritePeers(peers)
	log.IfFatal(err)
	return peers
}
Esempio n. 3
0
//update read the file and regexp.comples each lines in the file if file is newer.
func (r *RegexpList) update() bool {
	should, _ := shouldUpdate(r.path, r.mtime)
	if !should {
		return false
	}
	var spamReg []map[string]string
	dat, err := ioutil.ReadFile(r.path)
	log.IfFatal(err)
	log.IfFatal(json.Unmarshal(dat, &spamReg))
	r.regs = make([]map[string]*regexp.Regexp, len(spamReg))
	for i, m := range spamReg {
		r.regs[i] = make(map[string]*regexp.Regexp)
		for k, v := range m {
			r.regs[i][k] = regexp.MustCompile(v)
		}
	}
	return true
}
Esempio n. 4
0
File: peer.go Progetto: utamaro/core
//FromStringSlice returns Peer list from string slice.
func FromStringSlice(strs []string, self *Self) Peers {
	peers := make(Peers)
	for _, line := range strs {
		line = strings.TrimSpace(line)
		p, err := New(line, self)
		log.IfFatal(err)
		peers.Add(p)
	}
	return peers
}
Esempio n. 5
0
File: self.go Progetto: utamaro/core
func (m *Self) makeInitNodes(parent *merkledag.Node) {
	n := &merkledag.Node{}
	log.IfFatal(n.AddNodeLink(config.RecentPath, &merkledag.Node{}))
	log.IfFatal(n.AddNodeLink(config.PeersPath, &merkledag.Node{}))
	log.IfFatal(n.AddNodeLink(config.TagsPath, &merkledag.Node{}))
	log.IfFatal(n.AddNodeLink(config.SpamsPath, &merkledag.Node{}))
	log.IfFatal(parent.AddNodeLink(m.RootPath, n))
	var err error
	m.myIpns, err = m.AddDAGNode(parent, false)
	log.IfFatal(err)
	log.IfFatal(m.NamePublish())
}
Esempio n. 6
0
//update read the file if newer, and stores all lines in the file.
func (r *ConfList) update() bool {
	should, modtime := shouldUpdate(r.path, r.mtime)
	if !should {
		return false
	}
	r.mtime = modtime
	r.data = r.data[:0]
	dat, err := ioutil.ReadFile(r.path)
	log.IfFatal(err)
	for _, line := range strings.Split(string(dat), "\n") {
		line = strings.TrimSpace(line)
		if line != "" && !strings.HasPrefix(line, "#") {
			r.data = append(r.data, line)
		}
	}
	return true
}
Esempio n. 7
0
//Load fills a struct cfg which usually embeds Config struct from the json file.
func Load(appl, p string, cfg interface{}) {
	files := []string{p, "file", "/usr/local/etc/" + appl, "/etc/" + appl}
	usr, err := user.Current()
	if err == nil {
		files = append(files, path.Join(usr.HomeDir, "."+appl))
	}
	for _, f := range files {
		if f == "" {
			continue
		}
		f = filepath.Join(f, appl+".json")
		dat, errr := ioutil.ReadFile(f)
		if errr != nil {
			continue
		}
		log.Println("loading config from", f)
		log.IfFatal(json.Unmarshal(dat, cfg))
	}
}
Esempio n. 8
0
//InitRepo initizlizes IPFS repo.
func InitRepo(repo string) {
	log.IfFatal(doInit(os.Stdout, repo, false, true, nBitsForKeypairDefault))
}
Esempio n. 9
0
File: self.go Progetto: utamaro/core
//WriteThread writes threads names which are already read.
func (m *Self) WriteThread(threads slice.String) {
	tstr := strings.Join(threads.ToSlice(), "\n")
	fname := filepath.Join(m.cfg.RunDir, config.ReadThreadsTxt)
	err := ioutil.WriteFile(fname, []byte(tstr), 0644)
	log.IfFatal(err)
}
Esempio n. 10
0
File: self.go Progetto: utamaro/core
//ToPeer conerts self to Peer obj.
func (m *Self) ToPeer() *Peer {
	p, err := FromBytes([]byte(m.ipfsNode.Identity), m)
	log.IfFatal(err)
	return p
}