Exemple #1
0
func main() {
	scamp.Initialize("/etc/GTSOA/soa.conf")

	svc, err := scamp.NewService("0.0.0.0:63531", "service_verifier")
	if err != nil {
		scamp.Error.Printf("could not create service: `%s`", err.Error())
		os.Exit(1)
	}

	var reqCnt uint64 = 0
	go func() {
		for {
			reqLcl := atomic.LoadUint64(&reqCnt)
			scamp.Info.Printf("cnt: %d", reqLcl)
			time.Sleep(time.Duration(10) * time.Second)
		}
	}()

	// svc.Register("sqrt", func(msg *scamp.Message, client *scamp.Client) {
	//   atomic.AddUint64(&reqCnt,1)
	//   var sqrtReq SqrtRequest
	//   err = json.NewDecoder(bytes.NewReader(msg.Bytes())).Decode(&sqrtReq)
	//   if err != nil {
	//     scamp.Error.Printf("nooo: `%s`", err)
	//     return
	//   }

	//   // scamp.Info.Printf("sqrt req: %s", sqrtReq)

	//   var sqrtResp SqrtResponse

	//   res := math.Sqrt(sqrtReq.Input)
	//   if math.IsNaN(res) {
	//     sqrtResp.NaN = true
	//   } else {
	//     sqrtResp.Output = res
	//   }

	//   var buf bytes.Buffer
	//   err = json.NewEncoder(&buf).Encode(sqrtResp)
	//   if err != nil {
	//     scamp.Error.Printf("nooo: `%s`", err)
	//     return
	//   }

	//   resp := scamp.NewResponseMessage()
	//   resp.SetRequestId(msg.RequestId)
	//   resp.Write(buf.Bytes())
	//   _,err = client.Send(resp)
	//   if err != nil {
	//     panic(err.Error())
	//   }
	// })

	svc.Run()
}
Exemple #2
0
func (sip *SCAMPInputPlugin) Run(ir pipeline.InputRunner, h pipeline.PluginHelper) (err error) {
	sip.service, err = scamp.NewService(sip.conf.Service, sip.conf.Name)
	if err != nil {
		return
	}

	announcer, err := scamp.NewDiscoveryAnnouncer()
	if err != nil {
		scamp.Error.Printf("failed to create announcer: `%s`", err)
		return
	}
	announcer.Track(sip.service)
	go announcer.AnnounceLoop()

	var handlerConfig SCAMPInputHandlerConfig
	for _, handlerConfig = range sip.conf.Handlers {
		scamp.Trace.Printf("registering handler: `%s`", handlerConfig)

		sip.service.Register(handlerConfig.Action, func(msg *scamp.Message, client *scamp.Client) {
			var pack *pipeline.PipelinePack

			pack = <-ir.InChan()
			pack.Message.SetUuid(uuid.NewRandom())
			pack.Message.SetTimestamp(time.Now().UnixNano())
			pack.Message.SetPayload(string(msg.Bytes()[:]))
			pack.Message.SetSeverity(int32(handlerConfig.Severity))
			pack.Message.SetLogger(handlerConfig.Logger) // TODO not sure what this means
			ir.Deliver(pack)

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

			scamp.Trace.Printf("sending msg: {requestId: %d, type: `%s`, envelope: `%s`, body: `%s`}", reply.RequestId, reply.MessageType, reply.Envelope, reply.Bytes())

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

	sip.service.Run()
	return
}
Exemple #3
0
func main() {
	scamp.Initialize()

	service, err := scamp.NewService(":30100", "helloworld")
	if err != nil {
		scamp.Error.Fatalf("error creating new service: `%s`", err)
	}
	service.Register("helloworld.hello", func(message *scamp.Message, client *scamp.Client) {
		scamp.Info.Printf("go message: `%s`", message.Bytes())
		blob := message.Bytes()

		if len(blob) > 0 {
			scamp.Info.Printf("helloworld had data: %s", blob)
		} else {
			scamp.Trace.Printf("helloworld was called without data")
		}

		reply := &scamp.Message{MessageType: scamp.MESSAGE_TYPE_REPLY}
		reply.Write(famous_words)
		reply.SetRequestId(message.RequestId)
		_, err := client.Send(reply)
		if err != nil {
			scamp.Error.Printf("error while sending reply: `%s`. continuing.", err)
			return
		}
	})

	announcer, err := scamp.NewDiscoveryAnnouncer()
	if err != nil {
		scamp.Error.Printf("failed to create announcer: `%s`", err)
		return
	}
	announcer.Track(service)

	go announcer.AnnounceLoop()

	service.Run()
}