예제 #1
0
파일: server-saver.go 프로젝트: vvf/gotest
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)
}