Esempio n. 1
0
func main() {
	log.SetFlags(log.Flags() | log.Lshortfile)

	var clientPairId string
	meetAddr := "localhost:8080"

	flag.StringVar(&meetAddr, "meetAddr", meetAddr, "Server for discover other")
	flag.StringVar(&clientPairId, "pair", "", "key to macth service")
	flag.Parse()

	log.Printf("Discover server is %s\n", meetAddr)
	log.Printf("unique ID for the client pair: %s\n", clientPairId)
	if len(clientPairId) == 0 {
		log.Fatal("Must config the pair")
	}

	firstConn, err := net.Dial("tcp", meetAddr)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Fprintf(firstConn, "GET /meet/"+clientPairId+" HTTP/1.0\r\n\r\n")
	firstConn.Close()

	// Block to let the OS free the port.
	time.Sleep(5 * time.Second)

	log.Println("firstConn.LocalAddr", firstConn.LocalAddr().String())
	ln, err := net.Listen("tcp", firstConn.LocalAddr().String())
	if err != nil {
		log.Println("Could not open listening socket! " + err.Error())
		return
	}

	log.Println("Listening on address " + firstConn.LocalAddr().String())

	for {
		conn, err := ln.Accept()
		if err != nil {
			fmt.Println(err.Error())
			continue
		}
		go handleConnection(conn)
	}
}
Esempio n. 2
0
File: cmd.go Progetto: cryks/server
func (cmd commandSize) Execute(conn *Conn, param string) {
	path := conn.buildPath(param)
	stat, err := conn.driver.Stat(path)
	if err != nil {
		log.Printf("Size: error(%s)", err)
		conn.writeMessage(450, fmt.Sprintln("path", path, "not found"))
	} else {
		conn.writeMessage(213, strconv.Itoa(int(stat.Size())))
	}
}
Esempio n. 3
0
func Test_JsonMap(t *testing.T) {
	data := make(map[string]interface{})
	data["Item"] = "abc"
	data["Amount"] = 123

	myData := &MyData{}
	Map2Struct(data, myData)
	ddd := Struct2Map(myData)
	assert.Equal(t, data, ddd)
	log.Printf("m: %v\n", ddd)
}
Esempio n. 4
0
func main() {
	log.SetFlags(log.Flags() | log.Lshortfile)

	meetAddrFmt := "http://%s/meet/"
	meetAddr := "localhost:8080"
	var clientPairId string

	flag.StringVar(&meetAddr, "meetAddr", meetAddr, "Server for discover other")
	flag.StringVar(&clientPairId, "pair", "", "key to macth service")
	flag.Parse()

	meetAddr = fmt.Sprintf(meetAddrFmt, meetAddr)

	log.Printf("Discover server is %s\n", meetAddr)
	log.Printf("unique ID for the client pair: %s\n", clientPairId)
	if len(clientPairId) == 0 {
		log.Fatal("Must config the pair")
	}

	response, err := http.Get(meetAddr + clientPairId)
	if err != nil {
		log.Fatal(err)
	}
	address, err := ioutil.ReadAll(response.Body)
	if err != nil {
		log.Fatal(err)
	}

	log.Printf("Dial %s\n", string(address))
	conn, err := net.Dial("tcp", string(address))
	if err != nil {
		log.Fatal(err)
	}

	go readSocket(conn)
	writeSocket(conn)
}