Exemplo n.º 1
0
func writeOne(bw *bufio.Writer, x interface{}) error {
	switch y := x.(type) {
	case bool:
		fmt.Fprintf(bw, "%t", y)
	case byte:
		fmt.Fprintf(bw, "'%c'", y)
	case string:
		fmt.Fprintf(bw, "\"%s\"", y)
	case int, int64:
		fmt.Fprintf(bw, "%d", y)
	case float32, float64:
		fmt.Fprintf(bw, "%f", y)
	default:
		slice := reflect.ValueOf(x)
		if slice.Kind() != reflect.Slice {
			return NewErrType(slice.Type())
		}
		bw.WriteByte('[')
		for i := 0; i < slice.Len(); i++ {
			if i != 0 {
				bw.WriteString(", ")
			}
			elem := slice.Index(i)
			if err := writeOne(bw, elem.Interface()); err != nil {
				return err
			}
		}
		bw.WriteByte(']')
	}
	return nil
}
Exemplo n.º 2
0
Arquivo: fml.go Projeto: FIPress/fml
func (f *FML) writeTo(parentKey string, writer *bufio.Writer) {
	for key, val := range f.dict {
		fullKey := key
		if len(parentKey) != 0 {
			fullKey = parentKey + "." + key
		}
		switch v := val.(type) {
		case []*FML:
			defer func() {
				writeNodeName(fullKey, writer)
				for i := 0; i < len(v); i++ {
					writer.Write([]byte{'-', ' '})
					v[i].writeTo(fullKey, writer)
				}
			}()
		case *FML:
			defer func() {
				writeNodeName(fullKey, writer)
				v.writeTo(fullKey, writer)
			}()
		default:
			//log.Println("write",key)
			writer.WriteString(key)
			writer.WriteByte(':')
			writer.WriteString(wrapVal(v))
			//writer.WriteString("after key value")
			writer.WriteByte('\n')
		}
	}
}
Exemplo n.º 3
0
func HandshakeSample(c net.Conn, br *bufio.Reader, bw *bufio.Writer, timeout time.Duration) (err error) {
	defer func() {
		if r := recover(); r != nil {
			err = r.(error)
		}
	}()
	// Send C0+C1
	err = bw.WriteByte(0x03)
	c1 := CreateRandomBlock(RTMP_SIG_SIZE)
	for i := 0; i < 8; i++ {
		c1[i] = 0
	}
	bw.Write(c1)
	err = bw.Flush()
	CheckError(err, "Handshake() Flush C0+C1")
	// Read S0+S1+S2
	s0, err := br.ReadByte()
	CheckError(err, "Handshake() Read S0")
	if s0 != 0x03 {
		return errors.New(fmt.Sprintf("Handshake() Got S0: %x", s0))
	}
	s1 := make([]byte, RTMP_SIG_SIZE)
	_, err = io.ReadAtLeast(br, s1, RTMP_SIG_SIZE)
	CheckError(err, "Handshake() Read S1")
	bw.Write(s1)
	err = bw.Flush()
	CheckError(err, "Handshake() Flush C2")
	_, err = io.ReadAtLeast(br, s1, RTMP_SIG_SIZE)
	CheckError(err, "Handshake() Read S2")
	return
}
Exemplo n.º 4
0
// Write embeddings to a binary file accepted by word2vec
func (e *Embeddings) Write(w *bufio.Writer) error {
	nWords := len(e.words)
	if nWords == 0 {
		return nil
	}

	if e.embedSize == 0 {
		return nil
	}

	if _, err := fmt.Fprintf(w, "%d %d\n", nWords, e.embedSize); err != nil {
		return err
	}

	for idx, word := range e.words {
		if _, err := w.WriteString(word + " "); err != nil {
			return err
		}

		if err := binary.Write(w, binary.LittleEndian, e.lookupIdx(idx)); err != nil {
			return err
		}

		w.WriteByte(0x0a)
	}

	return nil
}
Exemplo n.º 5
0
func emit_nocall_ref(start_pos int64, n int64, ref_ain *simplestream.SimpleStream, aout *bufio.Writer) (int64, error) {

	end_pos := start_pos + n
	for ; start_pos < end_pos; start_pos++ {

		if ref_ain.Pos >= ref_ain.N {
			if e := ref_ain.Refresh(); e != nil {
				return 0, e
			}
		}

		bp := ref_ain.Buf[ref_ain.Pos]
		ref_ain.Pos++

		switch bp {
		case 'a', 'A':
			aout.WriteByte('A')
		case 'c', 'C':
			aout.WriteByte('C')
		case 'g', 'G':
			aout.WriteByte('G')
		case 't', 'T':
			aout.WriteByte('T')
		default:
			aout.WriteByte(bp)
		}

		gCounter++

	}

	return start_pos, nil

}
Exemplo n.º 6
0
func (r *Replica) bcastCommit(cstruct []int32) {
	defer func() {
		if err := recover(); err != nil {
			dlog.Println("Commit bcast failed:", err)
		}
	}()
	args := &gpaxosproto.Commit{cstruct}

	n := r.N - 1
	q := r.Id
	var w *bufio.Writer
	for sent := 0; sent < n; {
		q = (q + 1) % int32(r.N)
		if q == r.Id {
			break
		}
		if !r.Alive[q] {
			continue
		}
		sent++
		w = r.PeerWriters[q]
		w.WriteByte(gpaxosproto.COMMIT)
		args.Marshal(w)
		w.Flush()
	}
}
Exemplo n.º 7
0
/*
	Frame physical write.
*/
func (f *Frame) writeFrame(w *bufio.Writer, l string) error {
	// Write the frame Command
	if _, e := w.WriteString(f.Command + "\n"); e != nil {
		return e
	}
	// Content length - Always add it if client does not suppress it and
	// does not supply it.
	if _, ok := f.Headers.Contains("suppress-content-length"); !ok {
		if _, clok := f.Headers.Contains("content-length"); !clok {
			f.Headers = append(f.Headers, "content-length", strconv.Itoa(len(f.Body)))
		}
	}
	// Write the frame Headers
	for i := 0; i < len(f.Headers); i += 2 {
		if l > SPL_10 && f.Command != CONNECT {
			f.Headers[i] = encode(f.Headers[i])
			f.Headers[i+1] = encode(f.Headers[i+1])
		}
		_, e := w.WriteString(f.Headers[i] + ":" + f.Headers[i+1] + "\n")
		if e != nil {
			return e
		}
	}
	// Write the last Header LF
	if e := w.WriteByte('\n'); e != nil {
		return e
	}
	// Write the body
	if len(f.Body) != 0 { // Foolish to write 0 length data
		if _, e := w.Write(f.Body); e != nil {
			return e
		}
	}
	return nil
}
Exemplo n.º 8
0
func (r *Replica) bcastPrepare(replica int32, instance int32, ballot int32) {
	defer func() {
		if err := recover(); err != nil {
			dlog.Println("Prepare bcast failed:", err)
		}
	}()
	args := &gpaxosproto.Prepare{r.Id, instance, ballot}

	n := r.N - 1
	//TODO: fix quorum size
	if r.Thrifty {
		n = r.N >> 1
	}
	q := r.Id
	var w *bufio.Writer
	for sent := 0; sent < n; {
		q = (q + 1) % int32(r.N)
		if q == r.Id {
			dlog.Println("Not enough replicas alive!")
			break
		}
		if !r.Alive[q] {
			continue
		}
		sent++
		w = r.PeerWriters[q]
		w.WriteByte(gpaxosproto.PREPARE)
		args.Marshal(w)
		w.Flush()
	}
}
Exemplo n.º 9
0
func (r *Replica) bcast2a(balnum int32, cstruct []int32, fast bool) {
	defer func() {
		if err := recover(); err != nil {
			dlog.Println("1a bcast failed:", err)
		}
	}()
	args := &gpaxosproto.M_2a{r.Id, balnum, cstruct}

	n := r.N - 1
	if r.Thrifty {
		if fast {
			n = r.fastQSize - 1
		} else {
			n = r.N >> 1
		}
	}
	q := r.Id
	var w *bufio.Writer
	for sent := 0; sent < n; {
		q = (q + 1) % int32(r.N)
		if q == r.Id {
			break
		}
		if !r.Alive[q] {
			continue
		}
		sent++
		w = r.PeerWriters[q]
		w.WriteByte(gpaxosproto.M2A)
		args.Marshal(w)
		w.Flush()
	}
}
Exemplo n.º 10
0
// writeTo serializes the processing instruction to the writer.
func (p *ProcInst) writeTo(w *bufio.Writer) {
	w.WriteString("<?")
	w.WriteString(p.Target)
	w.WriteByte(' ')
	w.WriteString(p.Inst)
	w.WriteString("?>")
}
Exemplo n.º 11
0
/**
 * Write long data packet
 */
