Beispiel #1
0
func (collector Collector) StartCollectorServer() {
	service := ":1312"
	tcpAddr, err := net.ResolveTCPAddr("tcp4", service)
	lib.CheckError(err)
	listener, err := net.ListenTCP("tcp", tcpAddr)
	lib.CheckError(err)

	for {
		conn, err := listener.Accept()
		lib.CheckError(err)

		go collector.handleConnnection(conn)

	}
}
Beispiel #2
0
func (tc TcpClient) StartLogAgentServer() {
	service := ":1202"
	tcpAddr, err := net.ResolveTCPAddr("tcp4", service)
	lib.CheckError(err)
	listener, err := net.ListenTCP("tcp", tcpAddr)
	lib.CheckError(err)

	for {
		conn, err := listener.Accept()
		lib.CheckError(err)

		go tc.handleConnnection(conn, tc.logChan)

	}
}
Beispiel #3
0
func testClient() {

	//connect to server
	service := "localhost:1202"
	tcpAddr, err := net.ResolveTCPAddr("tcp4", service)
	lib.CheckError(err)
	conn, err := net.DialTCP("tcp", nil, tcpAddr)
	lib.CheckError(err)
	defer conn.Close()

	bio := bufio.NewReader(os.Stdin)
	for {
		//read line
		line, hasMoreInLine, err := bio.ReadLine()
		_ = hasMoreInLine
		_ = err
		data := string(line)
		if len(line) == 0 {
			continue
		}
		if data == "exit" {
			os.Exit(0)
		}
		fmt.Println("read from stdin", data)

		// // //analyze
		//    var words []string
		//    for _,word := range strings.Split(data," ") {
		//    	words = append(words,word)
		//    }

		//sending msg to server
		msg := data
		fmt.Println("sending:", msg)
		// err = gob.NewEncoder(conn).Encode(msg)
		_, err = conn.Write([]byte(msg))
		if err != nil {
			fmt.Println(err)
		}

		//get result
		// result, err := ioutil.ReadAll(conn)
		var temp []byte = make([]byte, 1000)
		_, err = conn.Read(temp)
		// lib.CheckError(err)
		fmt.Println("received from server:", string(temp))
	}
}
Beispiel #4
0
func (t *TcpReceiver) Start() {

	tcpAddr, err := net.ResolveTCPAddr("tcp4", t.receiveFromAddress)
	lib.CheckError(err)
	listener, err := net.ListenTCP("tcp", tcpAddr)
	lib.CheckError(err)

	wg := &sync.WaitGroup{}

	wg.Add(1)
	go t.clearFootPrint(wg)

	//主routine信号处理
	go lib.HandleQuitSignal(func() {
		//接收到信号关闭listenner,此时Accept会马上返回一个nil 的conn
		listener.Close()
		loglib.Info("close tcp receiver's listener.")
	})

	defer func() {
		if err := recover(); err != nil {
			loglib.Error(fmt.Sprintf("tcp receiver panic:%v", err))
		}

		loglib.Info("wait connections finish...")
		wg.Wait()
		loglib.Info("all connections have been processed. quit.")
		close(t.buffer) //关闭chan
		t.saveFootPrint()

		t.wq.AllDone()

	}()

	for {
		conn, err := listener.Accept()
		if conn == nil {
			break
		}
		lib.CheckError(err)
		wg.Add(1)
		go t.handleConnnection(conn, wg)
	}

}
Beispiel #5
0
func createSingleConnection(address string) (conn *net.TCPConn, err error) {

	tcpAddr, err := net.ResolveTCPAddr("tcp4", address)
	lib.CheckError(err)
	if err != nil {
		return nil, err
	}
	conn, err = net.DialTCP("tcp", nil, tcpAddr)
	if err != nil {
		loglib.Error("get connection from " + address + " failed! Error:" + err.Error())
		return nil, err
	} else {
		loglib.Info("get connection from " + address + " success! remote addr " + conn.RemoteAddr().String())
	}
	lib.CheckError(err)

	return conn, nil
}
Beispiel #6
0
//init conn list from addrMap
func (sc *SingleConnection) initConnection() {
	newConn, err := createSingleConnection(sc.currentAddr)
	if err != nil {
		loglib.Error("init err:" + err.Error())
	} else {
		sc.conn = newConn
	}

	lib.CheckError(err)
}
Beispiel #7
0
func (s Sender) sendData(data []byte, conn *net.TCPConn) bool {
	if len(data) == 0 {
		return true
	}

	if conn == nil {
		return false
	}
	/*
	   lenBuf := make([]byte, 4)
	   nData := len(data)
	   binary.PutUvarint(lenBuf, uint64(nData))
	   data = append(lenBuf, data...)
	*/

	st := time.Now()
	packId := tcp_pack.GetPackId(data)

	conn.SetDeadline(time.Now().Add(5 * time.Minute)) //设置超时
	loglib.Info(fmt.Sprintf("sender%d start sending pack:%s length:%d", s.id, packId, len(data)))
	n, err := conn.Write(data)
	ed := time.Now()
	loglib.Info(fmt.Sprintf("sender%d end sending pack:%s length:%d elapse:%s", s.id, packId, n, ed.Sub(st)))

	lib.CheckError(err)

	//写失败了就不用等应答了,肯定拿不到
	if err == nil {
		conn.SetReadDeadline(time.Now().Add(8 * time.Minute)) //设置超时
		time1 := time.Now()
		var temp []byte = make([]byte, 128)
		count, err := conn.Read(temp)
		if err == nil {
			loglib.Info(fmt.Sprintf("sender%d get anwser data len:%d for pack:%s elapse:%s", s.id, count, packId, time.Now().Sub(time1)))
		} else {
			loglib.Info(fmt.Sprintf("sender%d get anwser data len:%d for pack:%s elapse:%s, error:%s", s.id, count, packId, time.Now().Sub(time1), err.Error()))
		}

		temp = temp[:count]
		if string(temp) == "ok" { //发送成功
			return true
		} else if string(temp) == "wrong header" {
			//包头错误,丢弃
			loglib.Info(packId + " has wrong header, retry later!")
			return false
		} else { //发送失败
			//报警
			return false
		}
	} else {
		loglib.Warning(fmt.Sprintf("write pack %s error:%s", packId, err.Error()))
	}
	return false
}
Beispiel #8
0
func (s *Sender) writeToFile(data bytes.Buffer) {
	//写入文件
	filename := createFileName(s.id)
	//创建文件
	_, err := os.Create(filename)
	lib.CheckError(err)

	d := data.Bytes()

	packId := tcp_pack.GetPackId(d)

	loglib.Info(fmt.Sprintf("sender%d save pack %s to file %s len:%d", s.id, packId, filename, len(d)))
	err = ioutil.WriteFile(filename, d, 0666)
	if err != nil {
		loglib.Warning("write to file " + filename + " error:" + err.Error())
		lib.CheckError(err)
	} else {
		//追加fileCacheList
		fileList.PushBack(filename)
	}
}
Beispiel #9
0
func testClient2() {
	//connect to server
	service := "localhost:1302"
	tcpAddr, err := net.ResolveTCPAddr("tcp4", service)
	lib.CheckError(err)
	i := 1
	for {
		conn, err := net.DialTCP("tcp", nil, tcpAddr)
		lib.CheckError(err)
		defer conn.Close()

		fmt.Println("sending:", i)
		_, err = conn.Write([]byte(strconv.Itoa(i) + "\n"))
		// _,err = conn.Write([]byte("hahahhahahhahhahahahahahahhaahahahhhhhhhshdfsadfhasdhfajdhfahdsjfhajlhdfljadhfjhadshfjadhfljas"))
		i++

		if err != nil {
			fmt.Println(err)
		}

		time.Sleep(1000 * time.Millisecond)
	}

}
Beispiel #10
0
func (collector Collector) handleConnnection(conn net.Conn) {
	defer conn.Close()
	filename := "test.log"
	logFile, err := os.OpenFile(filename, os.O_RDWR|os.O_APPEND|os.O_CREATE, 0666)
	lib.CheckError(err)
	defer logFile.Close()

	conn.SetReadDeadline(time.Now().Add(30 * time.Minute))
	request := make([]byte, 12800)

	for {
		//get consumer id
		requestLen, _ := conn.Read(request)
		if requestLen == 0 {
			return
		}
		// msg := string(request)
		fmt.Println("received:")
		b := bytes.NewBuffer(request)
		r, err := zlib.NewReader(b)
		if err != nil {
			panic(err)
		}

		io.Copy(os.Stdout, r)
		// result,err := ioutil.ReadAll(r)
		// lib.CheckError(err)
		// _,err = logFile.Write(result)
		// lib.CheckError(err)
		r.Close()

		// c <- msg
		conn.Write([]byte("ok"))
		// conn.Close()
	}
}
Beispiel #11
0
//goroutine
func (s *Sender) Start() {
	// conn := s.getConnection()
	//初始化fileCacheList
	once.Do(s.reloadFileCache)

	//收尾工作
	defer func() {
		if err := recover(); err != nil {
			loglib.Error(fmt.Sprintf("sender %d panic:%v", s.id, err))
		}

		s.saveBufferInChan()

		//s.saveMemCache()

		s.connection.close()

		s.wq.AllDone()

	}()

	go s.pickPacks()
	//var connLost = 0
	var quit = false
	go lib.HandleQuitSignal(func() {
		quit = true
		s.connection.close()
	})

	var sendInterval = time.Duration(2000) //间隔稍大,避免发送文件缓存时因无连接或其他错误进入死循环

	var timeoutChan = time.After(sendInterval * time.Millisecond)
	for !quit {

		select {
		case b := <-s.memBuffer:
			//send b
			result := s.sendBuffer(b)
			if result == false {
				//改为直接放入文件缓存
				s.writeToFile(b)
			}

		case <-timeoutChan:
			timeoutChan = time.After(sendInterval * time.Millisecond)

			// send from file
			e := fileList.Remove()
			if e != nil { // file list is not empty
				filename := e.Value.(string)
				// fmt.Println("sender ",s.id,": get file :",filename)
				data, err := ioutil.ReadFile(filename)
				if err != nil {
					// fmt.Println("sender ",s.id,":",err)
					if _, ok := err.(*os.PathError); !ok {
						fileList.PushBack(filename)
					}
					loglib.Error(fmt.Sprintf("sender%d read file cache %s error:%s", s.id, filename, err.Error()))
				} else {

					packId := tcp_pack.GetPackId(data)                                                                          //debug info
					loglib.Info(fmt.Sprintf("sender%d read pack %s from file: %s, len: %d", s.id, packId, filename, len(data))) //debug info
					result := s.sendData2(data)
					if result == true {
						// s.fileCacheList.Remove(front)
						// log.Println("sender ",s.id,":removed file:",filename, "for pack", packId)//debug info
						err = os.Remove(filename)
						lib.CheckError(err)
						timeoutChan = time.After(time.Millisecond) //发送成功,不用再等待
					} else {
						fileList.PushBack(filename)
						// fmt.Println("sender ",s.id,": pushback file :",filename)
					}
				}
			}

		}
	}

}