Skip to content

Commit 2034727

Browse files
pvgupta24mahim23
authored andcommitted
Implement gRPC server for evaluator
1 parent d115f40 commit 2034727

File tree

3 files changed

+111
-42
lines changed

3 files changed

+111
-42
lines changed

evaluator.go

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,50 @@
1-
// Get Results
2-
// Check output and send back results
3-
// Queue
4-
51
package main
62

73
import (
84
"io/ioutil"
95
"log"
106
"os"
7+
8+
"github.com/udhos/equalfile"
119
// "io"
1210
// "bytes"
13-
"github.com/udhos/equalfile"
1411
)
1512

16-
func EvaluateSubmission(submissionPath string, expectedOutputPath string) int32 {
13+
// EvaluateSubmission : Evaluates the submission
14+
func EvaluateSubmission(submissionID string, questionID string) int32 {
15+
// Path refers to path inside the cpjudge_webserver container because
16+
// directory is already mounted.
17+
base := "/media/vaibhav/Coding/go/src/github.com/cpjudge/cpjudge_webserver"
18+
submissionsPath := base + "/submissions/" + submissionID
19+
expectedOutputPath := base + "/questions/testcases/" +
20+
questionID +
21+
"/output/"
1722

1823
expectedOutputFiles, err := ioutil.ReadDir(expectedOutputPath)
1924
if err != nil {
2025
log.Fatal(err)
2126
}
2227

2328
for _, f := range expectedOutputFiles {
24-
errorFilePath := submissionPath + "error/" + f.Name()
25-
outputFilePath := submissionPath + "output/" + f.Name()
29+
errorFilePath := submissionsPath + "/error/" + f.Name()
30+
compilationErrorFilePath := submissionsPath + "/compilation_error.err"
31+
outputFilePath := submissionsPath + "/output/" + f.Name()
2632
expectedOutputFilePath := expectedOutputPath + f.Name()
2733

28-
if fileInfo, _ := os.Stat(errorFilePath); fileInfo.Size() != 0 {
29-
return 4 //Runtime Error
34+
if fileInfo, err := os.Stat(compilationErrorFilePath); err == nil {
35+
if fileInfo.Size() != 0 {
36+
return 3 //Compilation Error
37+
}
38+
}
39+
if fileInfo, err := os.Stat(errorFilePath); err == nil {
40+
if fileInfo.Size() != 0 {
41+
return 4 //Runtime Error
42+
}
3043
}
3144
cmp := equalfile.New(nil, equalfile.Options{Debug: false})
3245
if equal, _ := cmp.CompareFile(outputFilePath, expectedOutputFilePath); !equal {
3346
return 1 // Wrong Answer
3447
}
35-
// if !deepCompare(outputFilePath, expectedOutputFilePath) {
36-
// return 1
37-
// }
3848
}
39-
40-
log.Println("CORRECT")
4149
return 0 // Correct Answer
4250
}

sandbox_client.go

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,19 @@ import (
77
"time"
88

99
pb "github.com/cpjudge/proto/submission"
10+
1011
"google.golang.org/grpc"
1112
"google.golang.org/grpc/credentials"
1213
"google.golang.org/grpc/testdata"
1314
)
1415

1516
var (
16-
tls = flag.Bool("tls", false, "Connection uses TLS if true, else plain TCP")
17+
address = "172.17.0.1:10000"
1718
caFile = flag.String("ca_file", "", "The file containning the CA root cert file")
18-
serverAddr = flag.String("server_addr", "127.0.0.1:10000", "The server address in the format of host:port")
1919
serverHostOverride = flag.String("server_host_override", "x.test.youtube.com", "The server name use to verify the hostname returned by TLS handshake")
2020
)
2121

22-
// Submits the code to sandbox service and gets submission status
22+
// Internal function to call the sandbox service and submit the code.
2323
func submitCode(client pb.SandboxClient, submission *pb.Submission) *pb.CodeStatus {
2424
log.Printf("Submitting code for submission_id: %s", submission.GetSubmissionId())
2525
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
@@ -28,22 +28,13 @@ func submitCode(client pb.SandboxClient, submission *pb.Submission) *pb.CodeStat
2828
if err != nil {
2929
log.Fatalf("%v.SubmitCode(_) = _, %v: ", client, err)
3030
}
31-
log.Println(codeStatus)
32-
33-
expectedOutputPath := "/media/pvgupta24/MyZone/Projects/go/src/github.com/cpjudge/testcases/1/output/"
34-
35-
if codeStatus.CodeStatus == pb.SubmissionStatus_TO_BE_EVALUATED {
36-
codeStatus.CodeStatus = pb.SubmissionStatus(
37-
EvaluateSubmission(submission.SubmissionPath, expectedOutputPath))
38-
}
39-
40-
log.Println(codeStatus)
41-
31+
log.Println("After sandbox execution: ", codeStatus)
4232
return codeStatus
4333
}
4434

45-
46-
func main() {
35+
// SubmitCode : Spawns a connection to sandbox service and calls submitCode()
36+
func SubmitCode(submission *pb.Submission) *pb.CodeStatus {
37+
// Establish a connection with the sandbox service.
4738
flag.Parse()
4839
var opts []grpc.DialOption
4940
if *tls {
@@ -58,20 +49,13 @@ func main() {
5849
} else {
5950
opts = append(opts, grpc.WithInsecure())
6051
}
61-
conn, err := grpc.Dial(*serverAddr, opts...)
52+
conn, err := grpc.Dial(address, opts...)
6253
if err != nil {
6354
log.Fatalf("fail to dial: %v", err)
6455
}
6556
defer conn.Close()
6657
client := pb.NewSandboxClient(conn)
67-
68-
69-
submitCode(client, &pb.Submission{
70-
Language: "cpp",
71-
QuestionId: "1",
72-
SubmissionId: "1",
73-
SubmissionPath: "/media/pvgupta24/MyZone/Projects/go/src/github.com/cpjudge/submissions/1/",
74-
TestcasesPath: "/media/pvgupta24/MyZone/Projects/go/src/github.com/cpjudge/testcases/1/input/",
75-
UserId: "mahim23",
76-
})
58+
// Call internal function to submit code to sandbox service
59+
codeStatus := submitCode(client, submission)
60+
return codeStatus
7761
}

server.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"flag"
6+
"log"
7+
"net"
8+
9+
epb "github.com/cpjudge/proto/evaluator"
10+
spb "github.com/cpjudge/proto/submission"
11+
"google.golang.org/grpc"
12+
"google.golang.org/grpc/credentials"
13+
"google.golang.org/grpc/testdata"
14+
)
15+
16+
var (
17+
tls = flag.Bool("tls", false, "Connection uses TLS if true, else plain TCP")
18+
certFile = flag.String("cert_file", "", "The TLS cert file")
19+
keyFile = flag.String("key_file", "", "The TLS key file")
20+
jsonDBFile = flag.String("json_db_file", "", "A json file containing a list of features")
21+
serverAddr = flag.String("server_addr", "172.17.0.1:12000", "The server address in the format of host:port")
22+
)
23+
24+
type evaluatorServer struct{}
25+
26+
func (e *evaluatorServer) EvaluateCode(ctx context.Context, submission *spb.Submission) (*epb.CodeStatus, error) {
27+
codeStatus := SubmitCode(submission)
28+
evaluateCodeStatus := &epb.CodeStatus{}
29+
switch codeStatus.CodeStatus {
30+
case spb.SubmissionStatus_TO_BE_EVALUATED:
31+
evaluateStatus := EvaluateSubmission(submission.SubmissionId, submission.QuestionId)
32+
switch evaluateStatus {
33+
case 0:
34+
evaluateCodeStatus.CodeStatus = epb.EvaluationStatus_CORRECT_ANSWER
35+
case 2:
36+
evaluateCodeStatus.CodeStatus = epb.EvaluationStatus_TIME_LIMIT_EXCEEDED
37+
case 3:
38+
evaluateCodeStatus.CodeStatus = epb.EvaluationStatus_COMPILATION_ERROR
39+
case 4:
40+
evaluateCodeStatus.CodeStatus = epb.EvaluationStatus_RUNTIME_ERROR
41+
default:
42+
evaluateCodeStatus.CodeStatus = epb.EvaluationStatus_WRONG_ANSWER
43+
}
44+
case spb.SubmissionStatus_TIME_LIMIT_EXCEEDED:
45+
evaluateCodeStatus.CodeStatus = epb.EvaluationStatus_TIME_LIMIT_EXCEEDED
46+
case spb.SubmissionStatus_COMPILATION_ERROR:
47+
evaluateCodeStatus.CodeStatus = epb.EvaluationStatus_COMPILATION_ERROR
48+
default:
49+
evaluateCodeStatus.CodeStatus = epb.EvaluationStatus_WRONG_ANSWER
50+
}
51+
return evaluateCodeStatus, nil
52+
}
53+
54+
func main() {
55+
flag.Parse()
56+
lis, err := net.Listen("tcp", *serverAddr)
57+
if err != nil {
58+
log.Fatalf("failed to listen: %v", err)
59+
}
60+
var opts []grpc.ServerOption
61+
if *tls {
62+
if *certFile == "" {
63+
*certFile = testdata.Path("server1.pem")
64+
}
65+
if *keyFile == "" {
66+
*keyFile = testdata.Path("server1.key")
67+
}
68+
creds, err := credentials.NewServerTLSFromFile(*certFile, *keyFile)
69+
if err != nil {
70+
log.Fatalf("Failed to generate credentials %v", err)
71+
}
72+
opts = []grpc.ServerOption{grpc.Creds(creds)}
73+
}
74+
grpcServer := grpc.NewServer(opts...)
75+
epb.RegisterEvaluatorServer(grpcServer, &evaluatorServer{})
76+
grpcServer.Serve(lis)
77+
}

0 commit comments

Comments
 (0)