func (pkt *packetLongData) write(writer *bufio.Writer) (err os.Error) {
	// Construct packet header
	pkt.header = new(packetHeader)
	pkt.header.length = 7 + uint32(len(pkt.data))
	pkt.header.sequence = pkt.sequence
	err = pkt.header.write(writer)
	if err != nil {
		return
	}
	// Write command
	err = writer.WriteByte(byte(pkt.command))
	if err != nil {
		return
	}
	// Write statement id
	err = pkt.writeNumber(writer, uint64(pkt.statementId), 4)
	if err != nil {
		return
	}
	// Write param number
	err = pkt.writeNumber(writer, uint64(pkt.paramNumber), 2)
	if err != nil {
		return
	}
	// Write data
	_, err = writer.WriteString(pkt.data)
	if err != nil {
		return
	}
	// Flush
	err = writer.Flush()
	return
}
Exemplo n.º 12
0
func (r *AsyncResponse) WriteResponse(buf *bufio.Writer) error {
	_, err := buf.WriteString("+ASYNC ")
	_, err = buf.WriteString(r.asyncID)
	err = buf.WriteByte(' ')
	err = r.resp.WriteResponse(buf)
	return err
}
Exemplo n.º 13
0
func (g *CGIRefVar) WritePastaByte(pasta_ch byte, out *bufio.Writer) {
	out.WriteByte(pasta_ch)
	g.OCounter++
	if (g.LFMod > 0) && (g.OCounter > 0) && ((g.OCounter % g.LFMod) == 0) {
		out.WriteByte('\n')
	}
}
Exemplo n.º 14
0
// writeTo serializes the element to the writer w.
func (e *Element) writeTo(w *bufio.Writer) {
	w.WriteByte('<')
	if e.Space != "" {
		w.WriteString(e.Space)
		w.WriteByte(':')
	}
	w.WriteString(e.Tag)
	for _, a := range e.Attr {
		w.WriteByte(' ')
		a.writeTo(w)
	}
	if len(e.Child) > 0 {
		w.WriteString(">")
		for _, c := range e.Child {
			c.writeTo(w)
		}
		w.Write([]byte{'<', '/'})
		if e.Space != "" {
			w.WriteString(e.Space)
			w.WriteByte(':')
		}
		w.WriteString(e.Tag)
		w.WriteByte('>')
	} else {
		w.Write([]byte{'/', '>'})
	}
}
Exemplo n.º 15
0
func (p *MsgResponseItem) WriteResponse(buf *bufio.Writer) error {

	v := 5
	if p.msg.UnlockTs > 0 {
		v = 6
	}

	err := enc.WriteDictSize(buf, v)
	_, err = buf.WriteString(" ID ")
	err = enc.WriteString(buf, p.msg.StrId)

	_, err = buf.WriteString(" PL ")
	err = enc.WriteBytes(buf, p.payload)

	_, err = buf.WriteString(" ETS ")
	err = enc.WriteInt64(buf, p.msg.ExpireTs)

	_, err = buf.WriteString(" POPCNT ")
	err = enc.WriteInt64(buf, p.msg.PopCount)

	_, err = buf.WriteString(" UTS ")
	err = enc.WriteInt64(buf, p.msg.UnlockTs)

	if p.msg.UnlockTs > 0 {
		_, err = buf.WriteString(" RCPT ")
		_, err = buf.WriteString(enc.EncodeTo36Base(p.msg.SerialNumber))
		err = buf.WriteByte('-')
		_, err = buf.WriteString(enc.EncodeTo36Base(uint64(p.msg.PopCount)))
	}
	return err
}
Exemplo n.º 16
0
func groupLs(r *bufio.Reader, w *bufio.Writer, args []string, thisUser *utils.User) (err error) {

	if len(args) != 2 {
		fmt.Println("Wrong number of arguments for call to group-ls:")
		fmt.Println("Format should be: group-ls GID")
		return nil
	}

	err = w.WriteByte(23)
	w.Flush()
	if err != nil {
		return err
	}

	// send the group id
	w.WriteString(args[1] + "\n")
	w.Flush()

	// get success (1) or fail (2)
	success, _ := r.ReadByte()
	if success != 1 {
		fmt.Println("The group does not exist")
		return err
	}

	// get uuids of the group
	result, _ := r.ReadString('\n')
	result = strings.TrimSuffix(strings.TrimSpace(result), ",")

	fmt.Println("Members of Group " + args[1] + ": " + result)

	return err
}
Exemplo n.º 17
0
func WriteError(b *bufio.Writer, code int64, text string) error {
	_, err := b.WriteString("-ERR ")
	err = WriteInt64(b, code)
	err = b.WriteByte(' ')
	err = WriteString(b, text)
	return err
}
Exemplo n.º 18
0
func rm(r *bufio.Reader, w *bufio.Writer, currentDir string, args []string) (err error) {

	// START SENDCODE BLOCK
	err = w.WriteByte(7)
	w.Flush()
	if err != nil {
		panic(err)
	}
	// END SENDCODE BLOCK

	// Send current dir
	w.WriteString(currentDir + "\n")
	w.Flush()

	// send len args
	err = w.WriteByte(uint8(len(args)))
	w.Flush()
	if err != nil {
		panic(err)
	}

	// send each arg (if it exists)
	for i := 1; i < len(args); i++ {

		w.WriteString(args[i] + "\n")
		w.Flush()
	}

	return err
}
Exemplo n.º 19
0
func createGroup(r *bufio.Reader, w *bufio.Writer, args []string, thisUser *utils.User) (err error) {

	if len(args) == 1 {
		fmt.Println("No arguments for call to create-group.")
		return nil
	}

	err = w.WriteByte(20)
	w.Flush()
	if err != nil {
		return err
	}

	// send the length of args
	err = w.WriteByte(uint8(len(args)))
	w.Flush()
	if err != nil {
		return err
	}

	for i := 1; i < len(args); i++ {

		// send group to create
		w.WriteString(args[i] + "\n")
		w.Flush()

		// get group id to display to user
		gid, _ := r.ReadString('\n')
		fmt.Println("Created group \"" + args[i] + "\" with id: " + strings.TrimSpace(gid))
	}
	// send the group to create

	return err
}
Exemplo n.º 20
0
func deleteGroup(r *bufio.Reader, w *bufio.Writer, args []string, thisUser *utils.User) (err error) {

	if len(args) < 2 {
		fmt.Println("Not enough arguments for call to group-remove:")
		fmt.Println("Format should be: delete-group GID")
		return nil
	}

	err = w.WriteByte(24)
	w.Flush()
	if err != nil {
		return err
	}

	w.WriteString(args[1] + "\n")
	w.Flush()

	// get success (1) or fail (2)
	success, _ := r.ReadByte()
	if success != 1 {
		fmt.Println("You cannot remove this group. Does it exist/are you the owner?")
		return err
	}

	fmt.Println("Removed: " + args[1])

	return err
}
Exemplo n.º 21
0
func WriteDict(b *bufio.Writer, dict map[string]interface{}) error {
	err := WriteDictSize(b, len(dict))

	if len(dict) == 0 {
		return nil
	}
	for k, v := range dict {
		err = b.WriteByte(' ')
		err = WriteString(b, k)
		err = b.WriteByte(' ')

		switch t := v.(type) {
		case string:
			err = WriteString(b, t)
		case int:
			err = WriteInt64(b, int64(t))
		case int64:
			err = WriteInt64(b, t)
		case bool:
			err = WriteBool(b, t)
		}

		if err != nil {
			break
		}
	}

	return err
}
Exemplo n.º 22
0
func WriteBytes(b *bufio.Writer, v []byte) error {
	err := b.WriteByte('$')
	_, err = b.WriteString(strconv.Itoa(len(v)))
	err = b.WriteByte(' ')
	_, err = b.Write(v)
	return err
}
Exemplo n.º 23
0
func newlineStream(dst *bufio.Writer, prefix, indent string, depth int) {
	dst.WriteByte('\n')
	dst.WriteString(prefix)
	for i := 0; i < depth; i++ {
		dst.WriteString(indent)
	}
}
Exemplo n.º 24
0
func writeString(buf *bufio.Writer, str string) {
	buf.WriteByte(size_byte)
	buf.WriteString(strconv.Itoa(len(str)))
	buf.Write(cr_lf)

	buf.WriteString(str)
	buf.Write(cr_lf)
}
Exemplo n.º 25
0
func writeBytes(buf *bufio.Writer, b []byte) {
	buf.WriteByte(size_byte)
	buf.WriteString(strconv.Itoa(len(b)))
	buf.Write(cr_lf)

	buf.Write(b)
	buf.Write(cr_lf)
}
Exemplo n.º 26
0
func (g *FASTAInfo) PastaEnd(out *bufio.Writer) error {

	if (g.LFMod > 0) && ((g.OCounter % g.LFMod) != 0) {
		out.WriteByte('\n')
	}

	out.Flush()
	return nil
}
Exemplo n.º 27
0
func (c *InfoCommand) Encode(w *bufio.Writer) error {
	if _, err := w.WriteString("info "); err != nil {
		return err
	}
	if _, err := w.WriteString(c.SetName); err != nil {
		return err
	}
	return w.WriteByte('\n')
}
Exemplo n.º 28
0
func (r *Replica) send2b(msg *gpaxosproto.M_2b, w *bufio.Writer) {
	w.WriteByte(gpaxosproto.M2B)
	msg.Marshal(w)
	for _, cid := range msg.Cids {
		cmd := r.commands[cid]
		cmd.Marshal(w)
	}
	w.Flush()
}
Exemplo n.º 29
0
// writeTo serializes the processing instruction to the writer.
func (p *ProcInst) writeTo(w *bufio.Writer, s *WriteSettings) {
	w.WriteString("<?")
	w.WriteString(p.Target)
	if p.Inst != "" {
		w.WriteByte(' ')
		w.WriteString(p.Inst)
	}
	w.WriteString("?>")
}
Exemplo n.º 30
0
func (r *MessagesResponse) WriteResponse(buf *bufio.Writer) error {
	_, err := buf.WriteString("+MSGS ")
	err = enc.WriteArraySize(buf, len(r.items))
	for _, item := range r.items {
		err = buf.WriteByte(' ')
		err = item.WriteResponse(buf)
	}
	return err
}