Exemple #1
0
func main() {
	flag.Parse()
	log.SetFormatter(&ServerFormatter{})

	pki := ReadPKI(*pkiPath)

	firstServer, err := vrpc.Dial("tcp", pki.FirstServer(), runtime.NumCPU())
	if err != nil {
		log.Fatalf("vrpc.Dial: %s", err)
	}

	lastServer, err := vrpc.Dial("tcp", pki.LastServer(), 1)
	if err != nil {
		log.Fatalf("vrpc.Dial: %s", err)
	}

	srv := &server{
		firstServer:   firstServer,
		lastServer:    lastServer,
		connections:   make(map[*connection]bool),
		convoRound:    0,
		convoRequests: make([]*convoReq, 0, 10000),
		dialRound:     0,
		dialRequests:  make([]*dialReq, 0, 10000),
	}

	go srv.convoRoundLoop()
	go srv.dialRoundLoop()

	http.HandleFunc("/ws", srv.wsHandler)

	httpServer := &http.Server{
		Addr: *addr,
	}

	if err := httpServer.ListenAndServe(); err != nil {
		log.Fatal("ListenAndServe: ", err)
	}
}
Exemple #2
0
func main() {
	flag.Parse()
	log.SetFormatter(&ServerFormatter{})

	if *confPath == "" {
		log.Fatalf("must specify -conf flag")
	}

	if *doInit {
		WriteDefaultConf(*confPath)
		return
	}

	pki := ReadPKI(*pkiPath)

	conf := new(Conf)
	ReadJSONFile(*confPath, conf)
	if conf.ServerName == "" || conf.PublicKey == nil || conf.PrivateKey == nil {
		log.Fatalf("missing required fields: %s", *confPath)
	}

	if *muOverride >= 0 {
		conf.ConvoMu = *muOverride
	}

	var err error
	var client *vrpc.Client
	if addr := pki.NextServer(conf.ServerName); addr != "" {
		client, err = vrpc.Dial("tcp", addr, runtime.NumCPU())
		if err != nil {
			log.Fatalf("vrpc.Dial: %s", err)
		}
	}

	var idle sync.Mutex

	convoService := &ConvoService{
		Idle: &idle,

		LaplaceMu: conf.ConvoMu,
		LaplaceB:  conf.ConvoB,

		PKI:        pki,
		ServerName: conf.ServerName,
		PrivateKey: conf.PrivateKey,

		Client:     client,
		LastServer: client == nil,
	}
	InitConvoService(convoService)

	if convoService.LastServer {
		histogram := &Histogram{Mu: conf.ConvoMu, NumServers: len(pki.ServerOrder)}
		go histogram.run(convoService.AccessCounts)
	}

	dialService := &DialService{
		Idle: &idle,

		LaplaceMu: conf.DialMu,
		LaplaceB:  conf.DialB,

		PKI:        pki,
		ServerName: conf.ServerName,
		PrivateKey: conf.PrivateKey,

		Client:     client,
		LastServer: client == nil,
	}
	InitDialService(dialService)

	if err := rpc.Register(dialService); err != nil {
		log.Fatalf("rpc.Register: %s", err)
	}
	if err := rpc.Register(convoService); err != nil {
		log.Fatalf("rpc.Register: %s", err)
	}

	if conf.DebugAddr != "" {
		go func() {
			log.Println(http.ListenAndServe(conf.DebugAddr, nil))
		}()
		runtime.SetBlockProfileRate(1)
	}

	if conf.ListenAddr == "" {
		conf.ListenAddr = DefaultServerAddr
	}
	listen, err := net.Listen("tcp", conf.ListenAddr)
	if err != nil {
		log.Fatal("Listen:", err)
	}
	rpc.Accept(listen)
}