示例#1
0
func main() {
	scamp.Initialize("/etc/SCAMP/soa.conf")

	service, err := scamp.NewService(":30100", "staticdev")
	if err != nil {
		scamp.Error.Fatalf("error creating new service: `%s`", err)
	}

	// rename Register -> RegisterAction
	service.Register("Logger.log", func(req *scamp.Message, client *scamp.Client) {
		scamp.Info.Printf("received msg len: %d", len(req.Bytes()))

		reply := scamp.NewMessage()
		reply.SetMessageType(scamp.MESSAGE_TYPE_REPLY)
		reply.SetEnvelope(scamp.ENVELOPE_JSON)
		reply.SetRequestId(req.RequestId)
		reply.Write([]byte("{}"))

		_, err = client.Send(reply)
		if err != nil {
			scamp.Error.Printf("could not reply to message: `%s`", err)
			return
		}
	})

	serviceDone := make(chan bool)
	go func() {
		service.Run()
		serviceDone <- true
	}()

	sigUsr1 := make(chan os.Signal)
	signal.Notify(sigUsr1, syscall.SIGUSR1)
	select {
	case <-sigUsr1:
		scamp.Info.Printf("shutdown requested")
		service.Stop()
	}

	select {
	case <-serviceDone:
		scamp.Info.Printf("service exiting gracefully")
	}

	scamp.Info.Printf("going to timeout so you can send a SIGQUIT and dump the goroutines...")
	select {
	case <-time.After(time.Duration(1) * time.Minute):
		scamp.Info.Printf("1 minute timeout achieved")
	}
}
示例#2
0
func oldmain() {
	bigMsg := make([]byte, 256)
	scamp.Initialize("/etc/SCAMP/soa.conf")

	client, err := scamp.Dial("127.0.0.1:30100")
	defer client.Close()
	if err != nil {
		scamp.Error.Fatalf("could not connect! `%s`\n", err)
		return
	}

	i := 0
	for i = 0; i < len(bigMsg); i++ {
		bigMsg[i] = 'f'
	}

	message := scamp.NewMessage()
	message.SetRequestId(1234)
	message.SetAction("Logger.log")
	message.SetEnvelope(scamp.ENVELOPE_JSON)
	message.SetVersion(1)
	message.SetMessageType(scamp.MESSAGE_TYPE_REQUEST)
	// message.Write([]byte(`hey logger`))
	message.Write(bigMsg)

	recvChan, err := client.Send(message)
	if err != nil {
		scamp.Error.Fatalf("could not send message: `%s`\n", err)
		return
	}

	select {
	case response, ok := <-recvChan:
		if !ok {
			scamp.Error.Printf("recvChan was closed. exiting.")
		} else {
			scamp.Info.Printf("got reply: %s", response.Bytes())
		}
	case <-time.After(timeout):
		scamp.Error.Fatalf("failed to get reply before timeout")
	}

	select {
	case <-time.After(timeout):
		scamp.Info.Printf("done waiting")
	}

	client.Close()
}
示例#3
0
func main() {
	scamp.Initialize("/etc/SCAMP/soa.conf")
	client, err := scamp.Dial("127.0.0.1:30100")
	defer client.Close()
	if err != nil {
		scamp.Error.Fatalf("could not connect! `%s`\n", err)
		return
	}

	for i := 0; i < 10; i++ {
		message := scamp.NewMessage()
		message.SetRequestId(i + 1)
		message.SetAction("Logger.log")
		message.SetEnvelope(scamp.ENVELOPE_JSON)
		message.SetVersion(1)
		message.SetMessageType(scamp.MESSAGE_TYPE_REQUEST)
		// message.Write([]byte(`hey logger`))
		msgBytes := []byte(fmt.Sprintf("TEST MESSAGE %d\n", i))
		message.Write(msgBytes)

		recvChan, err := client.Send(message)
		if err != nil {
			scamp.Error.Fatalf("could not send message: `%s`\n", err)
			return
		}

		select {
		case response, ok := <-recvChan:
			if !ok {
				scamp.Error.Printf("recvChan was closed. exiting.")
			} else {
				scamp.Info.Printf("got reply: %s", response.Bytes())
			}
		case <-time.After(timeout):
			scamp.Error.Fatalf("failed to get reply before timeout")
		}

		time.Sleep(time.Duration(500) * time.Millisecond)

	}

	select {
	case <-time.After(timeout):
		scamp.Info.Printf("done waiting")
	}
}