Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 169 lines (144 sloc) 3.381 kb
a920047 @nsf Progress..
authored
1 package main
2
3 import (
fcf51d5 @nsf Update due to changes in Go weekly.2011-11-09.
authored
4 "fmt"
a920047 @nsf Progress..
authored
5 "net"
fcf51d5 @nsf Update due to changes in Go weekly.2011-11-09.
authored
6 "net/rpc"
94b7b01 @nsf More mess, change jsonrpc to simple rpc (uses gob).
authored
7 "os/signal"
e88dab2 @nsf Implement failsafe service.
authored
8 "runtime"
c6f24d3 @nsf Get panic safety back.
authored
9 "sync"
6379b17 @nsf Implement basics of a client and a server using jsonrpc.
authored
10 )
11
12 //-------------------------------------------------------------------------
e30aeaa @nsf Misc comment updates and renames.
authored
13 // Daemon
14 //-------------------------------------------------------------------------
6379b17 @nsf Implement basics of a client and a server using jsonrpc.
authored
15
e30aeaa @nsf Misc comment updates and renames.
authored
16 type Daemon struct {
17 acr *Server
b89be8e @nsf Change term 'module' to term 'package' everywhere + gofmt.
authored
18 acc *AutoCompleteContext
c984d49 @nsf More renaming. Less obvious things like 'ms' and 'mcache'.
authored
19 pcache PackageCache
a5ad476 @nsf Add semanticcontext.go, a first step towards refactoring functionality.
authored
20 declcache *DeclCache
6379b17 @nsf Implement basics of a client and a server using jsonrpc.
authored
21 }
22
e098933 @nsf Add "sock" and "addr" command line options. Implement TCP server.
authored
23 func NewDaemon(network, address string) *Daemon {
e30aeaa @nsf Misc comment updates and renames.
authored
24 d := new(Daemon)
e098933 @nsf Add "sock" and "addr" command line options. Implement TCP server.
authored
25 d.acr = NewServer(network, address)
c984d49 @nsf More renaming. Less obvious things like 'ms' and 'mcache'.
authored
26 d.pcache = NewPackageCache()
668b580 @nsf Get rid of 'self' name in method variables.
authored
27 d.declcache = NewDeclCache()
c984d49 @nsf More renaming. Less obvious things like 'ms' and 'mcache'.
authored
28 d.acc = NewAutoCompleteContext(d.pcache, d.declcache)
668b580 @nsf Get rid of 'self' name in method variables.
authored
29 return d
6379b17 @nsf Implement basics of a client and a server using jsonrpc.
authored
30 }
31
e30aeaa @nsf Misc comment updates and renames.
authored
32 func (d *Daemon) DropCache() {
c984d49 @nsf More renaming. Less obvious things like 'ms' and 'mcache'.
authored
33 d.pcache = NewPackageCache()
668b580 @nsf Get rid of 'self' name in method variables.
authored
34 d.declcache = NewDeclCache()
c984d49 @nsf More renaming. Less obvious things like 'ms' and 'mcache'.
authored
35 d.acc = NewAutoCompleteContext(d.pcache, d.declcache)
a5ad476 @nsf Add semanticcontext.go, a first step towards refactoring functionality.
authored
36 }
37
e30aeaa @nsf Misc comment updates and renames.
authored
38 var daemon *Daemon
94b7b01 @nsf More mess, change jsonrpc to simple rpc (uses gob).
authored
39
40 //-------------------------------------------------------------------------
e30aeaa @nsf Misc comment updates and renames.
authored
41 // printBacktrace
42 //-------------------------------------------------------------------------
6379b17 @nsf Implement basics of a client and a server using jsonrpc.
authored
43
c6f24d3 @nsf Get panic safety back.
authored
44 var btSync sync.Mutex
45
46 func printBacktrace(err interface{}) {
47 btSync.Lock()
48 defer btSync.Unlock()
49 fmt.Printf("panic: %v\n", err)
e88dab2 @nsf Implement failsafe service.
authored
50 i := 2
51 for {
52 pc, file, line, ok := runtime.Caller(i)
53 if !ok {
54 break
55 }
56 f := runtime.FuncForPC(pc)
57 fmt.Printf("%d(%s): %s:%d\n", i-1, f.Name(), file, line)
58 i++
59 }
4a682ec @nsf Add empty line after stack trace dump.
authored
60 fmt.Println("")
e88dab2 @nsf Implement failsafe service.
authored
61 }
62
e30aeaa @nsf Misc comment updates and renames.
authored
63 //-------------------------------------------------------------------------
64 // Server_* functions
65 //
66 // Corresponding Client_* functions are autogenerated by goremote.
67 //-------------------------------------------------------------------------
68
bd72d4b @nsf Mutlifile packages support, few misc fixes.
authored
69 func Server_AutoComplete(file []byte, filename string, cursor int) (a, b, c []string, d int) {
e88dab2 @nsf Implement failsafe service.
authored
70 defer func() {
71 if err := recover(); err != nil {
c6f24d3 @nsf Get panic safety back.
authored
72 printBacktrace(err)
e88dab2 @nsf Implement failsafe service.
authored
73 a = []string{"PANIC"}
74 b = a
75 c = a
3bd2d40 @nsf Add drop-cache command and support for local packages.
authored
76
77 // drop cache
a5ad476 @nsf Add semanticcontext.go, a first step towards refactoring functionality.
authored
78 daemon.DropCache()
e88dab2 @nsf Implement failsafe service.
authored
79 }
80 }()
a5ad476 @nsf Add semanticcontext.go, a first step towards refactoring functionality.
authored
81 a, b, c, d = daemon.acc.Apropos(file, filename, cursor)
e88dab2 @nsf Implement failsafe service.
authored
82 return
6379b17 @nsf Implement basics of a client and a server using jsonrpc.
authored
83 }
84
94b7b01 @nsf More mess, change jsonrpc to simple rpc (uses gob).
authored
85 func Server_Close(notused int) int {
86 daemon.acr.Close()
87 return 0
6379b17 @nsf Implement basics of a client and a server using jsonrpc.
authored
88 }
89
a5fa988 @nsf Add new "status" command. Server now handles one request at a time.
authored
90 func Server_Status(notused int) string {
a5ad476 @nsf Add semanticcontext.go, a first step towards refactoring functionality.
authored
91 return daemon.acc.Status()
a5fa988 @nsf Add new "status" command. Server now handles one request at a time.
authored
92 }
93
3bd2d40 @nsf Add drop-cache command and support for local packages.
authored
94 func Server_DropCache(notused int) int {
95 // drop cache
a5ad476 @nsf Add semanticcontext.go, a first step towards refactoring functionality.
authored
96 daemon.DropCache()
3bd2d40 @nsf Add drop-cache command and support for local packages.
authored
97 return 0
98 }
99
a8bd075 @nsf Add configuration facility and 'set' command.
authored
100 func Server_Set(key, value string) string {
a889242 @nsf Radical change. Move towards goinstall compatibility.
authored
101 if key == "\x00" {
a8bd075 @nsf Add configuration facility and 'set' command.
authored
102 return listConfig(&Config)
a889242 @nsf Radical change. Move towards goinstall compatibility.
authored
103 } else if value == "\x00" {
a8bd075 @nsf Add configuration facility and 'set' command.
authored
104 return listOption(&Config, key)
105 }
106 return setOption(&Config, key, value)
107 }
108
6379b17 @nsf Implement basics of a client and a server using jsonrpc.
authored
109 //-------------------------------------------------------------------------
e30aeaa @nsf Misc comment updates and renames.
authored
110 // Server
6379b17 @nsf Implement basics of a client and a server using jsonrpc.
authored
111 //-------------------------------------------------------------------------
112
113 const (
e30aeaa @nsf Misc comment updates and renames.
authored
114 SERVER_CLOSE = iota
a920047 @nsf Progress..
authored
115 )
116
e30aeaa @nsf Misc comment updates and renames.
authored
117 type Server struct {
e098933 @nsf Add "sock" and "addr" command line options. Implement TCP server.
authored
118 listener net.Listener
b51bf09 @nsf Apply gofmt to all go source files.
authored
119 cmd_in chan int
a920047 @nsf Progress..
authored
120 }
121
e098933 @nsf Add "sock" and "addr" command line options. Implement TCP server.
authored
122 func NewServer(network, address string) *Server {
410cc44 @nsf Use the new built-in "error" instead of "os.Error".
authored
123 var err error
a920047 @nsf Progress..
authored
124
e098933 @nsf Add "sock" and "addr" command line options. Implement TCP server.
authored
125 s := new(Server)
126 s.listener, err = net.Listen(network, address)
a920047 @nsf Progress..
authored
127 if err != nil {
410cc44 @nsf Use the new built-in "error" instead of "os.Error".
authored
128 panic(err.Error())
a920047 @nsf Progress..
authored
129 }
668b580 @nsf Get rid of 'self' name in method variables.
authored
130 s.cmd_in = make(chan int, 1)
131 return s
a920047 @nsf Progress..
authored
132 }
133
e098933 @nsf Add "sock" and "addr" command line options. Implement TCP server.
authored
134 func acceptConnections(in chan net.Conn, listener net.Listener) {
a920047 @nsf Progress..
authored
135 for {
6379b17 @nsf Implement basics of a client and a server using jsonrpc.
authored
136 c, err := listener.Accept()
a920047 @nsf Progress..
authored
137 if err != nil {
410cc44 @nsf Use the new built-in "error" instead of "os.Error".
authored
138 panic(err.Error())
a920047 @nsf Progress..
authored
139 }
6379b17 @nsf Implement basics of a client and a server using jsonrpc.
authored
140 in <- c
141 }
142 }
143
e30aeaa @nsf Misc comment updates and renames.
authored
144 func (s *Server) Loop() {
6379b17 @nsf Implement basics of a client and a server using jsonrpc.
authored
145 conn_in := make(chan net.Conn)
668b580 @nsf Get rid of 'self' name in method variables.
authored
146 go acceptConnections(conn_in, s.listener)
6379b17 @nsf Implement basics of a client and a server using jsonrpc.
authored
147 for {
148 // handle connections or server CMDs (currently one CMD)
149 select {
150 case c := <-conn_in:
a5fa988 @nsf Add new "status" command. Server now handles one request at a time.
authored
151 rpc.ServeConn(c)
bd72d4b @nsf Mutlifile packages support, few misc fixes.
authored
152 runtime.GC()
668b580 @nsf Get rid of 'self' name in method variables.
authored
153 case cmd := <-s.cmd_in:
6379b17 @nsf Implement basics of a client and a server using jsonrpc.
authored
154 switch cmd {
e30aeaa @nsf Misc comment updates and renames.
authored
155 case SERVER_CLOSE:
6379b17 @nsf Implement basics of a client and a server using jsonrpc.
authored
156 return
157 }
94b7b01 @nsf More mess, change jsonrpc to simple rpc (uses gob).
authored
158 case sig := <-signal.Incoming:
0543ba4 @nsf Fix win port issues. Signal handling and default sock type.
authored
159 if IsTerminationSignal(sig) {
94b7b01 @nsf More mess, change jsonrpc to simple rpc (uses gob).
authored
160 return
161 }
6379b17 @nsf Implement basics of a client and a server using jsonrpc.
authored
162 }
a920047 @nsf Progress..
authored
163 }
164 }
165
e30aeaa @nsf Misc comment updates and renames.
authored
166 func (s *Server) Close() {
167 s.cmd_in <- SERVER_CLOSE
a920047 @nsf Progress..
authored
168 }
Something went wrong with that request. Please try again.