Exemple #1
0
func (rc *ReceiveChannel) receiveTopicFrom(target string) {
	// connect to a TCP server
	network := "tcp"
	raddr, err := net.ResolveTCPAddr(network, target)
	if err != nil {
		log.Printf("Fail to resolve %s:%v", target, err)
		return
	}

	// println("dial tcp", raddr.String())
	conn, err := net.DialTCP(network, nil, raddr)
	if err != nil {
		log.Printf("Fail to dial %s:%v", raddr, err)
		time.Sleep(time.Second)
		return
	}
	defer conn.Close()

	buf := make([]byte, 4)

	util.WriteBytes(conn, buf, util.NewMessage(util.Data, []byte("GET "+rc.name)))

	util.WriteUint64(conn, rc.offset)

	util.WriteBytes(conn, buf, util.NewMessage(util.Data, []byte("ok")))

	ticker := time.NewTicker(time.Millisecond * 1100)
	defer ticker.Stop()
	go func() {
		buf := make([]byte, 4)
		for range ticker.C {
			util.WriteBytes(conn, buf, util.NewMessage(util.Data, []byte("ok")))
			// print(".")
		}
	}()

	for {
		f, data, err := util.ReadBytes(conn, buf)
		if err == io.EOF {
			// print("recieve close chan2: eof")
			break
		}
		if err != nil {
			log.Printf("receive error:%v", err)
			continue
		}
		rc.offset += 4 + 1
		if f != util.Data {
			// print("recieve close chan1: ", string([]byte{byte(f)}))
			break
		}
		// println("receive raw data :", string(data.Bytes()))
		rc.offset += uint64(len(data.Data()))
		rc.Ch <- data.Data()
	}
	close(rc.Ch)
}
Exemple #2
0
func (rc *ReceiveChannel) receiveTopicFrom(target string) {

	readWriter, err := util.Dial(rc.tlsConfig, target)
	if err != nil {
		log.Printf("Fail to dial receive %s: %v", target, err)
		return
	}
	defer readWriter.Close()

	// println("receiving", rc.name, "from", target)

	buf := make([]byte, 4)

	util.WriteBytes(readWriter, buf, util.NewMessage(util.Data, []byte("GET "+rc.name)))

	util.WriteUint64(readWriter, rc.offset)

	util.WriteBytes(readWriter, buf, util.NewMessage(util.Data, []byte("ok")))

	ticker := time.NewTicker(time.Millisecond * 1100)
	defer ticker.Stop()
	go func() {
		buf := make([]byte, 4)
		for range ticker.C {
			util.WriteBytes(readWriter, buf, util.NewMessage(util.Data, []byte("ok")))
			// print(".")
		}
	}()

	for {
		f, data, err := util.ReadBytes(readWriter, buf)
		if err == io.EOF {
			// print("recieve close chan2: eof")
			break
		}
		if err != nil {
			log.Printf("receive error:%v", err)
			continue
		}
		rc.offset += 4 + 1
		if f != util.Data {
			// print("recieve close chan1: ", string([]byte{byte(f)}))
			break
		}
		// println("receive raw data :", string(data.Bytes()))
		rc.offset += uint64(len(data.Data()))
		rc.Ch <- data.Data()
	}
	close(rc.Ch)
}
Exemple #3
0
// Handles incoming requests.
func (r *AgentServer) handleRequest(conn net.Conn) {

	buf := make([]byte, 4)

	f, message, err := util.ReadBytes(conn, buf)

	tlscon, ok := conn.(*tls.Conn)
	if ok {
		state := tlscon.ConnectionState()
		if !state.HandshakeComplete {
			log.Printf("Failed to tls handshake with: %+v", tlscon.RemoteAddr())
			return
		}
	}

	if f != util.Data {
		//strange if this happens
		println("read", len(message.Bytes()), "request flag:", f, "data", string(message.Data()))
		return
	}

	if err != nil {
		log.Printf("Failed to read command %s:%v", string(message.Data()), err)
	}
	if bytes.HasPrefix(message.Data(), []byte("PUT ")) {
		name := string(message.Data()[4:])
		r.handleLocalWriteConnection(conn, name)
	} else if bytes.HasPrefix(message.Data(), []byte("GET ")) {
		name := string(message.Data()[4:])
		offset := util.ReadUint64(conn)
		r.handleReadConnection(conn, name, int64(offset))
	} else if bytes.HasPrefix(message.Data(), []byte("CMD ")) {
		newCmd := &cmd.ControlMessage{}
		err := proto.Unmarshal(message.Data()[4:], newCmd)
		if err != nil {
			log.Fatal("unmarshaling error: ", err)
		}
		reply := r.handleCommandConnection(conn, newCmd)
		if reply != nil {
			data, err := proto.Marshal(reply)
			if err != nil {
				log.Fatal("marshaling error: ", err)
			}
			conn.Write(data)
		}
	}

}
Exemple #4
0
func (as *AgentServer) handleWriteConnection(r io.Reader, name string) {

	as.name2StoreCond.L.Lock()
	ds, ok := as.name2Store[name]
	if ok {
		as.doDelete(name)
	}

	s, err := store.NewLocalFileDataStore(as.dir, fmt.Sprintf("%s-%d", name, as.Port))
	if err != nil {
		log.Printf("Failed to create a queue on disk: %v", err)
		as.name2StoreCond.L.Unlock()
		return
	}

	as.name2Store[name] = NewLiveDataStore(s)
	ds = as.name2Store[name]
	// println(name, "is broadcasting...")
	as.name2StoreCond.Broadcast()

	as.name2StoreCond.L.Unlock()

	//register stream
	go client.NewHeartBeater(as.Port, *as.Option.Master).StartChannelHeartBeat(ds.killHeartBeater, name)

	// println(name, "start writing.")

	buf := make([]byte, 4)
	for {
		_, message, err := util.ReadBytes(r, buf)
		if err == io.EOF {
			// println("agent recv eof:", string(message.Bytes()))
			break
		}
		if err == nil {
			util.WriteBytes(ds.store, buf, message)
			// println("agent recv:", string(message.Bytes()))
		}
		if message.Flag() != util.Data {
			// println("finished writing", name)
			break
		}
	}
}
func (as *AgentServer) handleLocalWriteConnection(r io.Reader, name string) {

	dsStore := as.storageBackend.CreateNamedDatasetShard(name)

	// println(name, "start writing.")

	buf := make([]byte, 4)
	for {
		_, message, err := util.ReadBytes(r, buf)
		if err == io.EOF {
			// println("agent recv eof:", string(message.Bytes()))
			break
		}
		if err == nil {
			util.WriteBytes(dsStore, buf, message)
			// println("agent recv:", string(message.Bytes()))
		}
		if message.Flag() != util.Data {
			// println("finished writing", name)
			break
		}
	}
}
Exemple #6
0
func (as *AgentServer) handleLocalReadConnection(conn net.Conn, name string, offset int64) {
	var ds *LiveDataStore
	var ok bool

	as.name2StoreCond.L.Lock()
	for {
		ds, ok = as.name2Store[name]
		if ok {
			break
		}
		println(name, "is waiting to read...")
		as.name2StoreCond.Wait()
	}
	as.name2StoreCond.L.Unlock()

	// println(name, "start reading from:", offset)

	closeSignal := make(chan bool, 1)

	go func() {
		buf := make([]byte, 4)
		for false {
			// println("wait for reader heartbeat")
			conn.SetReadDeadline(time.Now().Add(2500 * time.Millisecond))
			_, _, err := util.ReadBytes(conn, buf)
			if err != nil {
				fmt.Printf("connection is closed? (%v)\n", err)
				closeSignal <- true
				close(closeSignal)
				return
			}
		}
	}()

	buf := make([]byte, 4)

	// loop for every read
	for {
		_, err := ds.store.ReadAt(buf, offset)
		if err != nil {
			// connection is closed
			if err != io.EOF {
				log.Printf("Read size from %s offset %d: %v", name, offset, err)
			}
			// println("got problem reading", name, offset, err.Error())
			return
		}

		offset += 4
		size := util.BytesToUint32(buf)

		// println("reading", name, offset, "size:", size)

		messageBytes := make([]byte, size)
		_, err = ds.store.ReadAt(messageBytes, offset)
		if err != nil {
			// connection is closed
			if err != io.EOF {
				log.Printf("Read data from %s offset %d: %v", name, offset, err)
			}
			return
		}
		offset += int64(size)

		m := util.LoadMessage(messageBytes)
		util.WriteBytes(conn, buf, m)

		if m.Flag() != util.Data {
			// println("Finished reading", name)
			break
		}
	}

}