-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain.go
139 lines (113 loc) · 2.85 KB
/
main.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// RunServer is Judger and dispatcher for ZJGSU New OJ http://acm.zjgsu.edu.cn
// Copyright (C) 2013-2014 - ZJGSU OSC[https://github.com/ZJGSU-Open-Source/]
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
package main
import (
"vjudger"
"container/list"
"encoding/json"
"io"
"log"
"net/http"
"os"
"sync"
"time"
)
var (
logger *log.Logger
waittingQueue *list.List
SyncControll *Sync
runPath string
dataPath string
)
func init() {
runPath = os.Getenv("RUN_PATH")
dataPath = os.Getenv("DATA_PATH")
logger = log.New(os.Stdout, "", log.Lshortfile|log.Ltime|log.Ldate)
waittingQueue = list.New()
log.Println(runPath)
}
func main() {
SyncControll = &Sync{}
go JudgeForever()
http.HandleFunc("/", Handler)
http.ListenAndServe(":8888", nil)
}
func Handler(w http.ResponseWriter, r *http.Request) {
var info = &Info{}
err := LoadJson(r.Body, info)
if err != nil {
log.Println("load json err")
return
}
logger.Printf("Sid %v, Rejudge %v\n", info.Sid, info.Rejudge)
SyncControll.AddQueue(info)
}
func LoadJson(r io.Reader, v interface{}) (err error) {
err = json.NewDecoder(r).Decode(v)
return
}
//Info 判题必须信息
type Info struct {
Sid int
Pid int
OJ string
Rejudge bool
}
// Sync 锁
type Sync struct {
lock sync.Mutex
}
// 添加到队尾
func (s *Sync) AddQueue(info *Info) {
s.lock.Lock()
defer s.lock.Unlock()
waittingQueue.PushBack(info)
}
// 获得并删除队头
func (s *Sync) GetFrontAndRemove() (info *Info) {
s.lock.Lock()
defer s.lock.Unlock()
info, _ = waittingQueue.Front().Value.(*Info)
waittingQueue.Remove(waittingQueue.Front())
return
}
// 队列是否为空
func (s *Sync) IsEmpty() bool {
s.lock.Lock()
defer s.lock.Unlock()
return waittingQueue.Len() == 0
}
//
func JudgeForever() {
for {
if SyncControll.IsEmpty() == false {
info := SyncControll.GetFrontAndRemove()
go Judge(*info) //并行判题
}
time.Sleep(1 * time.Second)
}
}
var VJs = []vjudger.Vjudger{&ZJGSUJudger{}, &vjudger.HDUJudger{}, &vjudger.PKUJudger{}}
func Judge(info Info) {
user := &solution{}
user.Init(info)
for _, vj := range VJs {
if vj.Match(user.GetOJ()) {
vj.Run(user)
break
}
}
user.UpdateSim()
user.UpdateRecord()
}