示例#1
0
func main() {
	results = make(chan *common.FileStat)
	commonStatMutex = make(chan int, 1)
	commonStat = new(common.FileStat)
	commonStat.Filename = "all"
	// Listen on TCP port 2000 on all interfaces.
	mongoSession, err := mgo.DialWithInfo(&mgo.DialInfo{
		Addrs: []string{"localhost"}, Database: "gotest", Username: "******", Password: "******"})

	if err != nil {
		log.Fatalf("CreateSession: %s\n", err)
	}
	mongoSession.SetMode(mgo.Monotonic, true)
	log.Println("Start listen tcp on 8911")
	l, err := net.Listen("tcp", ":8911")
	if err != nil {
		log.Fatal(err)
	}
	defer l.Close()
	for {
		// Wait for a connection.
		conn, err := l.Accept()
		if err != nil {
			log.Fatal(err)
		}
		// Handle the connection in a new goroutine.
		// The loop then returns to accepting, so that
		// multiple connections may be served concurrently.
		go func(c net.Conn) {
			// Echo all incoming data.
			defer c.Close()
			info := new(common.FileStat)
			if err := info.Read(c); err != nil {
				log.Println(err)
				return
			}
			//			json_to_save := to_json(info)
			item_to_save := to_bson(info)
			commonStatMutex <- 1 // It will block next concurrent call
			conn.Write(commonStat.Bytes())
			<-commonStatMutex
			//			fmt.Println(json_to_save)
			//				client.Set('a', dat)
			// Shut down the connection.
			go func() {
				sessionCopy := mongoSession.Copy()
				defer sessionCopy.Close()
				// save received data to mongo sessionCopy
				collection := sessionCopy.DB("gotest").C("filestat")
				collection.Insert(item_to_save)
			}()

		}(conn)
	}
}
示例#2
0
文件: project.go 项目: vvf/gotest
func send_packet(packet []byte) {
	defer wg.Done()
	conn, err := net.Dial("tcp", "127.0.0.1:8911")
	if err != nil {
		fmt.Println("Error:")
		fmt.Println(err)
		return
	}
	defer conn.Close()
	fmt.Println("Send data ", len(packet), " bytes")
	conn.Write(packet)
	common_stat := new(common.FileStat)
	common_stat.Read(conn)
	fmt.Println("Common stat is: ", common_stat.Filename)
	fmt.Println(common_stat.Stat)
}
示例#3
0
文件: project.go 项目: vvf/gotest
func read_file_stat(file string) {
	//	arr := make([]int16,256,256)
	defer wg.Done()
	fmt.Printf("Start calc '%s'\n", file)
	fs := new(common.FileStat)
	fs.Filename = file
	fh, err := os.Open(file)
	if err != nil {
		return
	}
	defer fh.Close()
	stat, err := fh.Stat()
	if err != nil {
		return
	}
	// read the file
	// "eat" file by small part (not whole file if it is so big)
	file_size := stat.Size()
	var part_size int64 = BUFFER_SIZE
	for file_size > 0 {
		if file_size < part_size {
			file_size = part_size
		}
		bs := make([]byte, part_size)
		_, err = fh.Read(bs)
		if err != nil {
			return
		}
		// It can be go-routine but it will increase used memory size
		for _, b := range bs {
			fs.Inc(b)
		}
		file_size -= part_size
	}

	//    str := string(bs)
	//    fmt.Println(str)
	fmt.Println("Publish result ", file)
	results <- fs
}