Example #1
0
func main() {
	if len(os.Args) < 3 {
		fmt.Fprintf(os.Stderr, "Usage: %s ip port\n", os.Args[0])
		os.Exit(1)
	}
	ip, port_str := os.Args[1], os.Args[2]

	port, err := strconv.Atoi(port_str)

	l := net_util.NewListener(ip, port, "tcp", 1024)

	err = l.Listen()
	CheckError(err)

	var gDispatcher = dispatcher.Instance()
	gDispatcher.Add("select", Select)
	gDispatcher.Add("update", Update)
	gDispatcher.Add("insert", Insert)
	gDispatcher.Add("delete", Delete)

	err = InitDB(&dao)
	CheckError(err)

	l.Run(Handle)
	dao.Finalize()

	os.Exit(0)
}
Example #2
0
func Handle(buff []byte) error {

	//fmt.Println(buff)
	test := example.Test{}
	e := proto.Unmarshal(buff, &test)
	if e != nil {
		fmt.Printf("decode failed,due to:%s\n", e.Error())
		return e
	}

	d := dispatcher.Instance()
	f := d.Select(test.GetHead().GetCmd())

	if f == nil {
		fmt.Printf("invalid cmd %s\n", test.GetHead().GetCmd())
	} else {
		_, e = f(*test.GetPara())
		CheckError(e)
	}

	//fmt.Printf("%s %d %s\n",test.GetCmd(),test.GetId(),test.GetName())
	//fmt.Println(test)
	return nil
}