Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 148 lines (126 sloc) 3.025 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"
fff16d1 nsf Big refactoring, CamelCase -> words_with_underscores.
authored
6 "net/rpc"
e88dab2 nsf Implement failsafe service.
authored
7 "runtime"
c6f24d3 nsf Get panic safety back.
authored
8 "sync"
6379b17 nsf Implement basics of a client and a server using jsonrpc.
authored
9 )
10
11 //-------------------------------------------------------------------------
fff16d1 nsf Big refactoring, CamelCase -> words_with_underscores.
authored
12 // daemon
e30aeaa nsf Misc comment updates and renames.
authored
13 //-------------------------------------------------------------------------
6379b17 nsf Implement basics of a client and a server using jsonrpc.
authored
14
fff16d1 nsf Big refactoring, CamelCase -> words_with_underscores.
authored
15 type daemon struct {
16 listener net.Listener
17 cmd_in chan int
18 autocomplete *auto_complete_context
19 pkgcache package_cache
20 declcache *decl_cache
6379b17 nsf Implement basics of a client and a server using jsonrpc.
authored
21 }
22
fff16d1 nsf Big refactoring, CamelCase -> words_with_underscores.
authored
23 func new_daemon(network, address string) *daemon {
24 var err error
25
26 d := new(daemon)
27 d.listener, err = net.Listen(network, address)
28 if err != nil {
29 panic(err)
30 }
31
32 d.cmd_in = make(chan int, 1)
33 d.pkgcache = new_package_cache()
34 d.declcache = new_decl_cache()
35 d.autocomplete = new_auto_complete_context(d.pkgcache, d.declcache)
668b580 nsf Get rid of 'self' name in method variables.
authored
36 return d
6379b17 nsf Implement basics of a client and a server using jsonrpc.
authored
37 }
38
fff16d1 nsf Big refactoring, CamelCase -> words_with_underscores.
authored
39 func (this *daemon) drop_cache() {
40 this.pkgcache = new_package_cache()
41 this.declcache = new_decl_cache()
42 this.autocomplete = new_auto_complete_context(this.pkgcache, this.declcache)
43 }
44
45 const (
46 daemon_close = iota
47 )
48
49 func (this *daemon) loop() {
50 conn_in := make(chan net.Conn)
51 go func() {
52 for {
53 c, err := this.listener.Accept()
54 if err != nil {
55 panic(err)
56 }
57 conn_in <- c
58 }
59 }()
60 for {
61 // handle connections or server CMDs (currently one CMD)
62 select {
63 case c := <-conn_in:
64 rpc.ServeConn(c)
65 runtime.GC()
66 case cmd := <-this.cmd_in:
67 switch cmd {
68 case daemon_close:
69 return
70 }
71 }
72 }
73 }
74
75 func (this *daemon) close() {
76 this.cmd_in <- daemon_close
a5ad476 nsf Add semanticcontext.go, a first step towards refactoring functionality.
authored
77 }
78
fff16d1 nsf Big refactoring, CamelCase -> words_with_underscores.
authored
79 var g_daemon *daemon
94b7b01 nsf More mess, change jsonrpc to simple rpc (uses gob).
authored
80
81 //-------------------------------------------------------------------------
fff16d1 nsf Big refactoring, CamelCase -> words_with_underscores.
authored
82 // print_backtrace
e30aeaa nsf Misc comment updates and renames.
authored
83 //-------------------------------------------------------------------------
6379b17 nsf Implement basics of a client and a server using jsonrpc.
authored
84
fff16d1 nsf Big refactoring, CamelCase -> words_with_underscores.
authored
85 var g_backtrace_mutex sync.Mutex
c6f24d3 nsf Get panic safety back.
authored
86
fff16d1 nsf Big refactoring, CamelCase -> words_with_underscores.
authored
87 func print_backtrace(err interface{}) {
88 g_backtrace_mutex.Lock()
89 defer g_backtrace_mutex.Unlock()
c6f24d3 nsf Get panic safety back.
authored
90 fmt.Printf("panic: %v\n", err)
e88dab2 nsf Implement failsafe service.
authored
91 i := 2
92 for {
93 pc, file, line, ok := runtime.Caller(i)
94 if !ok {
95 break
96 }
97 f := runtime.FuncForPC(pc)
98 fmt.Printf("%d(%s): %s:%d\n", i-1, f.Name(), file, line)
99 i++
100 }
4a682ec nsf Add empty line after stack trace dump.
authored
101 fmt.Println("")
e88dab2 nsf Implement failsafe service.
authored
102 }
103
e30aeaa nsf Misc comment updates and renames.
authored
104 //-------------------------------------------------------------------------
fff16d1 nsf Big refactoring, CamelCase -> words_with_underscores.
authored
105 // server_* functions
e30aeaa nsf Misc comment updates and renames.
authored
106 //
fff16d1 nsf Big refactoring, CamelCase -> words_with_underscores.
authored
107 // Corresponding client_* functions are autogenerated by goremote.
e30aeaa nsf Misc comment updates and renames.
authored
108 //-------------------------------------------------------------------------
109
4a058ff nsf Structify the communication between daemon and client.
authored
110 func server_auto_complete(file []byte, filename string, cursor int) (c []candidate, d int) {
e88dab2 nsf Implement failsafe service.
authored
111 defer func() {
112 if err := recover(); err != nil {
fff16d1 nsf Big refactoring, CamelCase -> words_with_underscores.
authored
113 print_backtrace(err)
4a058ff nsf Structify the communication between daemon and client.
authored
114 c = []candidate{
115 {"PANIC", "PANIC", "PANIC"},
116 }
3bd2d40 nsf Add drop-cache command and support for local packages.
authored
117
118 // drop cache
fff16d1 nsf Big refactoring, CamelCase -> words_with_underscores.
authored
119 g_daemon.drop_cache()
e88dab2 nsf Implement failsafe service.
authored
120 }
121 }()
4a058ff nsf Structify the communication between daemon and client.
authored
122 return g_daemon.autocomplete.apropos(file, filename, cursor)
6379b17 nsf Implement basics of a client and a server using jsonrpc.
authored
123 }
124
fff16d1 nsf Big refactoring, CamelCase -> words_with_underscores.
authored
125 func server_close(notused int) int {
126 g_daemon.close()
94b7b01 nsf More mess, change jsonrpc to simple rpc (uses gob).
authored
127 return 0
6379b17 nsf Implement basics of a client and a server using jsonrpc.
authored
128 }
129
fff16d1 nsf Big refactoring, CamelCase -> words_with_underscores.
authored
130 func server_status(notused int) string {
131 return g_daemon.autocomplete.status()
a5fa988 nsf Add new "status" command. Server now handles one request at a time.
authored
132 }
133
fff16d1 nsf Big refactoring, CamelCase -> words_with_underscores.
authored
134 func server_drop_cache(notused int) int {
3bd2d40 nsf Add drop-cache command and support for local packages.
authored
135 // drop cache
fff16d1 nsf Big refactoring, CamelCase -> words_with_underscores.
authored
136 g_daemon.drop_cache()
3bd2d40 nsf Add drop-cache command and support for local packages.
authored
137 return 0
138 }
139
fff16d1 nsf Big refactoring, CamelCase -> words_with_underscores.
authored
140 func server_set(key, value string) string {
a889242 nsf Radical change. Move towards goinstall compatibility.
authored
141 if key == "\x00" {
fff16d1 nsf Big refactoring, CamelCase -> words_with_underscores.
authored
142 return g_config.list()
a889242 nsf Radical change. Move towards goinstall compatibility.
authored
143 } else if value == "\x00" {
fff16d1 nsf Big refactoring, CamelCase -> words_with_underscores.
authored
144 return g_config.list_option(key)
6379b17 nsf Implement basics of a client and a server using jsonrpc.
authored
145 }
fff16d1 nsf Big refactoring, CamelCase -> words_with_underscores.
authored
146 return g_config.set_option(key, value)
a920047 nsf Progress..
authored
147 }
Something went wrong with that request. Please try again.