Пример #1
0
func hitService(client *scamp.Client, wg sync.WaitGroup) {
	defer client.Close()
	defer func() {
		scamp.Info.Printf("wg.Done()!")
		wg.Done()
	}()
	defer scamp.Info.Printf("closing %s", client)

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

	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())
		}
		return
	case <-time.After(timeout):
		scamp.Error.Fatalf("failed to get reply before timeout")
		return
	}
}
Пример #2
0
func (sop *SCAMPOutputPlugin) Run(or pipeline.OutputRunner, h pipeline.PluginHelper) (err error) {
	var pack *pipeline.PipelinePack

	// We have no default encoder
	if or.Encoder() == nil {
		return errors.New("Encoder required.")
	}

	for pack = range or.InChan() {
		scamp.Info.Printf("received pipeline pack")
		encoded, err := or.Encode(pack) // pack.Message.GetPayload()

		if err == nil {
			scamp.Info.Printf("payload: %s", encoded)
			msg := scamp.NewMessage()
			msg.SetEnvelope(scamp.ENVELOPE_JSON)
			msg.SetAction(sop.conf.Action)
			msg.SetVersion(1)
			msg.Write(encoded)
		}

		pack.Recycle(err)
	}
	fmt.Println("sup from end of for loop in Run")
	return
}
Пример #3
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
}