thrift 使用方法

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
namespace java CodeRunService
namespace go   CodeRunService


//编译
struct CodeCompile {
  1: string codeType,
  2: string codeContent
}
//运行
struct CodeRun {
  1: string codeType,
  2: string exePath,
  3: string input,
  4: i32    inputType 
  // 5: string    outPut
}
//编译结果
struct CompileResult {
  1: i32 retCode,
  2: string outPut
}
//运行结果
struct RunResult {
  1:i32 retCode,
  2: string outPut 
  
}


struct CheckResult {
  1:i32 retCode,
  2: string tips,
  3: string cpuCost,
  4: string memoryUsed
}
service JudgerService  {

  //编译
  CompileResult doCompile(1: CodeCompile task)
  //运行
  RunResult doRun(1: CodeRun task)

  CheckResult doCheck(1: string exePath,2: string input)

}

生成代码

1
2
thrift -r --gen java demo.thrift
thrift -r --gen go demo.thrift

go语言调用

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package service

import (
	"context"
	"fmt"
	"lyrJudger/thrift/gen-go/CodeRunService"
	"testing"

	// "git.apache.org/thrift.git/lib/go/thrift"
	"log"
	"net"

	"github.com/apache/thrift/lib/go/thrift"
)

const (
	HOST = "localhost"
	PORT = "6860"
)

func Test_runservice(t *testing.T) {

	handler := NewJudgeService()
	processor := CodeRunService.NewJudgerServiceProcessor(handler)
	serverTransport, err := thrift.NewTServerSocket(HOST + ":" + PORT)
	if err != nil {
		fmt.Println("Error:", err)
	}
	transportFactory := thrift.NewTFramedTransportFactory(thrift.NewTTransportFactory())
	protocolFactory := thrift.NewTBinaryProtocolFactoryDefault()

	server := thrift.NewTSimpleServer4(processor, serverTransport, transportFactory, protocolFactory)
	fmt.Println("Running at:", HOST+":"+PORT)
	server.Serve()
	fmt.Println("结果是---")

}

func Test_run_client(t *testing.T) {
	// const (
	// 	HOST = "localhost"
	// 	PORT = "8080"
	// )
	tSocket, err := thrift.NewTSocket(net.JoinHostPort(HOST, PORT))
	if err != nil {
		log.Fatalln("tSocket error:", err)
	}
	transportFactory := thrift.NewTFramedTransportFactory(thrift.NewTTransportFactory())
	transport, _ := transportFactory.GetTransport(tSocket)
	protocolFactory := thrift.NewTBinaryProtocolFactoryDefault()

	client := CodeRunService.NewJudgerServiceClientFactory(transport, protocolFactory)

	if err := transport.Open(); err != nil {
		fmt.Println("Error opening:", HOST+":"+PORT)
	}
	defer transport.Close()

	// data := example.Data{Text: "hello,world!"}
	var ctx = context.Background()
	d, err := client.DoRun(ctx, CodeRunService.NewCodeRun())
	fmt.Println(" 得到结果 result === -> ", d, err)

}