Example #1
0
// NewMessage creates a new instance of Message
func NewMessage() *Message {
	return &Message{
		payload: lane.NewStack(),
		Reports: lane.NewStack(),
		Opts:    cmap.New(),
	}
}
Example #2
0
/*
Get the raw bencoded info dictionary value. This is a bit
of a hack, as it requires two passes through the tokens to
get the original data. A better approach may have been to
have the Lexer record the corresponding position and values
of every token have the parser be able to build up the
original representation of a parsed structure.
*/
func GetBencodedInfo(tokens []Token) []byte {
	startInfo, endInfo := 0, 0
	infoBytes := make([]byte, 0)

	dictStack := lane.NewStack()
	for i, token := range tokens {
		if token.Type == TOKEN_DICT_START {
			dictStack.Push(i)
		} else if token.Type == TOKEN_DICT_END {
			dictStack.Pop()
			if dictStack.Size() == 1 && endInfo == 0 {
				endInfo = i - 1
				infoBytes = append(infoBytes, token.Value...)
			}
		}
		if startInfo == 0 && string(tokens[i+3].Value) == "info" && dictStack.Size() == 1 {
			startInfo = i + 3
		}
		if startInfo > 0 && i > startInfo && endInfo == 0 {
			infoBytes = append(infoBytes, token.Value...)
		}
	}

	//	fmt.Println(fmt.Sprintf("%x", infoBytes[len(infoBytes)-2:]))
	return infoBytes
}
Example #3
0
func New(name string, r io.Reader) *Parser {
	var doc graphql.Document
	query, _ := ioutil.ReadAll(r)
	p := &Parser{
		name:     name,
		query:    string(query),
		Document: &doc,
		nodes:    lane.NewStack(),
	}
	return p
}
Example #4
0
func BeginLexing(name, input string, state LexFn) *Lexer {
	l := &Lexer{
		Name:        name,
		Input:       []byte(input),
		State:       state,
		Tokens:      make(chan Token, 3),
		NestedStack: lane.NewStack(),
	}

	return l
}
Example #5
0
/*
beginParsing initializes the Parser.
*/
func beginParsing(tokens []Token, state ParseFn) *Parser {
	p := &Parser{
		Tokens: tokens,
		State:  state,
		//		Output: make([]interface{}, 0),
		Stack: lane.NewStack(),
		Pos:   0,
	}

	//	p.CurrentContainer = p.Output
	return p
}
Example #6
0
// Size size of this DFA
func (d *DFA) Size() int {
	stack := lane.NewStack()
	visited := map[*state]bool{}
	stack.Push(d.start)
	visited[d.start] = true
	cnt := 0
	for !stack.Empty() {
		curr := stack.Pop().(*state)
		cnt++
		for _, v := range curr.edges {
			if _, ok := visited[v]; !ok {
				visited[v] = true
				stack.Push(v)
			}
		}
	}
	return cnt
}
Example #7
0
// Debug output debug information about this DFA
func (d *DFA) Debug() {
	stack := lane.NewStack()
	visited := map[*state]bool{}
	stack.Push(d.start)
	visited[d.start] = true
	cnt := 0
	log.Println("DFA Debug:")
	for !stack.Empty() {
		curr := stack.Pop().(*state)
		log.Printf("\n%p:\n%v\n\n", curr, curr)
		cnt++
		for _, v := range curr.edges {
			if _, ok := visited[v]; !ok {
				visited[v] = true
				stack.Push(v)
			}
		}
	}
	log.Println("DFA Debug: size ", cnt)
}
Example #8
0
func (p *pipeline) Run() {
	for index, component := range p.components {
		for w := 0; w < component.composser.Workers(); w++ {
			go func(w, index int, component Component) {
				component.composser = component.composser.Spawn(w)
				messages := make(chan *utils.Message)
				component.pool <- messages

				for m := range messages {
					component.composser.OnMessage(m,
						// Done function
						func(m *utils.Message, code int, status string) {
							// If there is another component next in the pipeline send the
							// messate to it. I other case send the message to the report
							// handler
							if code == 0 && len(p.components)-1 > index {
								nextWorker := <-p.components[index+1].pool
								nextWorker <- m
							} else {
								reports := lane.NewStack()

								for !m.Reports.Empty() {
									rep := m.Reports.Pop().(Report)
									rep.Component = index
									rep.Code = code
									rep.Status = status
									reports.Push(rep)
								}

								m.Reports = reports
								p.output <- m
							}
						})

					component.pool <- messages
				}
			}(w, index, component)
		}
	}

	go func() {
		for {
			select {
			case m, ok := <-p.retry:
				if ok {
					rep := m.Reports.Head().(Report)
					worker := <-p.components[rep.Component].pool
					worker <- m
				}
				continue

			default:
			}

			select {
			case m, ok := <-p.retry:
				if ok {
					rep := m.Reports.Head().(Report)
					worker := <-p.components[rep.Component].pool
					worker <- m
				}
				continue

			case m, ok := <-p.input:
				// If input channel has been closed, close output channel
				if !ok {
					for _, component := range p.components {
						for i := 0; i < component.composser.Workers(); i++ {
							worker := <-component.pool
							close(worker)
						}
					}
					close(p.output)
				} else {
					worker := <-p.components[0].pool
					worker <- m
				}
			}
		}
	}()
}