Example #1
0
// packs string literal into json object structure around variable "varName"
// data string should already be in json format
func (this *Neo4j) pack(name string, data string) ([]byte, os.Error) {
	buf := new(bytes.Buffer)
	err := json.Compact(buf, []byte("{ \""+name+"\": "+data+" } ")) // pkg data into new json string then compact() it onto our empty buffer
	if err != nil {
		return nil, err
	}
	return buf.Bytes(), err
}
// Encode takes payload, encodes it and writes it to dst. Payload must be one
// of the following: a heartbeat, a handshake, []byte, string, int or anything
// than can be marshalled by the default json package. If payload can't be
// encoded or the writing fails, an error will be returned.
func (enc *sioStreamingEncoder) Encode(dst io.Writer, payload interface{}) (err os.Error) {
	enc.elem.Reset()

	switch t := payload.(type) {
	case heartbeat:
		s := strconv.Itoa(int(t))
		_, err = fmt.Fprintf(dst, "%d:%d:%s,", sioMessageTypeHeartbeat, len(s), s)

	case handshake:
		_, err = fmt.Fprintf(dst, "%d:%d:%s,", sioMessageTypeHandshake, len(t), t)

	case []byte:
		l := utf8.RuneCount(t)
		if l == 0 {
			break
		}
		_, err = fmt.Fprintf(dst, "%d:%d::%s,", sioMessageTypeMessage, 1+l, t)

	case string:
		l := utf8.RuneCountInString(t)
		if l == 0 {
			break
		}
		_, err = fmt.Fprintf(dst, "%d:%d::%s,", sioMessageTypeMessage, 1+l, t)

	case int:
		s := strconv.Itoa(t)
		if s == "" {
			break
		}
		_, err = fmt.Fprintf(dst, "%d:%d::%s,", sioMessageTypeMessage, 1+len(s), s)

	default:
		data, err := json.Marshal(payload)
		if len(data) == 0 || err != nil {
			break
		}
		err = json.Compact(&enc.elem, data)
		if err != nil {
			break
		}

		_, err = fmt.Fprintf(dst, "%d:%d:%s\n:", sioMessageTypeMessage, 2+len(SIOAnnotationJSON)+utf8.RuneCount(enc.elem.Bytes()), SIOAnnotationJSON)
		if err == nil {
			_, err = enc.elem.WriteTo(dst)
			if err == nil {
				_, err = dst.Write([]byte{','})
			}
		}
	}

	return err
}
Example #3
0
// Encode takes payload, encodes it and writes it to dst. Payload must be one
// of the following: a heartbeat, a handshake, []byte, string, int or anything
// than can be marshalled by the default json package. If payload can't be
// encoded or the writing fails, an error will be returned.
func (enc *sioEncoder) Encode(dst io.Writer, payload interface{}) (err os.Error) {
	enc.elem.Reset()

	switch t := payload.(type) {
	case heartbeat:
		s := strconv.Itoa(int(t))
		_, err = fmt.Fprintf(dst, "%s%d%s%s%s", sioFrameDelim, len(s)+len(sioFrameDelimHeartbeat), sioFrameDelim, sioFrameDelimHeartbeat, s)

	case handshake:
		_, err = fmt.Fprintf(dst, "%s%d%s%s", sioFrameDelim, len(t), sioFrameDelim, t)

	case []byte:
		l := utf8.RuneCount(t)
		if l == 0 {
			break
		}
		_, err = fmt.Fprintf(dst, "%s%d%s%s", sioFrameDelim, l, sioFrameDelim, t)

	case string:
		l := utf8.RuneCountInString(t)
		if l == 0 {
			break
		}
		_, err = fmt.Fprintf(dst, "%s%d%s%s", sioFrameDelim, l, sioFrameDelim, t)

	case int:
		s := strconv.Itoa(t)
		if s == "" {
			break
		}
		_, err = fmt.Fprintf(dst, "%s%d%s%s", sioFrameDelim, len(s), sioFrameDelim, s)

	default:
		data, err := json.Marshal(payload)
		if len(data) == 0 || err != nil {
			break
		}
		err = json.Compact(&enc.elem, data)
		if err != nil {
			break
		}

		_, err = fmt.Fprintf(dst, "%s%d%s%s", sioFrameDelim, utf8.RuneCount(enc.elem.Bytes())+len(sioFrameDelimJSON), sioFrameDelim, sioFrameDelimJSON)
		if err == nil {
			_, err = enc.elem.WriteTo(dst)
		}
	}

	return err
}
func (a *Apns) SendMessage(identifier, expiry uint32, token []byte, payload interface{}) (<-chan *os.Error, os.Error) {
	buf := make([]byte, 256).(*buffer)

	if identifier == 0 && expiry == 0 {
		// use simple format
		buf.writeByte(0)
	} else {
		// use extended format
		buf.writeByte(1)
		buf.writeUint32(identifier)
		buf.writeUint32(expiry)
	}

	// append the token
	buf.writeUint16(uint16(len(token)))
	buf.write(token)

	// build the JSON data
	rawJson, err := json.Marshal(payload)
	if err != nil {
		return nil, err
	}

	// compact it (requires a bytes.Buffer to store the compacted version)
	jbuf := &bytes.Buffer{}
	err = json.Compact(jbuf, rawJson)
	if err != nil {
		return nil, err
	}

	// write the JSOB payload into the command buffer
	buf.writeUint32(uint32(jbuf.Len()))
	buf.write(jbuf.Bytes())

	// all done-- now send it!
	var sent int = 0
	l := len(buf)
	for sent < l {
		n, err = a.conn.Write(buf[sent:l])
		if err != nil {
			return nil, err
		}
		sent += n
	}

	// sent successfully, return a channel which will funnel the result back asynchronously
	errchan := make(chan *os.Error)

	if identifier != 0 {
		if a.waitReplies == nil {
			a.waitReplies = make(map[uint32]chan result)
		}
		a.waitReplies[identifier] = make(chan result, 1)

		go func() {
			var r result
			select {
			case r = <-a.waitReplies[identifier]:
				if r.status != 0 {
					// send an error
					errchan <- *Error{r.status}
				} else {
					errchan <- nil
				}
			}
		}()
	}

	return errchan, nil
}