Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 178 lines (153 sloc) 3.586 kb
a920047 @nsf Progress..
authored
1 package main
2
3 import (
4 "net"
94b7b01 @nsf More mess, change jsonrpc to simple rpc (uses gob).
authored
5 "rpc"
6 "os/signal"
e88dab2 @nsf Implement failsafe service.
authored
7 "fmt"
8 "runtime"
6379b17 @nsf Implement basics of a client and a server using jsonrpc.
authored
9 )
10
11 //-------------------------------------------------------------------------
12
94b7b01 @nsf More mess, change jsonrpc to simple rpc (uses gob).
authored
13 type AutoCompletionDaemon struct {
6379b17 @nsf Implement basics of a client and a server using jsonrpc.
authored
14 acr *ACRServer
a5ad476 @nsf Add semanticcontext.go, a first step towards refactoring functionality.
authored
15 acc *AutoCompleteContext
16 semantic *SemanticContext
17 mcache MCache
18 declcache *DeclCache
6379b17 @nsf Implement basics of a client and a server using jsonrpc.
authored
19 }
20
94b7b01 @nsf More mess, change jsonrpc to simple rpc (uses gob).
authored
21 func NewAutoCompletionDaemon(path string) *AutoCompletionDaemon {
22 self := new(AutoCompletionDaemon)
23 self.acr = NewACRServer(path)
a5ad476 @nsf Add semanticcontext.go, a first step towards refactoring functionality.
authored
24 self.mcache = NewMCache()
25 self.declcache = NewDeclCache()
26 self.acc = NewAutoCompleteContext(self.mcache, self.declcache)
27 self.semantic = NewSemanticContext(self.mcache, self.declcache)
94b7b01 @nsf More mess, change jsonrpc to simple rpc (uses gob).
authored
28 return self
6379b17 @nsf Implement basics of a client and a server using jsonrpc.
authored
29 }
30
a5ad476 @nsf Add semanticcontext.go, a first step towards refactoring functionality.
authored
31 func (self *AutoCompletionDaemon) DropCache() {
32 self.mcache = NewMCache()
33 self.declcache = NewDeclCache()
34 self.acc = NewAutoCompleteContext(self.mcache, self.declcache)
35 self.semantic = NewSemanticContext(self.mcache, self.declcache)
36 }
37
94b7b01 @nsf More mess, change jsonrpc to simple rpc (uses gob).
authored
38 var daemon *AutoCompletionDaemon
39
40 //-------------------------------------------------------------------------
6379b17 @nsf Implement basics of a client and a server using jsonrpc.
authored
41
e88dab2 @nsf Implement failsafe service.
authored
42 func printBacktrace() {
43 i := 2
44 for {
45 pc, file, line, ok := runtime.Caller(i)
46 if !ok {
47 break
48 }
49 f := runtime.FuncForPC(pc)
50 fmt.Printf("%d(%s): %s:%d\n", i-1, f.Name(), file, line)
51 i++
52 }
53 }
54
bd72d4b @nsf Mutlifile packages support, few misc fixes.
authored
55 func Server_AutoComplete(file []byte, filename string, cursor int) (a, b, c []string, d int) {
e88dab2 @nsf Implement failsafe service.
authored
56 defer func() {
57 if err := recover(); err != nil {
58 fmt.Printf("GOT PANIC!!!:\n")
59 fmt.Println(err)
60 printBacktrace()
61 a = []string{"PANIC"}
62 b = a
63 c = a
3bd2d40 @nsf Add drop-cache command and support for local packages.
authored
64
65 // drop cache
a5ad476 @nsf Add semanticcontext.go, a first step towards refactoring functionality.
authored
66 daemon.DropCache()
e88dab2 @nsf Implement failsafe service.
authored
67 }
68 }()
a5ad476 @nsf Add semanticcontext.go, a first step towards refactoring functionality.
authored
69 a, b, c, d = daemon.acc.Apropos(file, filename, cursor)
e88dab2 @nsf Implement failsafe service.
authored
70 return
6379b17 @nsf Implement basics of a client and a server using jsonrpc.
authored
71 }
72
a5ad476 @nsf Add semanticcontext.go, a first step towards refactoring functionality.
authored
73 func Server_SMap(filename string) []DeclDesc {
74 defer func() {
75 if err := recover(); err != nil {
76 fmt.Printf("GOT PANIC!!!:\n")
77 fmt.Println(err)
78 printBacktrace()
79
80 // drop cache
81 daemon.DropCache()
82 }
83 }()
84 return daemon.semantic.GetSMap(filename)
85 }
86
94b7b01 @nsf More mess, change jsonrpc to simple rpc (uses gob).
authored
87 func Server_Close(notused int) int {
88 daemon.acr.Close()
89 return 0
6379b17 @nsf Implement basics of a client and a server using jsonrpc.
authored
90 }
91
a5fa988 @nsf Add new "status" command. Server now handles one request at a time.
authored
92 func Server_Status(notused int) string {
a5ad476 @nsf Add semanticcontext.go, a first step towards refactoring functionality.
authored
93 return daemon.acc.Status()
a5fa988 @nsf Add new "status" command. Server now handles one request at a time.
authored
94 }
95
3bd2d40 @nsf Add drop-cache command and support for local packages.
authored
96 func Server_DropCache(notused int) int {
97 // drop cache
a5ad476 @nsf Add semanticcontext.go, a first step towards refactoring functionality.
authored
98 daemon.DropCache()
3bd2d40 @nsf Add drop-cache command and support for local packages.
authored
99 return 0
100 }
101
a8bd075 @nsf Add configuration facility and 'set' command.
authored
102 func Server_Set(key, value string) string {
103 if key == "" {
104 return listConfig(&Config)
105 } else if value == "" {
106 return listOption(&Config, key)
107 }
108 return setOption(&Config, key, value)
109 }
110
6379b17 @nsf Implement basics of a client and a server using jsonrpc.
authored
111 //-------------------------------------------------------------------------
112 // Autocompletion Refactoring Server
113 //-------------------------------------------------------------------------
114
115 const (
116 ACR_CLOSE = iota
a920047 @nsf Progress..
authored
117 )
118
119 type ACRServer struct {
120 listener *net.UnixListener
b51bf09 @nsf Apply gofmt to all go source files.
authored
121 cmd_in chan int
a920047 @nsf Progress..
authored
122 }
123
124 func NewACRServer(path string) *ACRServer {
125 self := new(ACRServer)
126 addr, err := net.ResolveUnixAddr("unix", path)
127 if err != nil {
128 panic(err.String())
129 }
130
131 self.listener, err = net.ListenUnix("unix", addr)
132 if err != nil {
133 panic(err.String())
134 }
9b82898 @nsf Few fixes, be more nice if the archive cannot be found.
authored
135 self.cmd_in = make(chan int, 1)
a920047 @nsf Progress..
authored
136 return self
137 }
138
6379b17 @nsf Implement basics of a client and a server using jsonrpc.
authored
139 func acceptConnections(in chan net.Conn, listener *net.UnixListener) {
a920047 @nsf Progress..
authored
140 for {
6379b17 @nsf Implement basics of a client and a server using jsonrpc.
authored
141 c, err := listener.Accept()
a920047 @nsf Progress..
authored
142 if err != nil {
143 panic(err.String())
144 }
6379b17 @nsf Implement basics of a client and a server using jsonrpc.
authored
145 in <- c
146 }
147 }
148
149 func (self *ACRServer) Loop() {
150 conn_in := make(chan net.Conn)
151 go acceptConnections(conn_in, self.listener)
152 for {
153 // handle connections or server CMDs (currently one CMD)
154 select {
155 case c := <-conn_in:
a5fa988 @nsf Add new "status" command. Server now handles one request at a time.
authored
156 rpc.ServeConn(c)
bd72d4b @nsf Mutlifile packages support, few misc fixes.
authored
157 runtime.GC()
6379b17 @nsf Implement basics of a client and a server using jsonrpc.
authored
158 case cmd := <-self.cmd_in:
159 switch cmd {
160 case ACR_CLOSE:
161 return
162 }
94b7b01 @nsf More mess, change jsonrpc to simple rpc (uses gob).
authored
163 case sig := <-signal.Incoming:
96cb3a2 @nsf Better storage format, more precise autocompletions.
authored
164 usig, ok := sig.(signal.UnixSignal)
165 if !ok {
166 break
167 }
94b7b01 @nsf More mess, change jsonrpc to simple rpc (uses gob).
authored
168 if usig == signal.SIGINT || usig == signal.SIGTERM {
169 return
170 }
6379b17 @nsf Implement basics of a client and a server using jsonrpc.
authored
171 }
a920047 @nsf Progress..
authored
172 }
173 }
174
175 func (self *ACRServer) Close() {
6379b17 @nsf Implement basics of a client and a server using jsonrpc.
authored
176 self.cmd_in <- ACR_CLOSE
a920047 @nsf Progress..
authored
177 }
Something went wrong with that request. Please try again.