func aboutCommonInterfaces() {
	{
		in := new(bytes.Buffer)
		in.WriteString("hello world")

		out := new(bytes.Buffer)
		out.ReadFrom(in)

		/*
		   Your code goes here.
		   Hint, use these resources:

		   $ godoc -http=:8080
		   $ open http://localhost:8080/pkg/io/
		   $ open http://localhost:8080/pkg/bytes/
		*/

		assert(out.String() == "hello world") // get data from the io.Reader to the io.Writer
	}

	{
		in := new(bytes.Buffer)
		in.WriteString("hello world")

		out := new(bytes.Buffer)
		x, _ := in.ReadBytes('o')
		out.Write(x)

		assert(out.String() == "hello") // duplicate only a portion of the io.Reader
	}
}
Exemple #2
0
func parseMeta(buf *bytes.Buffer, repo string) {
	var name string
	pack := map[string]string{}
	pack["REPO"] = repo
	_, _ = buf.ReadByte()
	for {
		key, err := buf.ReadBytes('%')
		if err == io.EOF {
			break
		}
		key = bytes.Trim(key, "%")
		values, _ := buf.ReadBytes('%')
		values = bytes.Trim(values, "%")
		values = bytes.Replace(values, []byte("\n"), []byte(" "), -1)
		values = bytes.Trim(values, " ")
		if string(key) == "NAME" {
			name = string(values)
		}
		pack[string(key)] = string(values)
	}
	v, _ := packages[name]
	if v != nil {
		printf("%s exists\n", name)
	}
	packages[name] = pack
}
Exemple #3
0
func ReadCString(buf *bytes.Buffer) string {
	b, err := buf.ReadBytes(0)
	if err != nil {
		panic(NewBsonError("%s", err))
	}
	return string(b[:len(b)-1])
}
Exemple #4
0
func readBytes(buf *bytes.Buffer) ([]byte, error) {
	out, err := buf.ReadBytes(0x0)
	if err != nil {
		return out, err
	}
	// Chop NUL off the end
	return out[:len(out)-1], nil
}
Exemple #5
0
func demandLine(buf *bytes.Buffer) ([]byte, error) {
	line, err := buf.ReadBytes('\n')
	if err == io.EOF {
		return nil, fmt.Errorf("Unexpected EOF when reading HMM in hhm.")
	}
	if err != nil {
		return nil, err
	}
	return trim(line), nil
}
Exemple #6
0
/*
	Dumps the bytes buffer a line at a time to our real stdout device.
*/
func dump_stderr(stderr bytes.Buffer, prefix string) {
	for { // read until the first error which we assume is io.EOF
		line, err := stderr.ReadBytes('\n')
		if err == nil {
			sheep.Baa(0, "%s stderr:  %s", prefix, bytes.TrimRight(line, "\n"))
		} else {
			return
		}
	}
}
func writeBufferMessage(ws *websocket.Conn, out *bytes.Buffer) {
	for {
		line, _ := out.ReadBytes('\n') //按行读取
		if line != nil {
			writeBytesMessage(ws, line)
		}
		if out.Len() == 0 {
			break
		}
	}
}
Exemple #8
0
func PacketToMessage(p []byte) *Message {
	var in bytes.Buffer
	in.Write(p)

	b, e := in.ReadBytes(delim)
	if e != nil {
		println(e)
	}
	var m Message
	json.Unmarshal(b[:len(b)-1], &m)

	return &m
}
Exemple #9
0
/*
	Accept an array and a bytes buffer; save the newline separated records in buf into the array starting at
	the index (sidx). Returns the index
*/
func buf_into_array(buf bytes.Buffer, a []string, sidx int) (idx int) {
	idx = sidx
	for { // read until the first error which we assume is io.EOF
		line, err := buf.ReadBytes('\n')
		if err == nil {
			if idx < len(a) {
				a[idx] = string(line[0 : len(line)-1]) // remove trailing newline
				idx++
			}
		} else {
			return
		}
	}
}
Exemple #10
0
func DecadeLang(b []byte) (lang Lang) {
	var buff bytes.Buffer
	buff.Write(b)
	temp, _ := buff.ReadBytes(ET)
	buff.Reset()
	buff.Write(temp)
	lang = make(Lang)
	for buff.Len() > 1 {
		key, _ := buff.ReadByte()
		text, _ := buff.ReadString(ETB)
		lang[uint8(key)] = wipeEOT(text)
	}
	return
}
func GetOperatingSystem() (string, error) {
	sw_vers, err := exec.LookPath("sw_vers")
	if err != nil {
		return "", fmt.Errorf("Can not find sw_vers")
	}
	cmd := exec.Command(sw_vers)
	stdbuf := new(bytes.Buffer)
	cmd.Stdout = stdbuf
	if err := cmd.Run(); err != nil {
		return "", err
	}
	b, _ := stdbuf.ReadBytes(byte('\n'))
	if i := bytes.Index(b, []byte("ProductName:")); i >= 0 {
		b = b[i+16:]
		return strings.Trim(string(b), " "), nil
	}
	return "", fmt.Errorf("ProductName not found")
}
Exemple #12
0
func runCmds(cmds []*exec.Cmd) ([]string, error) {
	if cmds == nil || len(cmds) == 0 {
		return nil, errors.New("The cmd slice is invalid!")
	}
	first := true
	var output []byte
	var err error
	for _, cmd := range cmds {
		fmt.Printf("Run command: %v ...\n", getCmdPlaintext(cmd))
		if !first {
			var stdinBuf bytes.Buffer
			stdinBuf.Write(output)
			cmd.Stdin = &stdinBuf
		}
		var stdoutBuf bytes.Buffer
		cmd.Stdout = &stdoutBuf
		if err = cmd.Start(); err != nil {
			return nil, getError(err, cmd)
		}
		if err = cmd.Wait(); err != nil {
			return nil, getError(err, cmd)
		}
		output = stdoutBuf.Bytes()
		//fmt.Printf("Output:\n%s\n", string(output))
		if first {
			first = false
		}
	}
	lines := make([]string, 0)
	var outputBuf bytes.Buffer
	outputBuf.Write(output)
	for {
		line, err := outputBuf.ReadBytes('\n')
		if err != nil {
			if err == io.EOF {
				break
			} else {
				return nil, getError(err, nil)
			}
		}
		lines = append(lines, string(line))
	}
	return lines, nil
}
Exemple #13
0
func readMail(arg string) (bs []byte, needSync bool) {
	f, err := os.Open(arg)
	defer f.Close()
	errExit(err)
	bufr := bufio.NewReader(f)
	bffbyts := bytes.Buffer{}
	bffbyts.ReadFrom(bufr)
	bs = make([]byte, 0)
	var prefix []byte
	for {
		l, err := bffbyts.ReadBytes(byte('\n'))
		if err == io.EOF {
			return
		}
		bs = append(bs, l...)
		if len(l) == 1 && l[0] == '\n' {
			break
		}
	}
	for {
		l, err := bffbyts.ReadBytes(byte('\n'))
		if err == io.EOF {
			break
		}
		if len(l) > 3 {
			prefix = l[:3]
			if bytes.Equal(prefix, pattern) {
				needSync = true
				break

			} else {
				bs = append(bs, l...)
			}

		} else {
			bs = append(bs, l...)
		}

	}

	return

}
Exemple #14
0
func (db *Csv) updateActivity(activity *Activity) (err error) {
	var pos int64
	var line []byte
	pos, line, err = db.findActivityLine(activity.Id)
	if err != nil {
		return
	}

	buf := new(bytes.Buffer)
	w := csv.NewWriter(buf)

	record := db.activityToRecord(activity)
	err = w.Write(record)
	if err != nil {
		return
	}
	w.Flush()

	var newLine []byte
	newLine, err = buf.ReadBytes('\n')
	if err != nil {
		return
	}

	/* If the resulting record is the same length, just overwrite it */
	if len(line) == len(newLine) {
		err = db.writeBytes(pos, newLine)
	} else {
		/* Save the data past the line, write the line, then put the data back */
		var data []byte
		data, err = db.readAll(pos + int64(len(line)))
		if err != nil {
			return
		}
		err = db.writeBytes(pos, newLine)
		if err != nil {
			return
		}
		err = db.writeBytes(pos+int64(len(newLine)), data)
	}

	return
}
Exemple #15
0
func readMeta(buf *bytes.Buffer) (*HHR, error) {
	hhr := &HHR{}
	for {
		line, err := buf.ReadBytes('\n')
		if err == io.EOF && len(line) == 0 {
			break
		}
		if err != nil && err != io.EOF {
			return nil, err
		}
		line = trim(line)

		switch {
		case hasPrefix(line, "Query"):
			hhr.Query = str(line[5:])
		case hasPrefix(line, "Match_columns"):
			hhr.MatchColumns, err = strconv.Atoi(str(line[13:]))
			if err != nil {
				return nil, err
			}
		case hasPrefix(line, "No_of_seqs"):
			hhr.NumSeqs = str(line[10:])
		case hasPrefix(line, "Neff"):
			f, err := strconv.ParseFloat(str(line[4:]), 64)
			if err != nil {
				return nil, err
			}
			hhr.Neff = seq.Prob(f)
		case hasPrefix(line, "Searched_HMMs"):
			hhr.SearchedHMMs, err = strconv.Atoi(str(line[13:]))
			if err != nil {
				return nil, err
			}
		case hasPrefix(line, "Date"):
			hhr.Date = str(line[4:])
		case hasPrefix(line, "Command"):
			hhr.Command = str(line[7:])
		}
	}
	return hhr, nil
}
func writeBytesBufferMessage(out *bytes.Buffer, cmdRunner *utils.DeployCmdRunner, ws *websocket.Conn) {
	for {
	loop:
		if out.Len() == 0 {
			if cmdRunner.Exited() {
				break
			} else {
				time.Sleep(time.Second)
				goto loop
			}
		}
	again:
		line, _ := out.ReadBytes('\n') //按行读取
		if line != nil {
			writeBytesMessage(ws, line)
			goto again
		} else {
			goto loop
		}
	}
}
func ReadAndWriteBytes(out *bytes.Buffer, cmdRunner *utils.DeployCmdRunner) {
	for {
	loop:
		if out.Len() == 0 {
			if cmdRunner.Exited() {
				break
			} else {
				time.Sleep(time.Second)
				goto loop
			}
		}

	again:
		line, _ := out.ReadBytes('\n')
		if line != nil {
			fmt.Println(string(line))
			goto again
		} else {
			goto loop
		}
	}
}
Exemple #18
0
func prettyPrint(o Object) string {
	switch o := o.(type) {
	case Blob:
		if len(o.Data) < 40 {
			return strconv.Quote(string(o.Data))
		}
		return strconv.Quote(string(o.Data[:40])) + "..."
	case Tree:
		buf := new(bytes.Buffer)
		for _, e := range o.Entries {
			fmt.Fprintf(buf, "%06o %s %s\n",
				gitMode(e.Mode), e.Hash, e.Name)
		}
		return buf.String()
	case Commit:
		buf := new(bytes.Buffer)
		o.WriteTo(buf)
		buf.ReadBytes(0)
		return buf.String()
	}
	panic("impossible")
}
Exemple #19
0
// removeCarriageReturns removes all carriage returns from the input if the
// OS is Windows. It does not return any errors.
func removeCarriageReturns(b bytes.Buffer) bytes.Buffer {
	if runtime.GOOS == "windows" {
		var buf bytes.Buffer
		for {
			byt, er := b.ReadBytes(0x0D)
			end := len(byt)
			if nil == er {
				end -= 1
			}
			if nil != byt {
				buf.Write(byt[:end])
			} else {
				break
			}
			if nil != er {
				break
			}
		}
		b = buf
	}
	return b

}
func aboutCommonInterfaces() {
	{
		in := new(bytes.Buffer)
		in.WriteString("hello world")

		out := new(bytes.Buffer)

		out.ReadFrom(in)

		assert(out.String() == "hello world") // get data from the io.Reader to the io.Writer
	}

	{
		in := new(bytes.Buffer)
		in.WriteString("hello world")

		out := new(bytes.Buffer)

		bs, _ := in.ReadBytes('o')
		out.Write(bs)

		assert(out.String() == "hello") // duplicate only a portion of the io.Reader
	}
}
Exemple #21
0
// marshalLength builds a byte representation of length
//
// http://luca.ntop.org/Teaching/Appunti/asn1.html
//
// Length octets. There are two forms: short (for lengths between 0 and 127),
// and long definite (for lengths between 0 and 2^1008 -1).
//
// * Short form. One octet. Bit 8 has value "0" and bits 7-1 give the length.
// * Long form. Two to 127 octets. Bit 8 of first octet has value "1" and bits
//   7-1 give the number of additional length octets. Second and following
//   octets give the length, base 256, most significant digit first.
func marshalLength(length int) ([]byte, error) {

	// more convenient to pass length as int than uint64. Therefore check < 0
	if length < 0 {
		return nil, fmt.Errorf("length must be greater than zero")
	} else if length < 127 {
		return []byte{byte(length)}, nil
	}

	buf := new(bytes.Buffer)
	err := binary.Write(buf, binary.LittleEndian, uint64(length))
	if err != nil {
		return nil, err
	}

	buf_bytes, err2 := buf.ReadBytes(0) // can't use buf.Bytes() - trailing 00's
	if err2 != nil {
		return nil, err
	}
	buf_bytes = buf_bytes[0 : len(buf_bytes)-1] // remove trailing 00

	header := []byte{byte(128 | len(buf_bytes))}
	return append(header, buf_bytes...), nil
}
Exemple #22
0
func parseCommand(buf *bytes.Buffer) (*command, error) {
	// Read ( *<number of arguments> CR LF )
	if c, err := buf.ReadByte(); c != '*' { // io.EOF
		return nil, err
	}
	// number of arguments
	line, err := buf.ReadBytes(LF)
	if err != nil {
		return nil, err
	}
	argCount, _ := strconv.Atoi(string(line[:len(line)-2]))
	args := make([][]byte, argCount)
	for i := 0; i < argCount; i++ {
		// Read ( $<number of bytes of argument 1> CR LF )
		if c, err := buf.ReadByte(); c != '$' {
			return nil, err
		}

		line, err := buf.ReadBytes(LF)
		if err != nil {
			return nil, err
		}
		argSize, _ := strconv.Atoi(string(line[:len(line)-2]))
		// Read ( <argument data> CR LF )
		args[i] = make([]byte, argSize)
		n, e2 := buf.Read(args[i])
		if n != argSize {
			return nil, errors.New("argSize too short")
		}
		if e2 != nil {
			return nil, e2
		}

		_, err = buf.ReadBytes(LF)
		if err != nil {
			return nil, err
		}
	}
	cmd := newCommand(args...)
	return cmd, nil
}
Exemple #23
0
func parsePage(r *bytes.Buffer, m *Match) error {
	ds, err := r.ReadString(byte(' '))
	if err != nil {
		return nil
	}
	ds = strings.ToLower(ds[0 : len(ds)-1])
	var d Division
	switch ds {
	case "classic":
		d = Classic
	case "standard":
		d = Standard
	case "production":
		d = Production
	case "open":
		d = Open
	case "revolver":
		d = Revolver
	default:
		_, err := r.ReadBytes(ff) // Skip page
		return err
	}

	// TODO(flowlo): Maybe match for those lines,
	// to make sure we're dealing with a nice file.
	r.ReadBytes(lf) // Skip rest of first line, usually "-- Overall Match Results"
	r.ReadBytes(lf) // Skip line, usually name of the Match
	r.ReadBytes(lf) // Skip line, usually print timestamp of the PDF
	r.ReadBytes(lf) // Skip empty line
	r.ReadBytes(lf) // Skip line, usually a descriptive headline
	r.ReadBytes(lf) // Skip empty line

	buf := new(bytes.Buffer)

	c := 1
	for {
		b, err := r.ReadByte()
		if err != nil {
			return err
		}
		r.UnreadByte()
		if b == byte('\n') {
			break
		}

		l, err := r.ReadBytes(byte('\n'))
		if err != nil {
			return err
		}

		buf.Reset()
		buf.Write(l)
		sc := bufio.NewScanner(buf)
		sc.Split(bufio.ScanWords)

		res := Result{}

		// Read rank.
		if !sc.Scan() {
			return ErrNoToken
		}

		i, err := strconv.Atoi(sc.Text())
		if err != nil {
			return err
		}
		if i != len(m.Results[d])+1 {
			return fmt.Errorf("expected rank %d but got %d", len(m.Results[d]), i)
		}

		// Read percentage.
		if !sc.Scan() {
			return ErrNoToken
		}

		dotted := strings.Replace(sc.Text(), ",", ".", 1)
		f, err := strconv.ParseFloat(dotted, 64)
		if err != nil {
			return err
		}
		res.Percent = f

		// Read points.
		if !sc.Scan() {
			return ErrNoToken
		}

		dotted = strings.Replace(sc.Text(), ",", ".", 1)
		f, err = strconv.ParseFloat(dotted, 64)
		if err != nil {
			return err
		}
		res.Points = f

		// Read MOS number.
		if !sc.Scan() {
			return ErrNoToken
		}

		i, err = strconv.Atoi(sc.Text())
		if err != nil {
			return err
		}
		res.MosNumber = i

		// Read name.
		sc.Split(scanThreeSpaces)
		if !sc.Scan() {
			return ErrNoToken
		}
		res.Name = sc.Text()

		sc.Split(bufio.ScanWords)
		if !sc.Scan() {
			return ErrNoToken
		}

		res.Flags = sc.Text()
		res.CountryCode = ""
		if len(res.Flags) > 2 {
			i := sort.SearchStrings(countryCodes[:], res.Flags)
			if i < len(countryCodes) && countryCodes[i] == res.Flags {
				res.CountryCode = res.Flags
				res.Flags = ""
				goto finish
			}
		}

		if !sc.Scan() {
			if res.CountryCode == "" {
				return ErrNoToken
			}
		} else {
			res.CountryCode = sc.Text()
		}

	finish:
		c++
		fmt.Printf("%+v\n", res)
		m.Results[d] = append(m.Results[d], res)
	}

	for {
		s, err := r.ReadString(lf) // Skip empty line
		if err != nil {
			return err
		}
		if len(s) == 0 {
			continue
		}
		if strings.Contains(s, "used") {
			_, err := r.ReadBytes(ff) // Skip footer
			return err
		}
	}

	return nil
}
Exemple #24
0
// Decode decodes binary data in the given buffer to build a Map.
//
// If the ShortMessage field is present, and DataCoding as well,
// we attempt to decode text automatically. See pdutext package
// for more information.
func (l List) Decode(r *bytes.Buffer) (Map, error) {
	var unsuccessCount int
	var numDest int
	f := make(Map)
loop:
	for _, k := range l {
		switch k {
		case
			AddressRange,
			DestinationAddr,
			ErrorCode,
			FinalDate,
			MessageID,
			MessageState,
			Password,
			ScheduleDeliveryTime,
			ServiceType,
			SourceAddr,
			SystemID,
			SystemType,
			ValidityPeriod:
			b, err := r.ReadBytes(0x00)
			if err == io.EOF {
				break loop
			}
			if err != nil {
				return nil, err
			}
			f[k] = &Variable{Data: b}
		case
			AddrNPI,
			AddrTON,
			DataCoding,
			DestAddrNPI,
			DestAddrTON,
			ESMClass,
			InterfaceVersion,
			NumberDests,
			NoUnsuccess,
			PriorityFlag,
			ProtocolID,
			RegisteredDelivery,
			ReplaceIfPresentFlag,
			SMDefaultMsgID,
			SourceAddrNPI,
			SourceAddrTON:
			b, err := r.ReadByte()
			if err == io.EOF {
				break loop
			}
			if err != nil {
				return nil, err
			}
			f[k] = &Fixed{Data: b}
			if k == NoUnsuccess {
				unsuccessCount = int(b)
			} else if k == NumberDests {
				numDest = int(b)
			}
		case DestinationList:
			var destList []DestSme
			for i := 0; i < numDest; i++ {
				var dest DestSme
				// Read DestFlag
				b, err := r.ReadByte()
				if err == io.EOF {
					break loop
				}
				if err != nil {
					return nil, err
				}
				dest.Flag = Fixed{Data: b}
				// Read Ton
				b, err = r.ReadByte()
				if err == io.EOF {
					break loop
				}
				if err != nil {
					return nil, err
				}
				dest.Ton = Fixed{Data: b}
				// Read npi
				b, err = r.ReadByte()
				if err == io.EOF {
					break loop
				}
				if err != nil {
					return nil, err
				}
				dest.Npi = Fixed{Data: b}
				// Read address
				bt, err := r.ReadBytes(0x00)
				if err == io.EOF {
					break loop
				}
				if err != nil {
					return nil, err
				}
				dest.DestAddr = Variable{Data: bt}
				destList = append(destList, dest)
			}
			f[k] = &DestSmeList{Data: destList}
		case UnsuccessSme:
			var unsList []UnSme
			for i := 0; i < unsuccessCount; i++ {
				var uns UnSme
				// Read Ton
				b, err := r.ReadByte()
				if err == io.EOF {
					break loop
				}
				if err != nil {
					return nil, err
				}
				uns.Ton = Fixed{Data: b}
				// Read npi
				b, err = r.ReadByte()
				if err == io.EOF {
					break loop
				}
				if err != nil {
					return nil, err
				}
				uns.Npi = Fixed{Data: b}
				// Read address
				bt, err := r.ReadBytes(0x00)
				if err == io.EOF {
					break loop
				}
				if err != nil {
					return nil, err
				}
				uns.DestAddr = Variable{Data: bt}
				// Read error code
				uns.ErrCode = Variable{Data: r.Next(4)}
				// Add unSme to the list
				unsList = append(unsList, uns)
			}
			f[k] = &UnSmeList{Data: unsList}
		case SMLength:
			b, err := r.ReadByte()
			if err == io.EOF {
				break loop
			}
			if err != nil {
				return nil, err
			}
			l := int(b)
			f[k] = &Fixed{Data: b}
			if r.Len() < l {
				return nil, fmt.Errorf("short read for smlength: want %d, have %d",
					l, r.Len())
			}
			f[ShortMessage] = &SM{Data: r.Next(l)}
		}
	}
	return f, nil
}
Exemple #25
0
func readMeta(buf *bytes.Buffer) (Meta, error) {
	meta := Meta{}
	for {
		line, err := buf.ReadBytes('\n')
		if err == io.EOF && len(line) == 0 {
			break
		}
		if err != nil && err != io.EOF {
			return Meta{}, err
		}

		line = trim(line)
		switch {
		case hasPrefix(line, "HH"):
			meta.FormatVersion = str(line)
		case hasPrefix(line, "NAME"):
			meta.Name = str(line[4:])
		case hasPrefix(line, "FAM"):
			meta.Fam = str(line[3:])
		case hasPrefix(line, "FILE"):
			meta.File = str(line[4:])
		case hasPrefix(line, "LENG"):
			meta.Leng = str(line[4:])
		case hasPrefix(line, "FILT"):
			meta.Filt = str(line[4:])
		case hasPrefix(line, "NEFF"):
			// You'd think we could use readNeff here, but does the HHM
			// format store all Neff values equally? NOOOOOOOOOOOOOOOOOOOO.
			f, err := strconv.ParseFloat(str(line[4:]), 64)
			if err != nil {
				return Meta{}, err
			}
			meta.Neff = seq.Prob(f)
		case hasPrefix(line, "EVD"):
			fields := bytes.Fields(bytes.TrimSpace(line[3:]))
			if len(fields) != 2 {
				return Meta{}, fmt.Errorf("Invalid EVD format: '%s'", line)
			}

			lambda, err := strconv.ParseFloat(string(fields[0]), 64)
			if err != nil {
				return Meta{}, fmt.Errorf("Error EVD lambda '%s': %s",
					string(fields[0]), err)
			}
			meta.EvdLambda = lambda

			mu, err := strconv.ParseFloat(string(fields[1]), 64)
			if err != nil {
				return Meta{}, fmt.Errorf("Error EVD mu '%s': %s",
					string(fields[1]), err)
			}
			meta.EvdMu = mu
		case hasPrefix(line, "PCT"):
			meta.Pct = true
		case hasPrefix(line, "DESC"):
			meta.Desc = str(line[4:])
		case hasPrefix(line, "COM"):
			meta.Com = str(line[3:])
		case hasPrefix(line, "DATE"):
			meta.Date = str(line[4:])
		}
	}
	return meta, nil
}
Exemple #26
0
// Starts the underlying subprocess, communicating with it to negotiate
// a port for RPC connections, and returning the address to connect via RPC.
//
// This method is safe to call multiple times. Subsequent calls have no effect.
// Once a client has been started once, it cannot be started again, even if
// it was killed.
func (c *Client) Start() (address string, err error) {
	c.l.Lock()
	defer c.l.Unlock()

	if c.address != "" {
		return c.address, nil
	}

	c.doneLogging = make(chan struct{})

	env := []string{
		fmt.Sprintf("%s=%s", MagicCookieKey, MagicCookieValue),
		fmt.Sprintf("PACKER_PLUGIN_MIN_PORT=%d", c.config.MinPort),
		fmt.Sprintf("PACKER_PLUGIN_MAX_PORT=%d", c.config.MaxPort),
	}

	stdout := new(bytes.Buffer)
	stderr_r, stderr_w := io.Pipe()

	cmd := c.config.Cmd
	cmd.Env = append(cmd.Env, os.Environ()...)
	cmd.Env = append(cmd.Env, env...)
	cmd.Stdin = os.Stdin
	cmd.Stderr = stderr_w
	cmd.Stdout = stdout

	log.Printf("Starting plugin: %s %#v", cmd.Path, cmd.Args)
	err = cmd.Start()
	if err != nil {
		return
	}

	// Make sure the command is properly cleaned up if there is an error
	defer func() {
		r := recover()

		if err != nil || r != nil {
			cmd.Process.Kill()
		}

		if r != nil {
			panic(r)
		}
	}()

	// Start goroutine to wait for process to exit
	go func() {
		// Make sure we close the write end of our stderr listener so
		// that the log goroutine ends properly.
		defer stderr_w.Close()

		// Wait for the command to end.
		cmd.Wait()

		// Log and make sure to flush the logs write away
		log.Printf("%s: plugin process exited\n", cmd.Path)
		os.Stderr.Sync()

		// Mark that we exited
		c.exited = true
	}()

	// Start goroutine that logs the stderr
	go c.logStderr(stderr_r)

	// Some channels for the next step
	timeout := time.After(c.config.StartTimeout)

	// Start looking for the address
	log.Printf("Waiting for RPC address for: %s", cmd.Path)
	for done := false; !done; {
		select {
		case <-timeout:
			err = errors.New("timeout while waiting for plugin to start")
			done = true
		default:
		}

		if err == nil && c.Exited() {
			err = errors.New("plugin exited before we could connect")
			done = true
		}

		if line, lerr := stdout.ReadBytes('\n'); lerr == nil {
			// Trim the address and reset the err since we were able
			// to read some sort of address.
			c.address = strings.TrimSpace(string(line))
			address = c.address
			err = nil
			break
		}

		// If error is nil from previously, return now
		if err != nil {
			return
		}

		// Wait a bit
		time.Sleep(10 * time.Millisecond)
	}

	return
}
Exemple #27
0
func (rcv *UDP) receiveWorker(exit chan bool) {
	defer rcv.conn.Close()

	var buf [65535]byte

	var data *bytes.Buffer

	lines := newIncompleteStorage()

	for {
		rlen, peer, err := rcv.conn.ReadFromUDP(buf[:])
		if err != nil {
			if strings.Contains(err.Error(), "use of closed network connection") {
				break
			}
			atomic.AddUint32(&rcv.errors, 1)
			logrus.Error(err)
			continue
		}

		prev := lines.pop(peer.String())

		if prev != nil {
			data = bytes.NewBuffer(prev)
			data.Write(buf[:rlen])
		} else {
			data = bytes.NewBuffer(buf[:rlen])
		}

		for {
			line, err := data.ReadBytes('\n')

			if err != nil {
				if err == io.EOF {
					if len(line) > 0 { // incomplete line received

						if rcv.logIncomplete {
							logIncomplete(peer, buf[:rlen], line)
						}

						lines.store(peer.String(), line)
						atomic.AddUint32(&rcv.incompleteReceived, 1)
					}
				} else {
					atomic.AddUint32(&rcv.errors, 1)
					logrus.Error(err)
				}
				break
			}
			if len(line) > 0 { // skip empty lines
				if msg, err := points.ParseText(string(line)); err != nil {
					atomic.AddUint32(&rcv.errors, 1)
					logrus.Info(err)
				} else {
					atomic.AddUint32(&rcv.metricsReceived, 1)
					rcv.out <- msg
				}
			}
		}
	}
}
Exemple #28
0
// Decode decodes binary data in the given buffer to build a Map.
//
// If the ShortMessage field is present, and DataCoding as well,
// we attempt to decode text automatically. See pdutext package
// for more information.
func (l List) Decode(r *bytes.Buffer) (Map, error) {
	f := make(Map)
loop:
	for _, k := range l {
		switch k {
		case
			AddressRange,
			DestinationAddr,
			ErrorCode,
			FinalDate,
			MessageID,
			MessageState,
			Password,
			ScheduleDeliveryTime,
			ServiceType,
			SourceAddr,
			SystemID,
			SystemType,
			ValidityPeriod:
			b, err := r.ReadBytes(0x00)
			if err == io.EOF {
				break loop
			}
			if err != nil {
				return nil, err
			}
			f[k] = &Variable{Data: b}
		case
			AddrNPI,
			AddrTON,
			DataCoding,
			DestAddrNPI,
			DestAddrTON,
			ESMClass,
			InterfaceVersion,
			PriorityFlag,
			ProtocolID,
			RegisteredDelivery,
			ReplaceIfPresentFlag,
			SMDefaultMsgID,
			SourceAddrNPI,
			SourceAddrTON:
			b, err := r.ReadByte()
			if err == io.EOF {
				break loop
			}
			if err != nil {
				return nil, err
			}
			f[k] = &Fixed{Data: b}
		case SMLength:
			b, err := r.ReadByte()
			if err == io.EOF {
				break loop
			}
			if err != nil {
				return nil, err
			}
			l := int(b)
			f[k] = &Fixed{Data: b}
			if r.Len() < l {
				return nil, fmt.Errorf("short read for smlength: want %d, have %d",
					l, r.Len())
			}
			f[ShortMessage] = &SM{Data: r.Next(l)}
		case ShortMessage:
			sm, exists := f[ShortMessage].(*SM)
			if !exists {
				continue
			}
			c, exists := f[DataCoding].(*Fixed)
			if !exists {
				continue
			}
			sm.Data = pdutext.Decode(pdutext.DataCoding(c.Data), sm.Data)
		}
	}
	return f, nil
}
Exemple #29
0
// Listen bind port. Receive messages and send to out channel
func (rcv *UDP) Listen(addr *net.UDPAddr) error {
	var err error
	rcv.conn, err = net.ListenUDP("udp", addr)
	if err != nil {
		return err
	}

	go func() {
		ticker := time.NewTicker(rcv.metricInterval)
		defer ticker.Stop()

		for {
			select {
			case <-ticker.C:
				metricsReceived := atomic.LoadUint32(&rcv.metricsReceived)
				atomic.AddUint32(&rcv.metricsReceived, -metricsReceived)
				rcv.Stat("udp.metricsReceived", float64(metricsReceived))

				incompleteReceived := atomic.LoadUint32(&rcv.incompleteReceived)
				atomic.AddUint32(&rcv.incompleteReceived, -incompleteReceived)
				rcv.Stat("udp.incompleteReceived", float64(incompleteReceived))

				errors := atomic.LoadUint32(&rcv.errors)
				atomic.AddUint32(&rcv.errors, -errors)
				rcv.Stat("udp.errors", float64(errors))

				logrus.WithFields(logrus.Fields{
					"metricsReceived":    int(metricsReceived),
					"incompleteReceived": int(incompleteReceived),
					"errors":             int(errors),
				}).Info("[udp] doCheckpoint()")

			case <-rcv.exit:
				rcv.conn.Close()
				return
			}
		}
	}()

	go func() {
		defer rcv.conn.Close()

		var buf [2048]byte

		var data *bytes.Buffer

		lines := newIncompleteStorage()

		for {
			rlen, peer, err := rcv.conn.ReadFromUDP(buf[:])
			if err != nil {
				if strings.Contains(err.Error(), "use of closed network connection") {
					break
				}
				atomic.AddUint32(&rcv.errors, 1)
				logrus.Error(err)
				continue
			}

			prev := lines.pop(peer.String())

			if prev != nil {
				data = bytes.NewBuffer(prev)
				data.Write(buf[:rlen])
			} else {
				data = bytes.NewBuffer(buf[:rlen])
			}

			for {
				line, err := data.ReadBytes('\n')

				if err != nil {
					if err == io.EOF {
						if len(line) > 0 { // incomplete line received

							if rcv.logIncomplete {
								logIncomplete(peer, buf[:rlen], line)
							}

							lines.store(peer.String(), line)
							atomic.AddUint32(&rcv.incompleteReceived, 1)
						}
					} else {
						atomic.AddUint32(&rcv.errors, 1)
						logrus.Error(err)
					}
					break
				}
				if len(line) > 0 { // skip empty lines
					if msg, err := points.ParseText(string(line)); err != nil {
						atomic.AddUint32(&rcv.errors, 1)
						logrus.Info(err)
					} else {
						atomic.AddUint32(&rcv.metricsReceived, 1)
						rcv.out <- msg
					}
				}
			}
		}

		close(rcv.finished)

	}()

	return nil
}
Exemple #30
0
func create_pdu_fields(fieldNames []string, r *bytes.Buffer) (map[string]Field, map[uint16]*TLVField, error) {

	fields := make(map[string]Field)
	eof := false
	for _, k := range fieldNames {
		switch k {
		case SERVICE_TYPE, SOURCE_ADDR, DESTINATION_ADDR, SCHEDULE_DELIVERY_TIME, VALIDITY_PERIOD, SYSTEM_ID, PASSWORD, SYSTEM_TYPE, ADDRESS_RANGE, MESSAGE_ID, FINAL_DATE, MESSAGE_STATE, ERROR_CODE:
			// Review this for fields that could be 1 or 17 int in length (E.g: FINAL_DATE)
			t, err := r.ReadBytes(0x00)

			if err == io.EOF {
				eof = true
			} else if err != nil {
				return nil, nil, err
			}

			if len(t) == 0 {
				fields[k] = NewVariableField(t)
			} else {
				fields[k] = NewVariableField(t[:len(t)-1])
			}
		case SOURCE_ADDR_TON, SOURCE_ADDR_NPI, DEST_ADDR_TON, DEST_ADDR_NPI, ESM_CLASS, PROTOCOL_ID, PRIORITY_FLAG, REGISTERED_DELIVERY, REPLACE_IF_PRESENT_FLAG, DATA_CODING, SM_DEFAULT_MSG_ID, INTERFACE_VERSION, ADDR_TON, ADDR_NPI:
			t, err := r.ReadByte()

			if err == io.EOF {
				eof = true
			} else if err != nil {
				return nil, nil, err
			}

			fields[k] = NewFixedField(t)
		case SM_LENGTH:
			// Short Message Length
			t, err := r.ReadByte()

			if err == io.EOF {
				eof = true
			} else if err != nil {
				return nil, nil, err
			}

			fields[k] = NewFixedField(t)

			// Short Message
			p := make([]byte, t)

			_, err = r.Read(p)
			if err == io.EOF {
				eof = true
			} else if err != nil {
				return nil, nil, err
			}

			fields[SHORT_MESSAGE] = NewSMField(p)
		case SHORT_MESSAGE:
			continue
		}
	}

	// Optional Fields
	tlvs := map[uint16]*TLVField{}
	var err error

	if !eof {
		tlvs, err = parse_tlv_fields(r)

		if err != nil {
			return nil, nil, err
		}
	}

	return fields, tlvs, nil
}