Skip to content
This repository
Browse code

Separate code from gocode.go to client.go and server.go.

gocode.go still contains common client/server code.
  • Loading branch information...
commit 4e9d7cf2cc1ed8cb9b672877cde78096347b1ec5 1 parent 34c54ec
nsf authored

Showing 3 changed files with 184 additions and 170 deletions. Show diff stats Hide diff stats

  1. +158 0 client.go
  2. +1 170 gocode.go
  3. +25 0 server.go
158 client.go
... ... @@ -0,0 +1,158 @@
  1 +package main
  2 +
  3 +import (
  4 + "net/rpc"
  5 + "os"
  6 + "time"
  7 + "flag"
  8 + "fmt"
  9 + "io/ioutil"
  10 + "path/filepath"
  11 + "strconv"
  12 +)
  13 +
  14 +func try_run_server() error {
  15 + path := get_executable_filename()
  16 + args := []string{os.Args[0], "-s", "-sock", *g_sock, "-addr", *g_addr}
  17 + cwd, _ := os.Getwd()
  18 + procattr := os.ProcAttr{Dir: cwd, Env: os.Environ(), Files: []*os.File{nil, nil, nil}}
  19 + p, err := os.StartProcess(path, args, &procattr)
  20 +
  21 + if err != nil {
  22 + return err
  23 + }
  24 + return p.Release()
  25 +}
  26 +
  27 +func try_to_connect(network, address string) (client *rpc.Client, err error) {
  28 + t := 0
  29 + for {
  30 + client, err = rpc.Dial(network, address)
  31 + if err != nil && t < 1000 {
  32 + time.Sleep(10 * time.Millisecond)
  33 + t += 10
  34 + continue
  35 + }
  36 + break
  37 + }
  38 +
  39 + return
  40 +}
  41 +
  42 +func do_client() int {
  43 + addr := *g_addr
  44 + if *g_sock == "unix" {
  45 + addr = get_socket_filename()
  46 + }
  47 +
  48 + // client
  49 + client, err := rpc.Dial(*g_sock, addr)
  50 + if err != nil {
  51 + if *g_sock == "unix" && file_exists(addr) {
  52 + os.Remove(addr)
  53 + }
  54 +
  55 + err = try_run_server()
  56 + if err != nil {
  57 + fmt.Printf("%s\n", err.Error())
  58 + return 1
  59 + }
  60 + client, err = try_to_connect(*g_sock, addr)
  61 + if err != nil {
  62 + fmt.Printf("%s\n", err.Error())
  63 + return 1
  64 + }
  65 + }
  66 + defer client.Close()
  67 +
  68 + if flag.NArg() > 0 {
  69 + switch flag.Arg(0) {
  70 + case "autocomplete":
  71 + cmd_auto_complete(client)
  72 + case "close":
  73 + cmd_close(client)
  74 + case "status":
  75 + cmd_status(client)
  76 + case "drop-cache":
  77 + cmd_drop_cache(client)
  78 + case "set":
  79 + cmd_set(client)
  80 + }
  81 + }
  82 + return 0
  83 +}
  84 +
  85 +//-------------------------------------------------------------------------
  86 +// commands
  87 +//-------------------------------------------------------------------------
  88 +
  89 +func cmd_status(c *rpc.Client) {
  90 + fmt.Printf("%s\n", client_status(c, 0))
  91 +}
  92 +
  93 +func cmd_auto_complete(c *rpc.Client) {
  94 + var file []byte
  95 + var err error
  96 +
  97 + if *g_input != "" {
  98 + file, err = ioutil.ReadFile(*g_input)
  99 + } else {
  100 + file, err = ioutil.ReadAll(os.Stdin)
  101 + }
  102 +
  103 + if err != nil {
  104 + panic(err.Error())
  105 + }
  106 +
  107 + var skipped int
  108 + file, skipped = filter_out_shebang(file)
  109 +
  110 + filename := *g_input
  111 + cursor := -1
  112 +
  113 + offset := ""
  114 + switch flag.NArg() {
  115 + case 2:
  116 + offset = flag.Arg(1)
  117 + case 3:
  118 + filename = flag.Arg(1) // Override default filename
  119 + offset = flag.Arg(2)
  120 + }
  121 +
  122 + if offset != "" {
  123 + if offset[0] == 'c' || offset[0] == 'C' {
  124 + cursor, _ = strconv.Atoi(offset[1:])
  125 + cursor = char_to_byte_offset(file, cursor)
  126 + } else {
  127 + cursor, _ = strconv.Atoi(offset)
  128 + }
  129 + }
  130 +
  131 + cursor -= skipped
  132 + if filename != "" && !filepath.IsAbs(filename) {
  133 + cwd, _ := os.Getwd()
  134 + filename = filepath.Join(cwd, filename)
  135 + }
  136 +
  137 + f := get_formatter(*g_format)
  138 + f.write_candidates(client_auto_complete(c, file, filename, cursor))
  139 +}
  140 +
  141 +func cmd_close(c *rpc.Client) {
  142 + client_close(c, 0)
  143 +}
  144 +
  145 +func cmd_drop_cache(c *rpc.Client) {
  146 + client_drop_cache(c, 0)
  147 +}
  148 +
  149 +func cmd_set(c *rpc.Client) {
  150 + switch flag.NArg() {
  151 + case 1:
  152 + fmt.Print(client_set(c, "\x00", "\x00"))
  153 + case 2:
  154 + fmt.Print(client_set(c, flag.Arg(1), "\x00"))
  155 + case 3:
  156 + fmt.Print(client_set(c, flag.Arg(1), flag.Arg(2)))
  157 + }
  158 +}
