예제 #1
0
파일: mist.go 프로젝트: datachand/nanobox
// connect connects 'mist' to the server running on the guest machine
func connect() mistClient.Client {
	mistClient, err := mistClient.NewRemoteClient(config.MistURI)
	if err != nil {
		config.Fatal("[util/server/mist/mist] mist.NewRemoteClient() failed - ", err.Error())
	}

	return mistClient
}
예제 #2
0
파일: mist.go 프로젝트: sfermigier/nanobox
// Connect is the same as stream however it go routines the stream internally and
// returns the client
func Connect(tags []string, handle func(Log)) (client mistClient.Client, err error) {

	// add log level to tags
	tags = append(tags, config.LogLevel)

	// if this subscription already exists, exit; this prevents double subscriptions
	if _, ok := subscriptions[strings.Join(tags, "")]; ok {
		return
	}

	// connect to mist
	if client, err = mistClient.NewRemoteClient(config.MistURI); err != nil {
		return
	}
	// we dont defer a client.Close() here because we're returning the client and
	// want it to remain open

	// this is a bandaid to fix a race condition in mist when immediatly subscribing
	// after connecting a client; once this is fixed in mist this can be removed
	<-time.After(time.Second * 1)

	// subscribe
	if err = client.Subscribe(tags); err != nil {
		return
	}
	defer delete(subscriptions, strings.Join(tags, ""))

	// add tags to list of subscriptions
	subscriptions[strings.Join(tags, "")] = struct{}{}

	//
	go func() {
		for msg := range client.Messages() {

			//
			log := Log{}

			// unmarshal the incoming Message
			if err := json.Unmarshal([]byte(msg.Data), &log); err != nil {
				config.Fatal("[util/server/mist/mist] json.Unmarshal() failed", err.Error())
			}

			//
			handle(log)
		}
	}()

	return
}
예제 #3
0
파일: mist.go 프로젝트: rubysolo/nanobox
// Listen connects a to mist, subscribes tags, and listens for 'model' updates
func Listen(tags []string, handle func(string) error) error {

	// only subscribe if a subscription doesn't already exist
	if _, ok := subscriptions[strings.Join(tags, "")]; ok {
		return nil
	}

	// connect to mist
	client, err := mistClient.NewRemoteClient(config.MistURI)
	if err != nil {
		config.Fatal("[util/server/mist/mist] mist.NewRemoteClient() failed - ", err.Error())
	}
	defer client.Close()

	// this is a bandaid to fix a race condition in mist when immediatly subscribing
	// after connecting a client; once this is fixed in mist this can be removed
	<-time.After(time.Second * 1)

	// subscribe
	if err := client.Subscribe(tags); err != nil {
		config.Fatal("[util/server/mist/mist] client.Subscribe() failed - ", err.Error())
	}
	defer delete(subscriptions, strings.Join(tags, ""))

	// add tags to list of subscriptions
	subscriptions[strings.Join(tags, "")] = struct{}{}

	//
	model := Model{}
	for msg := range client.Messages() {

		// unmarshal the incoming Message
		if err := json.Unmarshal([]byte(msg.Data), &model); err != nil {
			config.Fatal("[util/server/mist/mist] json.Unmarshal() failed - ", err.Error())
		}

		// handle the status; when the handler returns false, it's time to break the
		// stream
		return handle(model.Document.Status)
	}

	return nil
}