示例#1
0
文件: daemon.go 项目: mad3310/vrouter
func (d *Daemon) listRoute() ([]*Route, uint64, error) {
	var index uint64
	var err error

	routes := make([]*Route, 0, 256)
	client := d.etcdClient

	routerPath := registry.RouterRoutesPrefix()
	if resp, err := client.Get(routerPath, false, true); err != nil {
		return routes, index, err
	} else {
		index = resp.EtcdIndex
		hosts := resp.Node.Nodes
		for _, host := range hosts {
			if hostKey := host.Key; strings.HasSuffix(hostKey, d.Hostname) {
				continue
			}
			value := host.Value
			if r, err := ParseRoute(value); err != nil {
				log.Printf("error to parse route: %s", err)
				continue
			} else {
				routes = append(routes, r)
			}
		}
	}

	log.Printf("Routes is %v", routes)
	return routes, index, err
}
示例#2
0
文件: daemon.go 项目: mad3310/vrouter
func (d *Daemon) ManageRoute() error {
	routes, etcdindex, err := d.listRoute()
	if err != nil {
		return err
	}

	for _, r := range routes {
		err = r.AddRoute(d.iface)
		if err != nil {
			log.Printf("error to add route: %s", err)
		}
	}

	receiver := make(chan *etcd.Response, 4)
	client := d.etcdClient

	go client.Watch(registry.RouterRoutesPrefix(), etcdindex, true, receiver, nil)

	//log.Printf("Watching or %s", registry.RouterHostsPrefix())

	for resp := range receiver {
		host := resp.Node
		log.Printf("%v", host.Key)
		//if hostKey := host.Key; strings.HasSuffix(hostKey, d.Hostname) {
		//	continue
		//}
		value := host.Value
		r, err := ParseRoute(value)
		if err != nil {
			log.Printf("error to parse route: %s", err)
			continue
		}

		if err = r.AddRoute(d.iface); err != nil {
			log.Printf("AddRoute error(%v): %s", r, err)
		}
	}

	return nil
}