171 gocode.go
@@ -3,12 +3,8 @@ package main
3 3 import (
4 4 "flag"
5 5 "fmt"
6   - "io/ioutil"
7   - "net/rpc"
8 6 "os"
9 7 "path/filepath"
10   - "strconv"
11   - "time"
12 8 )
13 9
14 10 var (
@@ -24,172 +20,7 @@ func get_socket_filename() string {
24 20 if user == "" {
25 21 user = "all"
26 22 }
27   - return fmt.Sprintf("%s/gocode-daemon.%s", os.TempDir(), user)
28   -}
29   -
30   -func do_server() int {
31   - g_config.read()
32   -
33   - addr := *g_addr
34   - if *g_sock == "unix" {
35   - addr = get_socket_filename()
36   - if file_exists(addr) {
37   - fmt.Printf("unix socket: '%s' already exists\n", addr)
38   - return 1
39   - }
40   - }
41   - g_daemon = new_daemon(*g_sock, addr)
42   - if *g_sock == "unix" {
43   - // cleanup unix socket file
44   - defer os.Remove(addr)
45   - }
46   -
47   - rpc.Register(new(RPC))
48   -
49   - g_daemon.loop()
50   - return 0
51   -}
52   -
53   -func cmd_status(c *rpc.Client) {
54   - fmt.Printf("%s\n", client_status(c, 0))
55   -}
56   -
57   -func cmd_auto_complete(c *rpc.Client) {
58   - var file []byte
59   - var err error
60   -
61   - if *g_input != "" {
62   - file, err = ioutil.ReadFile(*g_input)
63   - } else {
64   - file, err = ioutil.ReadAll(os.Stdin)
65   - }
66   -
67   - if err != nil {
68   - panic(err.Error())
69   - }
70   -
71   - var skipped int
72   - file, skipped = filter_out_shebang(file)
73   -
74   - filename := *g_input
75   - cursor := -1
76   -
77   - offset := ""
78   - switch flag.NArg() {
79   - case 2:
80   - offset = flag.Arg(1)
81   - case 3:
82   - filename = flag.Arg(1) // Override default filename
83   - offset = flag.Arg(2)
84   - }
85   -
86   - if offset != "" {
87   - if offset[0] == 'c' || offset[0] == 'C' {
88   - cursor, _ = strconv.Atoi(offset[1:])
89   - cursor = char_to_byte_offset(file, cursor)
90   - } else {
91   - cursor, _ = strconv.Atoi(offset)
92   - }
93   - }
94   -
95   - cursor -= skipped
96   - if filename != "" && !filepath.IsAbs(filename) {
97   - cwd, _ := os.Getwd()
98   - filename = filepath.Join(cwd, filename)
99   - }
100   -
101   - f := get_formatter(*g_format)
102   - f.write_candidates(client_auto_complete(c, file, filename, cursor))
103   -}
104   -
105   -func cmd_close(c *rpc.Client) {
106   - client_close(c, 0)
107   -}
108   -
109   -func cmd_drop_cache(c *rpc.Client) {
110   - client_drop_cache(c, 0)
111   -}
112   -
113   -func cmd_set(c *rpc.Client) {
114   - switch flag.NArg() {
115   - case 1:
116   - fmt.Print(client_set(c, "\x00", "\x00"))
117   - case 2:
118   - fmt.Print(client_set(c, flag.Arg(1), "\x00"))
119   - case 3:
120   - fmt.Print(client_set(c, flag.Arg(1), flag.Arg(2)))
121   - }
122   -}
123   -
124   -func try_run_server() error {
125   - path := get_executable_filename()
126   - args := []string{os.Args[0], "-s", "-sock", *g_sock, "-addr", *g_addr}
127   - cwd, _ := os.Getwd()
128   - procattr := os.ProcAttr{Dir: cwd, Env: os.Environ(), Files: []*os.File{nil, nil, nil}}
129   - p, err := os.StartProcess(path, args, &procattr)
130   -
131   - if err != nil {
132   - return err
133   - }
134   - return p.Release()
135   -}
136   -
137   -func try_to_connect(network, address string) (client *rpc.Client, err error) {
138   - t := 0
139   - for {
140   - client, err = rpc.Dial(network, address)
141   - if err != nil && t < 1000 {
142   - time.Sleep(10 * time.Millisecond)
143   - t += 10
144   - continue
145   - }
146   - break
147   - }
148   -
149   - return
150   -}
151   -
152   -func do_client() int {
153   - addr := *g_addr
154   - if *g_sock == "unix" {
155   - addr = get_socket_filename()
156   - }
157   -
158   - // client
159   - client, err := rpc.Dial(*g_sock, addr)
160   - if err != nil {
161   - if *g_sock == "unix" && file_exists(addr) {
162   - os.Remove(addr)
163   - }
164   -
165   - err = try_run_server()
166   - if err != nil {
167   - fmt.Printf("%s\n", err.Error())
168   - return 1
169   - }
170   - client, err = try_to_connect(*g_sock, addr)
171   - if err != nil {
172   - fmt.Printf("%s\n", err.Error())
173   - return 1
174   - }
175   - }
176   - defer client.Close()
177   -
178   - if flag.NArg() > 0 {
179   - switch flag.Arg(0) {
180   - case "autocomplete":
181   - cmd_auto_complete(client)
182   - case "close":
183   - cmd_close(client)
184   - case "status":
185   - cmd_status(client)
186   - case "drop-cache":
187   - cmd_drop_cache(client)
188   - case "set":
189   - cmd_set(client)
190   - }
191   - }
192   - return 0
  23 + return filepath.Join(os.TempDir(), fmt.Sprintf("gocode-daemon.%s", user))
193 24 }
194 25
195 26 func main() {
25 server.go
@@ -4,8 +4,33 @@ import (
4 4 "net"
5 5 "net/rpc"
6 6 "runtime"
  7 + "fmt"
  8 + "os"
7 9 )
8 10
  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 +
9 34 //-------------------------------------------------------------------------
10 35 // daemon
11 36 //-------------------------------------------------------------------------

0 comments on commit 4e9d7cf

Please sign in to comment.
Something went wrong with that request. Please try again.