コード例 #1
0
ファイル: bstarsrv2.go プロジェクト: rrudduck/golang-stuff
func main() {
	//  Arguments can be either of:
	//      -p  primary server, at tcp://localhost:5001
	//      -b  backup server, at tcp://localhost:5002
	var bst *bstar.Bstar
	if len(os.Args) == 2 && os.Args[1] == "-p" {
		fmt.Println("I: Primary active, waiting for backup (passive)")
		bst, _ = bstar.New(bstar.PRIMARY, "tcp://*:5003", "tcp://localhost:5004")
		bst.Voter("tcp://*:5001", zmq.ROUTER, echo)
	} else if len(os.Args) == 2 && os.Args[1] == "-b" {
		fmt.Println("I: Backup passive, waiting for primary (active)")
		bst, _ = bstar.New(bstar.BACKUP, "tcp://*:5004", "tcp://localhost:5003")
		bst.Voter("tcp://*:5002", zmq.ROUTER, echo)
	} else {
		fmt.Println("Usage: bstarsrvs { -p | -b }")
		return
	}
	bst.Start()
}
コード例 #2
0
ファイル: clonesrv6.go プロジェクト: rrudduck/golang-stuff
func main() {
	var err error

	srv := &clonesrv_t{}

	if len(os.Args) == 2 && os.Args[1] == "-p" {
		log.Println("I: primary active, waiting for backup (passive)")
		srv.bstar, err = bstar.New(bstar.PRIMARY, "tcp://*:5003", "tcp://localhost:5004")
		if err != nil {
			log.Println(err)
			return
		}
		srv.bstar.Voter("tcp://*:5556", zmq.ROUTER, func(soc *zmq.Socket) error { return snapshots(soc, srv) })
		srv.port = 5556
		srv.peer = 5566
		srv.primary = true
	} else if len(os.Args) == 2 && os.Args[1] == "-b" {
		log.Println("I: backup passive, waiting for primary (active)")
		srv.bstar, err = bstar.New(bstar.BACKUP, "tcp://*:5004", "tcp://localhost:5003")
		srv.bstar.Voter("tcp://*:5566", zmq.ROUTER, func(soc *zmq.Socket) error { return snapshots(soc, srv) })
		srv.port = 5566
		srv.peer = 5556
		srv.primary = false
	} else {
		fmt.Println("Usage: clonesrv4 { -p | -b }")
		return
	}
	//  Primary server will become first active
	if srv.primary {
		srv.kvmap = make(map[string]*kvmsg.Kvmsg, 0)
		srv.kvmap_init = true
	}

	srv.pending = make([]*kvmsg.Kvmsg, 0)
	srv.bstar.SetVerbose(true)

	//  Set up our clone server sockets
	srv.publisher, _ = zmq.NewSocket(zmq.PUB)
	srv.collector, _ = zmq.NewSocket(zmq.SUB)
	srv.collector.SetSubscribe("")
	srv.publisher.Bind(fmt.Sprint("tcp://*:", srv.port+1))
	srv.collector.Bind(fmt.Sprint("tcp://*:", srv.port+2))

	//  Set up our own clone client interface to peer
	srv.subscriber, _ = zmq.NewSocket(zmq.SUB)
	srv.subscriber.SetSubscribe("")
	srv.subscriber.Connect(fmt.Sprint("tcp://localhost:", srv.peer+1))

	//  After we've set-up our sockets we register our binary star
	//  event handlers, and then start the bstar reactor. This finishes
	//  when the user presses Ctrl-C, or the process receives a SIGINT
	//  interrupt:

	//  Register state change handlers
	srv.bstar.NewActive(func() error { return new_active(srv) })
	srv.bstar.NewPassive(func() error { return new_passive(srv) })

	//  Register our other handlers with the bstar reactor
	srv.bstar.Reactor.AddSocket(srv.collector, zmq.POLLIN,
		func(e zmq.State) error { return collector(srv) })
	srv.bstar.Reactor.AddChannelTime(time.Tick(1000*time.Millisecond), 1,
		func(i interface{}) error {
			if e := flush_ttl(srv); e != nil {
				return e
			}
			return send_hugz(srv)
		})

	err = srv.bstar.Start()
	log.Println(err)
}