-
Notifications
You must be signed in to change notification settings - Fork 657
/
server.go
247 lines (222 loc) · 6.05 KB
/
server.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package main
import (
"bytes"
"fmt"
"go/build"
"log"
"net"
"net/rpc"
"os"
"path/filepath"
"reflect"
"runtime"
"time"
)
func do_server() int {
g_config.read()
if g_config.ForceDebugOutput != "" {
// forcefully enable debugging and redirect logging into the
// specified file
*g_debug = true
f, err := os.Create(g_config.ForceDebugOutput)
if err != nil {
panic(err)
}
log.SetOutput(f)
}
addr := *g_addr
if *g_sock == "unix" {
addr = get_socket_filename()
if file_exists(addr) {
log.Printf("unix socket: '%s' already exists\n", addr)
return 1
}
}
g_daemon = new_daemon(*g_sock, addr)
if *g_sock == "unix" {
// cleanup unix socket file
defer os.Remove(addr)
}
rpc.Register(new(RPC))
g_daemon.loop()
return 0
}
//-------------------------------------------------------------------------
// daemon
//-------------------------------------------------------------------------
type daemon struct {
listener net.Listener
cmd_in chan int
autocomplete *auto_complete_context
pkgcache package_cache
declcache *decl_cache
context package_lookup_context
}
func new_daemon(network, address string) *daemon {
var err error
d := new(daemon)
d.listener, err = net.Listen(network, address)
if err != nil {
panic(err)
}
d.cmd_in = make(chan int, 1)
d.pkgcache = new_package_cache()
d.declcache = new_decl_cache(&d.context)
d.autocomplete = new_auto_complete_context(d.pkgcache, d.declcache)
return d
}
func (this *daemon) drop_cache() {
this.pkgcache = new_package_cache()
this.declcache = new_decl_cache(&this.context)
this.autocomplete = new_auto_complete_context(this.pkgcache, this.declcache)
}
const (
daemon_close = iota
)
func (this *daemon) loop() {
conn_in := make(chan net.Conn)
go func() {
for {
c, err := this.listener.Accept()
if err != nil {
panic(err)
}
conn_in <- c
}
}()
timeout := time.Duration(g_config.CloseTimeout) * time.Second
countdown := time.NewTimer(timeout)
for {
// handle connections or server CMDs (currently one CMD)
select {
case c := <-conn_in:
rpc.ServeConn(c)
countdown.Reset(timeout)
runtime.GC()
case cmd := <-this.cmd_in:
switch cmd {
case daemon_close:
return
}
case <-countdown.C:
return
}
}
}
func (this *daemon) close() {
this.cmd_in <- daemon_close
}
var g_daemon *daemon
//-------------------------------------------------------------------------
// server_* functions
//
// Corresponding client_* functions are autogenerated by goremote.
//-------------------------------------------------------------------------
func server_auto_complete(file []byte, filename string, cursor int, context_packed go_build_context) (c []candidate, d int) {
context := unpack_build_context(&context_packed)
defer func() {
if err := recover(); err != nil {
print_backtrace(err)
c = []candidate{
{"PANIC", "PANIC", decl_invalid, "panic"},
}
// drop cache
g_daemon.drop_cache()
}
}()
// TODO: Probably we don't care about comparing all the fields, checking GOROOT and GOPATH
// should be enough.
if !reflect.DeepEqual(g_daemon.context.Context, context.Context) {
g_daemon.context = context
g_daemon.drop_cache()
}
switch g_config.PackageLookupMode {
case "bzl":
// when package lookup mode is bzl, we set GOPATH to "" explicitly and
// BzlProjectRoot becomes valid (or empty)
var err error
g_daemon.context.GOPATH = ""
g_daemon.context.BzlProjectRoot, err = find_bzl_project_root(g_config.LibPath, filename)
if *g_debug && err != nil {
log.Printf("Bzl project root not found: %s", err)
}
case "gb":
// when package lookup mode is gb, we set GOPATH to "" explicitly and
// GBProjectRoot becomes valid (or empty)
var err error
g_daemon.context.GOPATH = ""
g_daemon.context.GBProjectRoot, err = find_gb_project_root(filename)
if *g_debug && err != nil {
log.Printf("Gb project root not found: %s", err)
}
case "go":
// get current package path for GO15VENDOREXPERIMENT hack
g_daemon.context.CurrentPackagePath = ""
pkg, err := g_daemon.context.ImportDir(filepath.Dir(filename), build.FindOnly)
if err == nil {
if *g_debug {
log.Printf("Go project path: %s", pkg.ImportPath)
}
g_daemon.context.CurrentPackagePath = pkg.ImportPath
} else if *g_debug {
log.Printf("Go project path not found: %s", err)
}
}
if *g_debug {
var buf bytes.Buffer
log.Printf("Got autocompletion request for '%s'\n", filename)
log.Printf("Cursor at: %d\n", cursor)
if cursor > len(file) || cursor < 0 {
log.Println("ERROR! Cursor is outside of the boundaries of the buffer, " +
"this is most likely a text editor plugin bug. Text editor is responsible " +
"for passing the correct cursor position to gocode.")
} else {
buf.WriteString("-------------------------------------------------------\n")
buf.Write(file[:cursor])
buf.WriteString("#")
buf.Write(file[cursor:])
log.Print(buf.String())
log.Println("-------------------------------------------------------")
}
}
candidates, d := g_daemon.autocomplete.apropos(file, filename, cursor)
if *g_debug {
log.Printf("Offset: %d\n", d)
log.Printf("Number of candidates found: %d\n", len(candidates))
log.Printf("Candidates are:\n")
for _, c := range candidates {
abbr := fmt.Sprintf("%s %s %s", c.Class, c.Name, c.Type)
if c.Class == decl_func {
abbr = fmt.Sprintf("%s %s%s", c.Class, c.Name, c.Type[len("func"):])
}
log.Printf(" %s\n", abbr)
}
log.Println("=======================================================")
}
return candidates, d
}
func server_close(notused int) int {
g_daemon.close()
return 0
}
func server_status(notused int) string {
return g_daemon.autocomplete.status()
}
func server_drop_cache(notused int) int {
// drop cache
g_daemon.drop_cache()
return 0
}
func server_set(key, value string) string {
if key == "\x00" {
return g_config.list()
} else if value == "\x00" {
return g_config.list_option(key)
}
// drop cache on settings changes
g_daemon.drop_cache()
return g_config.set_option(key, value)
}
func server_options(notused int) string {
return g_config.options()
}