Example #1
0
func main() {
	str0 := "monkey"
	C.CreateDB((*C.char)(convert.String2C(str0))) //创建基础数据库
	str0 = "config"                               //配置数据库
	C.CreateDB((*C.char)(convert.String2C(str0)))
	key := "passwd" //初始密码
	data := "monkey"
	db := C.SwitchDB((*C.char)(convert.String2C(str0)))
	C.Set(&(*db).tIndex, (*C.char)(convert.String2C(key)), (convert.String2C(data)))
	servicePort := ":1517"
	tcpAddr, err := net.ResolveTCPAddr("tcp4", servicePort)
	if err != nil {
		panic(err)
	}
	l, err := net.ListenTCP("tcp", tcpAddr) //侦听TCP
	if err != nil {
		panic(err)
	}
	for {
		conn, err := l.AcceptTCP()
		conn.SetKeepAlive(true)
		conn.SetNoDelay(true)
		if err != nil {
			panic(err)
		}
		go Handler(conn)
	}
}
Example #2
0
func initDB() { //初始化数据库
	str0 := "monkey"
	C.CreateDB((*C.char)(convert.String2C(str0))) //创建基础数据库
	str0 = "config"                               //配置数据库
	C.CreateDB((*C.char)(convert.String2C(str0)))
	key := "passwd" //初始密码
	data := "monkey"
	db := C.SwitchDB((*C.char)(convert.String2C(str0)))
	C.Set(&(db.tIndex), (*C.char)(convert.String2C(key)), (convert.String2C(data)))

}
Example #3
0
func (cmd command) Createdb(db **C.Database) []byte {
	response := []byte{}
	key, _ := convert.ParseUntil(cmd, 0, 9)
	d := C.CreateDB((*C.char)(convert.Bytes2C(key)))
	if d != nil {
		*db = d
		response = []byte("Already exist,switched\n")
	} else {
		response = []byte("Created\n")
	}
	return response
}
Example #4
0
////////////////////////////////////////Dumplated//////////////////////////////////////////////////////////////////////////
func TranslateMessage(s *tcp.TCPSession, db **C.Database, message []byte) {
	command := string(message)
	params := strings.Split(command, " ")
	//fmt.Println(params)
	response := []byte{}
	if params[0] == "set" {
		r := C.Set(&(*db).tIndex, (*C.char)(convert.String2C(params[1])), (convert.String2C(params[2])))
		for i := 0; ; i++ {
			response = append(response, byte(r.msg[i]))
			if response[i] == 0 {
				break
			}
		}
	} else if params[0] == "get" {
		r := C.Get(&(*db).tIndex, (*C.char)(convert.String2C(params[1])))
		// for i := 0;;i++ {
		// 	response = append(response,byte(r.msg[i]))
		// 	if response[i] == 0 { break; }
		// }
		if int(r.code) == 0 {
			for i := 0; ; i++ {
				response = append(response, byte(*(*C.char)(unsafe.Pointer((uintptr(r.pData) + uintptr(i))))))
				if response[i] == 0 {
					break
				}
			}
		} else {
			// for i := 0;;i++ {
			// response = append(response,byte(r.msg[i]))
			// if response[i] == 0 { break; }
			// }
		}

	} else if params[0] == "delete" || params[0] == "remove" {
		r := C.Delete(&(*db).tIndex, (*C.char)(convert.String2C(params[1])))
		for i := 0; ; i++ {
			response = append(response, byte(r.msg[i]))
			if response[i] == 0 {
				break
			}
		}

	} else if params[0] == "createdb" {
		d := C.CreateDB((*C.char)(convert.String2C(params[1])))
		if d != nil {
			*db = d
			response = []byte("Already exist,switched\n")
		} else {
			response = []byte("Created\n")
		}
	} else if params[0] == "switchdb" {
		d := C.SwitchDB((*C.char)(convert.String2C(params[1])))
		if d != nil {
			*db = d
			response = []byte("ok\n")
		} else {
			response = []byte("fail\n")
		}
	} else if params[0] == "dropdb" {
		*db = C.DropDB((*C.char)(convert.String2C(params[1])))
	} else if strings.EqualFold("listdb", params[0]) {
		r := C.ListDB()
		for i := 0; i < 1024; i++ {
			b := byte(*(*C.char)(unsafe.Pointer(uintptr(unsafe.Pointer(r)) + uintptr(i))))
			response = append(response, b)
			if b == 0 {
				break
			}
		}
		C.free(unsafe.Pointer(r))
	} else {
		//fmt.Println("unkown command:",params[0])
	}
	s.SendMessage(response)
}