Exemplo n.º 1
0
func main() {
	engine := echoEngine()
	engine.Start()

	pipe := engine.NewPipe(
		engine.NewRequest("example.fetch"),
		engine.NewRequest("example.words"),
		engine.NewRequest("example.stopfilter"),
		engine.NewRequest("example.count"),
		engine.NewParallel(
			engine.NewRequest("example.popular3"),
			engine.NewRequest("example.popular4"),
			engine.NewRequest("example.popular5"),
		),
		engine.NewLambda(converge),
	)

	resp, err := pipe.Execute(G.NewMessage().SetData(url))
	if err != nil {
		log.Println(err)
		return
	}

	expected := [][]string{}
	if err := resp.Next().GetData(&expected); err != nil {
		log.Println(err)
	} else {
		for _, words := range expected {
			log.Printf("Popular words: %v\n", words)
		}
	}
}
Exemplo n.º 2
0
func generator(first, second float64, tick <-chan time.Time, e *G.Gilmour) {
	//Wait for a tick
	<-tick

	packet := map[string]float64{"first": first, "second": second}
	data := G.NewMessage().SetData(packet)

	req := e.NewRequest(fibTopic)
	resp, err := req.Execute(data)
	if err != nil {
		fmt.Println("Error in Handler", err)
		return
	}

	if resp.Code() != 200 {
		fmt.Println("Error in Handler", resp.Code())
		return
	}

	var next float64
	msg := resp.Next()
	if err := msg.GetData(&next); err != nil {
		fmt.Println(err)
		return
	}

	generator(second, next, tick, e)
}
Exemplo n.º 3
0
func stopWordsReply(g *G.Gilmour, stopList *[]string) func(req *G.Request, resp *G.Message) {
	return func(req *G.Request, resp *G.Message) {
		line := fmt.Sprintf("Filtering out stop words")
		g.Signal("example.log", G.NewMessage().SetData(line))

		words := []string{}
		if err := req.Data(&words); err != nil {
			panic(words)
		}

		stopMap := map[string]bool{}
		for _, w := range *stopList {
			stopMap[w] = true
		}

		filtered := []string{}
		for _, w := range words {
			if _, ok := stopMap[w]; !ok {
				filtered = append(filtered, w)
			}
		}

		resp.SetData(filtered)

	}
}
Exemplo n.º 4
0
func saveReply(g *G.Gilmour) func(req *G.Request, resp *G.Message) {
	return func(req *G.Request, resp *G.Message) {
		line := fmt.Sprintf("Save")
		g.Signal("example.log", G.NewMessage().SetData(line))

		resp.SetData("Hello from Save")
	}
}
Exemplo n.º 5
0
func monthLambda(month string) func(m *G.Message) (*G.Message, error) {
	return func(m *G.Message) (*G.Message, error) {
		var data map[string]interface{}
		if err := m.GetData(&data); err != nil {
			panic(err)
		}

		return G.NewMessage().SetData(data[month]), nil
	}
}
Exemplo n.º 6
0
func findReply(g *G.Gilmour) func(req *G.Request, resp *G.Message) {
	return func(req *G.Request, resp *G.Message) {
		line := fmt.Sprintf("Find")
		g.Signal("example.log", G.NewMessage().SetData(line))

		input := getInput(req)

		resp.SetData(input)
	}
}
Exemplo n.º 7
0
func wordsReply(g *G.Gilmour) func(req *G.Request, resp *G.Message) {
	return func(req *G.Request, resp *G.Message) {
		line := fmt.Sprintf("WordCount")
		g.Signal("example.log", G.NewMessage().SetData(line))

		input := getInput(req)
		wordRe := regexp.MustCompile("\\w+")
		words := wordRe.FindAllString(input, -1)
		resp.SetData(words)
	}
}
Exemplo n.º 8
0
func converge(msg *G.Message) (*G.Message, error) {
	out := []*popular{}
	msg.GetData(&out)

	pWords := make([][]string, 3)

	for _, o := range out {
		pWords[o.WordLength-3] = o.Words
	}

	return G.NewMessage().SetData(pWords), nil
}
Exemplo n.º 9
0
func echoRequest(wg *sync.WaitGroup, engine *G.Gilmour, msg string) {
	req := engine.NewRequest("echo")
	resp, err := req.Execute(G.NewMessage().SetData(msg))
	if err != nil {
		fmt.Println("Echoclient: error", err.Error())
	}

	defer wg.Done()

	var output string
	if err := resp.Next().GetData(&output); err != nil {
		fmt.Println("Echoclient: error", err.Error())
	} else {
		fmt.Println("Echoclient: received", output)
	}
}
Exemplo n.º 10
0
func countReply(g *G.Gilmour) func(req *G.Request, resp *G.Message) {
	return func(req *G.Request, resp *G.Message) {
		line := fmt.Sprintf("WordCount")
		g.Signal("example.log", G.NewMessage().SetData(line))

		input := []string{}
		if err := req.Data(&input); err != nil {
			panic(err)
		}

		word_counts := make(map[string]int)
		for _, word := range input {
			word_counts[word]++
		}

		resp.SetData(word_counts)
	}
}
Exemplo n.º 11
0
func main() {
	e := echoEngine()
	e.Start()

	batch := e.NewPipe(
		e.NewRequest("weather.fetch"),
		e.NewRequest("weather.group"),
		e.NewParallel(
			e.NewPipe(
				e.NewLambda(monthLambda("jan")),
				e.NewParallel(
					e.NewRequest("weather.min"),
					e.NewRequest("weather.max"),
				),
			),
			e.NewPipe(
				e.NewLambda(monthLambda("feb")),
				e.NewParallel(
					e.NewRequest("weather.min"),
					e.NewRequest("weather.max"),
				),
			),
		),
	)

	resp, _ := batch.Execute(G.NewMessage().SetData("pune"))

	var out []*G.Message
	for msg := resp.Next(); msg != nil; msg = resp.Next() {
		out = append(out, msg)
	}

	for _, msg := range out {
		monthOut := []*weatherTuple{}
		msg.GetData(&monthOut)

		for _, m := range monthOut {
			log.Println(m)
		}
	}
}
Exemplo n.º 12
0
func popularReply(g *G.Gilmour, wordLen int) func(req *G.Request, resp *G.Message) {
	return func(req *G.Request, resp *G.Message) {
		line := fmt.Sprintf("Popular %v", wordLen)
		g.Signal("example.log", G.NewMessage().SetData(line))

		input := map[string]int{}
		if err := req.Data(&input); err != nil {
			panic(err)
		}

		popular := map[int][]string{}
		score := []int{}

		for k, v := range input {
			if len(k) != wordLen {
				continue
			}
			popular[v] = append(popular[v], k)
		}

		for k := range popular {
			score = append(score, k)
		}

		sort.Sort(sort.Reverse(sort.IntSlice(score)))

		output := struct {
			WordLength int
			Score      int
			Words      []string
		}{WordLength: wordLen}

		if len(score) > 0 {
			output.Score = score[0]
			output.Words = popular[score[0]]
		}
		resp.SetData(output)
	}
}
Exemplo n.º 13
0
//Fetch a remote file from the URL received in Request.
func fetchReply(g *G.Gilmour) func(req *G.Request, resp *G.Message) {
	return func(req *G.Request, resp *G.Message) {
		url := getInput(req)

		line := fmt.Sprintf("Fetch %v", url)
		g.Signal("example.log", G.NewMessage().SetData(line))

		response, err := http.Get(url)
		if err != nil {
			panic(err)
		}

		defer response.Body.Close()
		contents, err := ioutil.ReadAll(response.Body)

		if err != nil {
			panic(err)
		}

		//Send back the contents.
		resp.SetData(string(contents))
	}
}
Exemplo n.º 14
0
func glog(g *G.Gilmour, msg string) {
	g.Signal("example.log", G.NewMessage().SetData(msg))
}