Пример #1
0
func NewServer(local_addr string) (server *Server, err error) {
	server = new(Server)
	server.Server, err = protocols.NewServer(local_addr, MiniProtocol)
	server.Server.RegisterHandlers(protocols.ServerHandlerMap{
		CodeMeasureLatency: server.HandleMeasureLatency,
	})
	if err != nil {
		server = nil
	}
	return
}
Пример #2
0
func NewHeartbeatServer(local_addr string) (*HeartbeatServer, error) {
	heartbeatServer := &HeartbeatServer{
		detectors: make(map[int64]*HeartbeatFaultDetector),
	}
	if server, err := protocols.NewServer(local_addr, &serverStopper{MiniProtocol, heartbeatServer}); err == nil {
		heartbeatServer.Server = server
		if err = RegisterServerHandler(server, heartbeatServer); err == nil {
			return heartbeatServer, nil
		} else {
			return nil, err
		}
	} else {
		return nil, err
	}
}
Пример #3
0
func main() {
	payloadSize := flag.Uint("payload", 0, "Additional payload to append to Load packets")
	amp_addr := protocols.ParseServerFlags("0.0.0.0", 7770)

	proto, err := protocols.NewProtocol("AMP/Load", amp.Protocol, amp_control.Protocol, ping.Protocol, heartbeat.Protocol)
	golib.Checkerr(err)
	server, err := protocols.NewServer(amp_addr, proto)
	golib.Checkerr(err)
	loadServer, err := RegisterLoadServer(server)
	golib.Checkerr(err)
	loadServer.PayloadSize = *payloadSize

	go printErrors(server)

	log.Println("Listening:", server)
	log.Println("Press Ctrl-C to close")
	golib.NewTaskGroup(
		server,
		&golib.NoopTask{golib.ExternalInterrupt(), "external interrupt"},
	).WaitAndExit()
}
Пример #4
0
func main() {
	proxies.UdpProxyFlags()
	pcp_addr := protocols.ParseServerFlags("0.0.0.0", 7778)

	proto, err := protocols.NewProtocol("PCP", pcp.Protocol, ping.Protocol, heartbeat.Protocol)
	golib.Checkerr(err)
	server, err := protocols.NewServer(pcp_addr, proto)
	golib.Checkerr(err)
	proxy, err := proxies.RegisterPcpProxy(server)
	golib.Checkerr(err)

	go printPcpErrors(proxy)
	proxy.ProxyStartedCallback = proxyStarted
	proxy.ProxyStoppedCallback = proxyStopped

	log.Println("Listening:", server)
	log.Println("Press Ctrl-C to close")
	golib.NewTaskGroup(
		server,
		&golib.NoopTask{golib.ExternalInterrupt(), "external interrupt"},
	).WaitAndExit()
}
Пример #5
0
func startLoadClient() (port int) {
	port = start_rtp_port
	var server *protocols.Server
	var stats *load.LoadStats
	for {
		var err error
		server, err = protocols.NewServer(net.JoinHostPort(rtp_ip, strconv.Itoa(port)), load.MiniProtocol)
		if err != nil {
			log.Printf("Failed to start Load server on port %v: %v", port, err)
			port += 2
			continue
		}
		stats, err = load.RegisterServer(server)
		if err != nil {
			log.Printf("Failed to register Load server on port %v: %v", port, err)
			port += 2
			continue
		}
		if print_load_packets {
			stats.Handler = func(packet *load.LoadPacket) {
				packet.PrintReceived()
			}
		}
		break
	}
	tasks.AddNamed("client", server)
	go func() {
		for err := range server.Errors() {
			log.Println("Error receiving Load:", err)
		}
	}()
	log.Printf("Listening on %v UDP port %v for Load\n", rtp_ip, port)

	statistics = append(statistics, stats.Received)
	statistics = append(statistics, stats.Missed)
	return
}
Пример #6
0
func main() {
	loadBackend := flag.Bool("load", false, "Use Load servers to create the streams, instead of regular AMP Media servers")
	useHeartbeat := flag.Bool("heartbeat", false, "Use heartbeat-based fault detection instead of active ping-based detection")
	_heartbeat_frequency := flag.Uint("heartbeat_frequency", 200, "Time between two heartbeats which observers will send (milliseconds)")
	_heartbeat_timeout := flag.Uint("heartbeat_timeout", 350, "Time between two heartbeats before assuming offline server (milliseconds)")
	amp_addr := protocols.ParseServerFlags("0.0.0.0", 7779)
	heartbeat_frequency := time.Duration(*_heartbeat_frequency) * time.Millisecond
	heartbeat_timeout := time.Duration(*_heartbeat_timeout) * time.Millisecond

	var detector_factory balancer.FaultDetectorFactory
	tasks := golib.NewTaskGroup()
	var heartbeatServer *heartbeat.HeartbeatServer
	if *useHeartbeat {
		var err error
		heartbeatServer, err = heartbeat.NewHeartbeatServer(heartbeat_server)
		golib.Checkerr(err)
		go printServerErrors("Heartbeat", heartbeatServer.Server)
		log.Println("Listening for Heartbeats on", heartbeatServer.LocalAddr())
		detector_factory = func(endpoint string) (protocols.FaultDetector, error) {
			return heartbeatServer.ObserveServer(endpoint, heartbeat_frequency, heartbeat_timeout)
		}
		tasks.AddNamed("heartbeat", heartbeatServer)
	} else {
		detector_factory = func(endpoint string) (protocols.FaultDetector, error) {
			detector, err := ping.DialNewFaultDetector(endpoint)
			if err != nil {
				return nil, err
			}
			detector.Start()
			return detector, nil
		}
	}

	protocol, err := protocols.NewProtocol("AMP", amp.Protocol, ping.Protocol, heartbeat.Protocol)
	golib.Checkerr(err)
	baseServer, err := protocols.NewServer(amp_addr, protocol)
	golib.Checkerr(err)
	server, err := amp_balancer.RegisterPluginServer(baseServer)
	golib.Checkerr(err)
	tasks.AddNamed("server", server)

	ampPlugin := amp_balancer.NewAmpBalancingPlugin(detector_factory)
	server.AddPlugin(ampPlugin)
	pcpPlugin := amp_balancer.NewPcpBalancingPlugin(detector_factory)
	server.AddPlugin(pcpPlugin)

	if *loadBackend {
		for _, load := range load_servers {
			err := ampPlugin.AddBackendServer(load, stateChangePrinter)
			golib.Checkerr(err)
		}
	} else {
		for _, amp := range amp_servers {
			err := ampPlugin.AddBackendServer(amp, stateChangePrinter)
			golib.Checkerr(err)
		}
	}
	for _, pcp := range pcp_servers {
		err := pcpPlugin.AddBackendServer(pcp, stateChangePrinter)
		golib.Checkerr(err)
	}

	go printServerErrors("Server", server.Server)
	server.SessionStartedCallback = printSessionStarted
	server.SessionStoppedCallback = printSessionStopped

	log.Println("Listening to AMP on " + amp_addr)
	log.Println("Press Ctrl-C to close")

	if heartbeatServer != nil {
		tasks.Add(heartbeatServer)
	}
	tasks.Add(&golib.NoopTask{golib.ExternalInterrupt(), "external interrupt"})
	tasks.WaitAndExit()
}