Beispiel #1
0
func Interact(rw *bufio.ReadWriter, events chan<- Event, m *sb.Manager) (err error) {
	connC, err := NewConnection(m)
	if err != nil {
		return
	}
	go func() {
		for msg := range connC.Data.(*Connection).Messages {
			rw.WriteString(msg)
			rw.Flush()
		}
	}()

	events <- &NewConnectionEvent{connC}

	// rw.ReadLine seems to return a blank line immediately upon starting
	line, isPrefix, err := rw.ReadLine()
	for ; err == nil; line, isPrefix, err = rw.ReadLine() {
		if isPrefix {
			continue
		}
		s := bytes.NewBuffer(line).String()
		events <- &Command{s, connC}
	}

	events <- &DisconnectEvent{connC}

	return
}
Beispiel #2
0
func handleConnection(rw *bufio.ReadWriter) {

	m := Message{}
	db := make(map[string]map[string]string)
	confs := make(map[string]int)

	for {
		b, _, err := rw.ReadLine()
		if err != nil {
			break
		}
		if len(b) == 0 {
			r := Message{
				"ActionID": m["ActionID"],
			}
			switch m["Action"] {
			case "Login":
				r["Response"] = "Success"
			case "Originate":
				if strings.HasPrefix(m["Channel"], "fakeconference/") {
					arr := strings.Split(m["Channel"], "/")
					confs[arr[1]]++
					r["Response"] = "Success"
				} else {
					r["Response"] = "Error"
				}
			case "Redirect":
				r["Response"] = "Error"
			case "DBPut":
				r["Response"] = "Success"
				fam := m["Family"]
				if _, ok := db[fam]; !ok {
					db[fam] = make(map[string]string)
				}
				db[fam][m["Key"]] = m["Value"]
			case "DBDelTree":
				r["Response"] = "Success"
				delete(db, m["Family"])
			case "DBGet":
				ok := false
				var val string
				if val, ok = db[m["Family"]][m["Key"]]; ok {
					r["Response"] = "Success"
					writeMessage(rw, r)
					r = Message{
						"Val":    val,
						"Event":  "DbGetResponse",
						"Family": m["Family"],
						"Key":    m["Key"],
					}
				} else {
					r["Response"] = "Error"
				}
			case "ConfbridgeList":
				if _, ok := confs[m["Conference"]]; ok {
					for i := 0; i < confs[m["Conference"]]; i++ {
						mem := Message{
							"Member":   fmt.Sprint(i),
							"Channel":  "sip/" + fmt.Sprint(i),
							"ActionID": m["ActionID"],
						}
						writeMessage(rw, mem)
					}
					r["Event"] = "ConfbridgeListComplete"
					r["EventList"] = "Complete"
				} else {
					r["Response"] = "Error"
				}
			case "ConfbridgeKick":
				if _, ok := confs[m["Conference"]]; ok {
					delete(confs, m["Conference"])
					r["Response"] = "Success"
				} else {
					r["Response"] = "Error"
				}
			case "UserEvent":
				r = m
				r["Event"] = "UserEvent"
			default:
				continue
			}
			writeMessage(rw, r)
			m = Message{}
		} else {
			arr := strings.Split(string(b), ":")
			if len(arr) < 2 {
				m["Unknown"] += arr[0]
			} else {
				m[arr[0]] = arr[1]
			}
		}
	}
}
Beispiel #3
0
func readData(rw *bufio.ReadWriter) string {
	data, _, _ := rw.ReadLine()
	return string(data)
}
Beispiel #4
0
/*
  Will parse an io stream in to a packet struct
*/
func ReadPacket(reader *bufio.ReadWriter) (pkt *Packet, err error) {
	pkt = new(Packet)
	var pktTypeBytes []byte
	var bodyBytesNeeded int

	hdrBytes, _, err := reader.ReadLine()

	// if enableWriteTee {
	// 	writeTeeTarget.file.Write([]byte("read: "))
	// 	writeTeeTarget.file.Write(hdrBytes)
	// 	writeTeeTarget.file.Write([]byte("\n"))
	// }

	if err != nil {
		return nil, fmt.Errorf("readline error: %s", err)
	}

	hdrValsRead, err := fmt.Sscanf(string(hdrBytes), "%s %d %d", &pktTypeBytes, &(pkt.msgNo), &bodyBytesNeeded)
	if hdrValsRead != 3 {
		return nil, fmt.Errorf("header must have 3 parts")
	} else if err != nil {
		return nil, fmt.Errorf("sscanf error: %s", err.Error)
	}

	Trace.Printf("reading pkt: (%d, `%s`)", pkt.msgNo, pktTypeBytes)

	if bytes.Equal(header_bytes, pktTypeBytes) {
		pkt.packetType = HEADER
	} else if bytes.Equal(data_bytes, pktTypeBytes) {
		pkt.packetType = DATA
	} else if bytes.Equal(eof_bytes, pktTypeBytes) {
		pkt.packetType = EOF
	} else if bytes.Equal(txerr_bytes, pktTypeBytes) {
		pkt.packetType = TXERR
	} else if bytes.Equal(ack_bytes, pktTypeBytes) {
		pkt.packetType = ACK
	} else {
		return nil, fmt.Errorf("unknown packet type `%s`", pktTypeBytes)
	}

	// Use the msg len to consume the rest of the connection
	Trace.Printf("(%d) reading rest of packet body (%d bytes)", packetSeenSinceBoot, bodyBytesNeeded)
	pkt.body = make([]byte, bodyBytesNeeded)
	bytesRead, err := io.ReadFull(reader, pkt.body)
	if err != nil {
		return nil, fmt.Errorf("failed to read body: `%s`", err)
	}

	theRest := make([]byte, the_rest_size)
	bytesRead, err = io.ReadFull(reader, theRest)
	if bytesRead != the_rest_size || !bytes.Equal(theRest, []byte("END\r\n")) {
		return nil, fmt.Errorf("packet was missing trailing bytes")
	}

	if pkt.packetType == HEADER {
		err := pkt.parseHeader()
		if err != nil {
			return nil, fmt.Errorf("parseHeader err: `%s`", err)
		}
		pkt.body = nil
	}

	Trace.Printf("(%d) done reading packet", packetSeenSinceBoot)
	packetSeenSinceBoot = packetSeenSinceBoot + 1
	return pkt, nil
}
Beispiel #5
0
func (ms MemcachedProtocolServer) readLine(conn net.Conn, buf *bufio.ReadWriter) ([]byte, error) {
	conn.SetReadDeadline(time.Now().Add(time.Second * 10))
	d, _, err := buf.ReadLine()
	return d, err
}