Exemple #1
0
func NewGatewayService(serviceHost string, registryProxy *registry.RegistryProxy) error {
	var err error
	gateway := new(Gateway)

	gateway.registryProxy = registryProxy
	if err != nil {
		return err
	}

	gateway.id, err = gateway.registryProxy.Register(serviceHost, piazza.GatewayService, "my fun gateway")
	if err != nil {
		log.Fatalf("(Gateway.NewGatewayService) %v", err)
		return err
	}

	log.Printf("gateway id is %d", gateway.id)

	dispatcherServiceList, err := gateway.registryProxy.Lookup(piazza.DispatcherService)
	if err != nil {
		return err
	}

	dispatcherService, ok := dispatcherServiceList.GetOne()
	if !ok {
		return errors.New("dispatcher service not registered")
	}

	gateway.dispatcherProxy, err = dispatcher.NewDispatcherProxy(dispatcherService.Host)
	if err != nil {
		return err
	}

	r := mux.NewRouter()
	r.HandleFunc("/service", gateway.postMessage).Methods("POST")

	server := &http.Server{Addr: serviceHost, Handler: r}
	err = server.ListenAndServe()
	if err != nil {
		log.Fatal(err)
		return err
	}

	// not reached
	return nil
}
func setup(t *testing.T) *GatewayProxy {
	var err error

	var registryHost = piazza.GetRandomHost()
	var registryProxy *registry.RegistryProxy

	var dispatcherHost = piazza.GetRandomHost()
	var dispatcherProxy *dispatcher.DispatcherProxy

	var gatewayHost = piazza.GetRandomHost()
	var gatewayProxy *GatewayProxy

	{
		go registry.NewRegistryService(registryHost, "my registry")
		time.Sleep(250 * time.Millisecond)

		registryProxy, err = registry.NewRegistryProxy(registryHost)
		if err != nil {
			t.Error(err)
		}
	}
	{
		go dispatcher.NewDispatcherService(dispatcherHost, registryProxy)
		time.Sleep(250 * time.Millisecond)

		dispatcherProxy, err = dispatcher.NewDispatcherProxy(dispatcherHost)
		if err != nil {
			t.Error(err)
		}
	}
	{
		go NewGatewayService(gatewayHost, registryProxy)
		time.Sleep(250 * time.Millisecond)

		gatewayProxy, err = NewGatewayProxy(gatewayHost, dispatcherProxy)
		if err != nil {
			t.Error(err)
		}
	}

	return gatewayProxy
}