Beispiel #1
0
func (o *Overlord) handleConnection(conn io.ReadWriteCloser) {
	defer conn.Close()
	decoder := codec.NewDecoder(conn, hAsocket)
	encoder := codec.NewEncoder(conn, hAsocket)
	for {
		var msg cocaine.Message
		if err := decoder.Decode(&msg); err != nil {
			log.Printf("protocol decoder error %v", err)
			return
		}
		switch msg.Session {
		case utilitySession:
			if msg.MsgType == handshake {
				log.Println("replying to heartbeat")
				encoder.Encode(msg)
			}
		default:
			o.mu.Lock()
			if ch, ok := o.sessions[msg.Session]; ok {
				select {
				case ch <- &msg:
				case <-time.After(time.Second * 2):
					log.Println("didn't send reply during 2 seconds")
				}
			} else {
				log.Printf("Invalid session %v", msg)
			}
			o.mu.Unlock()
		}
	}
}
func TestAPIUnpack(t *testing.T) {
	// {0: ['resolve', {}, {0: ['value', {}], 1: ['error', {}]}],
	//  1: ['connect', {}, {0: ['write', None], 1: ['error', {}], 2: ['close', {}]}],
	//  2: ['refresh', {}, {0: ['value', {}], 1: ['error', {}]}],
	//  3: ['cluster', {}, {0: ['value', {}], 1: ['error', {}]}],
	//  4: ['publish', {0: ['discard', {}]}, {0: ['value', {}], 1: ['error', {}]}],
	//  5: ['routing', {}, {0: ['write', None], 1: ['error', {}], 2: ['close', {}]}]}
	payload := []byte{134, 0, 147, 167, 114, 101, 115, 111, 108, 118, 101, 128, 130, 0, 146, 165, 118, 97, 108, 117, 101, 128,
		1, 146, 165, 101, 114, 114, 111, 114, 128, 1, 147, 167, 99, 111, 110, 110, 101, 99, 116, 128, 131, 0, 146,
		165, 119, 114, 105, 116, 101, 192, 1, 146, 165, 101, 114, 114, 111, 114, 128, 2, 146, 165, 99, 108, 111, 115,
		101, 128, 2, 147, 167, 114, 101, 102, 114, 101, 115, 104, 128, 130, 0, 146, 165, 118, 97, 108, 117, 101, 128, 1,
		146, 165, 101, 114, 114, 111, 114, 128, 3, 147, 167, 99, 108, 117, 115, 116, 101, 114, 128, 130, 0, 146, 165, 118,
		97, 108, 117, 101, 128, 1, 146, 165, 101, 114, 114, 111, 114, 128, 4, 147, 167, 112, 117, 98, 108, 105, 115, 104,
		129, 0, 146, 167, 100, 105, 115, 99, 97, 114, 100, 128, 130, 0, 146, 165, 118, 97, 108, 117, 101, 128, 1, 146, 165,
		101, 114, 114, 111, 114, 128, 5, 147, 167, 114, 111, 117, 116, 105, 110, 103, 128, 131, 0, 146, 165, 119, 114, 105,
		116, 101, 192, 1, 146, 165, 101, 114, 114, 111, 114, 128, 2, 146, 165, 99, 108, 111, 115, 101, 128}

	var dm dispatchMap
	decoder := codec.NewDecoder(bytes.NewReader(payload), hAsocket)
	err := decoder.Decode(&dm)
	if !assert.NoError(t, err) {
		t.FailNow()
	}

	expected := []string{"resolve", "connect", "refresh", "cluster", "publish", "routing"}
	sort.Strings(expected)
	actual := dm.Methods()
	sort.Strings(actual)
	assert.Equal(t, expected, actual)

	for _, v := range dm {
		if v.Name == "connect" {
			assert.True(t, v.Downstream.Type() == emptyDispatch)
			assert.True(t, v.Upstream.Type() == otherDispatch)

			for _, descrItem := range *v.Upstream {
				t.Log(descrItem)
				switch descrItem.Name {
				case "write":
					assert.True(t, descrItem.Description.Type() == recursiveDispatch)
				case "close":
					assert.True(t, descrItem.Description.Type() == emptyDispatch)
				case "error":
					assert.True(t, descrItem.Description.Type() == emptyDispatch)
				}
			}
		}
	}

}
func (sock *asyncRWSocket) readloop() {
	go func() {
		decoder := codec.NewDecoder(bufio.NewReader(sock.conn), hAsocket)
		for {
			var message *Message
			err := decoder.Decode(&message)
			if err != nil {
				close(sock.downstreamBuf.in)
				sock.close()
				return
			}
			sock.downstreamBuf.in <- message
		}
	}()
}
//'[]byte{148,1,0,147,161,65,161,66,161,67,147,147,194,80,168,124,0,0,0,0,0,0,0,147,194,81,168,159,134,1,0,0,0,0,0,82}'
func TestMessageUnpack(t *testing.T) {
	// Payload is packed by Python:
	// traceid = 124
	// parentid = 0
	// spanid=999999
	// [(False, 80, '|\x00\x00\x00\x00\x00\x00\x00'),
	// (False, 81, '\x9f\x86\x01\x00\x00\x00\x00\x00'),
	// 82]
	// [1, 0, ["A", "B", "C"], z]
	payload := []byte{148, 100, 101, 147, 161, 65, 161, 66, 161, 67, 147, 147, 194, 80,
		168, 124, 0, 0, 0, 0, 0, 0, 0, 147, 194, 81, 168, 159, 134, 1, 0, 0, 0, 0, 0, 82}
	decoder := codec.NewDecoder(bytes.NewReader(payload), hAsocket)

	var message Message
	decoder.MustDecode(&message)

	assert.Equal(t, uint64(100), message.Session)
	assert.Equal(t, uint64(101), message.MsgType)
	headers := message.Headers
	assert.Equal(t, 3, len(headers))
}
func (unpacker *streamUnpacker) Feed(data []byte) []messageInterface {
	var msgs []messageInterface
	unpacker.buf = append(unpacker.buf, data...)
	tmp := bytes.NewBuffer(unpacker.buf)
	dec := codec.NewDecoder(tmp, h)
	for {
		var res []interface{}
		err := dec.Decode(&res)
		if err != nil {
			break
		} else {
			msg, err := unpackMessage(res)
			if err != nil {
				fmt.Printf("Error occured: %s", err)
				continue
			}
			msgs = append(msgs, msg)
			unpacker.buf = unpacker.buf[len(unpacker.buf)-tmp.Len():]
		}

	}
	return msgs
}