Example #1
0
func AddCommandList() {
	kmgConsole.AddAction(kmgConsole.Command{
		Name:   "FileHttpServer",
		Runner: runFileHttpServer,
	})
	kmgConsole.AddCommandWithName("HttpGet", func() {
		requestUrl := ""
		key := ""
		flag.StringVar(&requestUrl, "url", "", "")
		flag.StringVar(&key, "key", "", "crypto key use to decrypt respond")
		flag.Parse()
		if requestUrl == "" {
			kmgConsole.ExitOnErr(errors.New("Usage: kmg HttpGet -url http://xxx"))
		}
		b := MustUrlGetContent(requestUrl)
		var err error
		if key != "" {
			b, err = kmgCrypto.CompressAndEncryptBytesDecodeV2(kmgCrypto.Get32PskFromString(key), b)
			if err != nil {
				panic(err)
			}
		}
		fmt.Print(string(b))
	})
}
Example #2
0
func TestGenerated(ot *testing.T) {
	psk := kmgCrypto.Get32PskFromString("abc")
	closer := ListenAndServe_Demo(":34895", &Demo{}, psk)
	defer closer()
	client := NewClient_Demo("http://127.0.0.1:34895/f", psk)
	info, err := client.PostScoreInt("LbId", 1)
	kmgTest.Equal(err, nil)
	kmgTest.Equal(info, "LbId")

	info, err = client.PostScoreInt("LbId", 2)
	kmgTest.Equal(info, "")
	kmgTest.Ok(err != nil, err)
	kmgTest.Equal(err.Error(), "Score!=1")

	info, err = client.DemoFunc8(DemoRequest{}, &DemoRequest{}, 1)
	kmgTest.Equal(info, "info1")

	info, err = client.DemoFunc8(DemoRequest{}, &DemoRequest{}, 2)
	kmgTest.Equal(info, "info")

	inT := kmgTime.MustParseAutoInDefault("2001-01-01 01:01:01")
	outT, err := client.DemoTime(inT)
	kmgTest.Equal(err, nil)
	kmgTest.Ok(outT.Equal(kmgTime.MustParseAutoInDefault("2001-01-01T02:01:01.001000001+08:00")), outT)

	ip, err := client.DemoClientIp()
	kmgTest.Equal(err, nil)
	kmgTest.Equal(ip, "127.0.0.1")
}
Example #3
0
func TestJava(ot *testing.T) {
	closer := testPackage.ListenAndServe_Demo(":34895", &testPackage.Demo{}, kmgCrypto.Get32PskFromString("abc psk"))
	defer closer()
	os.Chdir(filepath.Join("java", "src"))
	MustGenerateCode(&GenerateRequest{
		ObjectPkgPath:   "github.com/bronze1man/kmg/kmgRpc/testPackage",
		ObjectName:      "Demo",
		ObjectIsPointer: true,
		OutFilePath:     "testPackage/RpcDemo.java",
		OutPackageName:  "testPackage",
		OutClassName:    "RpcDemo",
	})
	kmgCmd.MustRun("javac -sourcepath . ./testPackage/Main.java")
	out := kmgCmd.MustRunAndReturnOutput("java -classpath . testPackage.Main")
	kmgTest.Equal(out, []byte("Success\n"))
}
Example #4
0
func BenchmarkGenerated(ot *testing.B) {
	psk := kmgCrypto.Get32PskFromString("abcd")
	closer := ListenAndServe_Demo(":34896", &Demo{}, psk)
	defer func() {
		closer()
		time.Sleep(10 * time.Millisecond)
	}()
	client := NewClient_Demo("http://127.0.0.1:34896/f", psk)
	ot.ResetTimer()
	for i := 0; i < ot.N; i++ {
		info, err := client.PostScoreInt("LbId", 1)
		kmgTest.Equal(err, nil)
		kmgTest.Equal(info, "LbId")
	}
	ot.StopTimer()
	// 结果 770 qps BenchmarkGenerated	    1000	   1298511 ns/op
}
Example #5
0
File: rpc.go Project: keysonZZZ/kmg
	"github.com/bronze1man/kmg/kmgCrypto"
	"github.com/bronze1man/kmg/kmgErr"
	"github.com/bronze1man/kmg/kmgProcessMutex"
	"time"
)

type StartStatus string

const (
	StartStatusSuccess StartStatus = "StartStatusSuccess"
	StartStatusFail                = "StartStatusFail"
)

var statusChannel = make(chan StartStatus)

var rpcPsk = kmgCrypto.Get32PskFromString("w4n4ts28cq")

var rpcAddress = "127.0.0.1:2777"

type ServiceRpc struct{}

func (sr *ServiceRpc) Send(status StartStatus) {
	statusChannel <- status
}

//TODO 对于不会发 RPC 的进程,应该可以用一个 flag 来表明不需要等待 RPC 响应
func waitRpcRespond() chan error {
	returnChan := make(chan error)
	lock := &kmgProcessMutex.FileMutex{Name: "kmg_service_lock"}
	lock.Lock()
	rpcCloser := ListenAndServe_ServiceRpc(rpcAddress, &ServiceRpc{}, rpcPsk)
Example #6
0
func runFileHttpServer() {
	listenAddr := ""
	path := ""
	key := ""
	flag.StringVar(&listenAddr, "l", ":80", "listen address")
	flag.StringVar(&path, "path", "", "root path of the file server")
	flag.StringVar(&key, "key", "", "crypto key use to encrypt all request of this server")
	flag.Parse()
	var err error
	if path == "" {
		path, err = os.Getwd()
		if err != nil {
			fmt.Printf("os.Getwd() fail %s", err)
			return
		}
	} else {
		kmgErr.PanicIfError(os.Chdir(path))
	}
	if key == "" {
		http.Handle("/", http.FileServer(http.Dir(path)))
	}
	if key != "" {
		http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
			realPath := filepath.Join(path, r.URL.Path)
			if !kmgFile.MustFileExist(realPath) {
				w.Write([]byte("File Not Exist"))
				return
			}
			if !kmgStrings.IsInSlice(cacheFilePathSlice, realPath) {
				cacheFilePathSlice = append(cacheFilePathSlice, realPath)
			}
			updateCache := func() {
				cacheFilePathEncryptMap[realPath] = kmgCrypto.CompressAndEncryptBytesEncodeV2(
					kmgCrypto.Get32PskFromString(key),
					kmgFile.MustReadFile(realPath),
				)
			}
			checkCache := func() {
				lock.Lock()
				defer lock.Unlock()
				kmgCache.MustMd5FileChangeCache(realPath, []string{realPath}, func() {
					updateCache()
				})
			}
			checkCache()
			//进程重启后,内存中的缓存掉了,但是文件系统的缓存还在
			_, exist := cacheFilePathEncryptMap[realPath]
			if !exist {
				updateCache()
			}
			w.Write(cacheFilePathEncryptMap[realPath])
		})
	}
	fmt.Println("start server at", listenAddr)
	err = http.ListenAndServe(listenAddr, nil)
	if err != nil {
		fmt.Printf("http.ListenAndServe() fail %s", err)
		return
	}
	return
}