Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 148 lines (125 sloc) 2.882 kB
a920047 @nsf Progress..
authored
1 package main
2
3 import (
4 "net"
fff16d1 @nsf Big refactoring, CamelCase -> words_with_underscores.
authored
5 "net/rpc"
e88dab2 @nsf Implement failsafe service.
authored
6 "runtime"
4e9d7cf @nsf Separate code from gocode.go to client.go and server.go.
authored
7 "fmt"
8 "os"
6379b17 @nsf Implement basics of a client and a server using jsonrpc.
authored
9 )
10
4e9d7cf @nsf Separate code from gocode.go to client.go and server.go.
authored
11 func do_server() int {
12 g_config.read()
13
14 addr := *g_addr
15 if *g_sock == "unix" {
16 addr = get_socket_filename()
17 if file_exists(addr) {
18 fmt.Printf("unix socket: '%s' already exists\n", addr)
19 return 1
20 }
21 }
22 g_daemon = new_daemon(*g_sock, addr)
23 if *g_sock == "unix" {
24 // cleanup unix socket file
25 defer os.Remove(addr)
26 }
27
28 rpc.Register(new(RPC))
29
30 g_daemon.loop()
31 return 0
32 }
33
6379b17 @nsf Implement basics of a client and a server using jsonrpc.
authored
34 //-------------------------------------------------------------------------
fff16d1 @nsf Big refactoring, CamelCase -> words_with_underscores.
authored
35 // daemon
e30aeaa @nsf Misc comment updates and renames.
authored
36 //-------------------------------------------------------------------------
6379b17 @nsf Implement basics of a client and a server using jsonrpc.
authored
37
fff16d1 @nsf Big refactoring, CamelCase -> words_with_underscores.
authored
38 type daemon struct {
39 listener net.Listener
40 cmd_in chan int
41 autocomplete *auto_complete_context
42 pkgcache package_cache
43 declcache *decl_cache
6379b17 @nsf Implement basics of a client and a server using jsonrpc.
authored
44 }
45
fff16d1 @nsf Big refactoring, CamelCase -> words_with_underscores.
authored
46 func new_daemon(network, address string) *daemon {
47 var err error
48
49 d := new(daemon)
50 d.listener, err = net.Listen(network, address)
51 if err != nil {
52 panic(err)
53 }
54
55 d.cmd_in = make(chan int, 1)
56 d.pkgcache = new_package_cache()
57 d.declcache = new_decl_cache()
58 d.autocomplete = new_auto_complete_context(d.pkgcache, d.declcache)
668b580 @nsf Get rid of 'self' name in method variables.
authored
59 return d
6379b17 @nsf Implement basics of a client and a server using jsonrpc.
authored
60 }
61
fff16d1 @nsf Big refactoring, CamelCase -> words_with_underscores.
authored
62 func (this *daemon) drop_cache() {
63 this.pkgcache = new_package_cache()
64 this.declcache = new_decl_cache()
65 this.autocomplete = new_auto_complete_context(this.pkgcache, this.declcache)
66 }
67
68 const (
69 daemon_close = iota
70 )
71
72 func (this *daemon) loop() {
73 conn_in := make(chan net.Conn)
74 go func() {
75 for {
76 c, err := this.listener.Accept()
77 if err != nil {
78 panic(err)
79 }
80 conn_in <- c
81 }
82 }()
83 for {
84 // handle connections or server CMDs (currently one CMD)
85 select {
86 case c := <-conn_in:
87 rpc.ServeConn(c)
88 runtime.GC()
89 case cmd := <-this.cmd_in:
90 switch cmd {
91 case daemon_close:
92 return
93 }
94 }
95 }
96 }
97
98 func (this *daemon) close() {
99 this.cmd_in <- daemon_close
a5ad476 @nsf Add semanticcontext.go, a first step towards refactoring functionality.
authored
100 }
101
fff16d1 @nsf Big refactoring, CamelCase -> words_with_underscores.
authored
102 var g_daemon *daemon
94b7b01 @nsf More mess, change jsonrpc to simple rpc (uses gob).
authored
103
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{
8ce3695 @nsf Make the class into an integer type.
authored
115 {"PANIC", "PANIC", decl_invalid},
4a058ff @nsf Structify the communication between daemon and client.
authored
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.