Browse files

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

gocode.go still contains common client/server code.
  • Loading branch information...
1 parent 34c54ec commit 4e9d7cf2cc1ed8cb9b672877cde78096347b1ec5 @nsf committed Feb 26, 2012
Showing with 184 additions and 170 deletions.
  1. +158 −0 client.go
  2. +1 −170 gocode.go
  3. +25 −0 server.go
View
158 client.go
@@ -0,0 +1,158 @@
+package main
+
+import (
+ "net/rpc"
+ "os"
+ "time"
+ "flag"
+ "fmt"
+ "io/ioutil"
+ "path/filepath"
+ "strconv"
+)
+
+func try_run_server() error {
+ path := get_executable_filename()
+ args := []string{os.Args[0], "-s", "-sock", *g_sock, "-addr", *g_addr}
+ cwd, _ := os.Getwd()
+ procattr := os.ProcAttr{Dir: cwd, Env: os.Environ(), Files: []*os.File{nil, nil, nil}}
+ p, err := os.StartProcess(path, args, &procattr)
+
+ if err != nil {
+ return err
+ }
+ return p.Release()
+}
+
+func try_to_connect(network, address string) (client *rpc.Client, err error) {
+ t := 0
+ for {
+ client, err = rpc.Dial(network, address)
+ if err != nil && t < 1000 {
+ time.Sleep(10 * time.Millisecond)
+ t += 10
+ continue
+ }
+ break
+ }
+
+ return
+}
+
+func do_client() int {
+ addr := *g_addr
+ if *g_sock == "unix" {
+ addr = get_socket_filename()
+ }
+
+ // client
+ client, err := rpc.Dial(*g_sock, addr)
+ if err != nil {
+ if *g_sock == "unix" && file_exists(addr) {
+ os.Remove(addr)
+ }
+
+ err = try_run_server()
+ if err != nil {
+ fmt.Printf("%s\n", err.Error())
+ return 1
+ }
+ client, err = try_to_connect(*g_sock, addr)
+ if err != nil {
+ fmt.Printf("%s\n", err.Error())
+ return 1
+ }
+ }
+ defer client.Close()
+
+ if flag.NArg() > 0 {
+ switch flag.Arg(0) {
+ case "autocomplete":
+ cmd_auto_complete(client)
+ case "close":
+ cmd_close(client)
+ case "status":
+ cmd_status(client)
+ case "drop-cache":
+ cmd_drop_cache(client)
+ case "set":
+ cmd_set(client)
+ }
+ }
+ return 0
+}
+
+//-------------------------------------------------------------------------
+// commands
+//-------------------------------------------------------------------------
+
+func cmd_status(c *rpc.Client) {
+ fmt.Printf("%s\n", client_status(c, 0))
+}
+
+func cmd_auto_complete(c *rpc.Client) {
+ var file []byte
+ var err error
+
+ if *g_input != "" {
+ file, err = ioutil.ReadFile(*g_input)
+ } else {
+ file, err = ioutil.ReadAll(os.Stdin)
+ }
+
+ if err != nil {
+ panic(err.Error())
+ }
+
+ var skipped int
+ file, skipped = filter_out_shebang(file)
+
+ filename := *g_input
+ cursor := -1
+
+ offset := ""
+ switch flag.NArg() {
+ case 2:
+ offset = flag.Arg(1)
+ case 3:
+ filename = flag.Arg(1) // Override default filename
+ offset = flag.Arg(2)
+ }
+
+ if offset != "" {
+ if offset[0] == 'c' || offset[0] == 'C' {
+ cursor, _ = strconv.Atoi(offset[1:])
+ cursor = char_to_byte_offset(file, cursor)
+ } else {
+ cursor, _ = strconv.Atoi(offset)
+ }
+ }
+
+ cursor -= skipped
+ if filename != "" && !filepath.IsAbs(filename) {
+ cwd, _ := os.Getwd()
+ filename = filepath.Join(cwd, filename)
+ }
+
+ f := get_formatter(*g_format)
+ f.write_candidates(client_auto_complete(c, file, filename, cursor))
+}
+
+func cmd_close(c *rpc.Client) {
+ client_close(c, 0)
+}
+
+func cmd_drop_cache(c *rpc.Client) {
+ client_drop_cache(c, 0)
+}
+
+func cmd_set(c *rpc.Client) {
+ switch flag.NArg() {
+ case 1:
+ fmt.Print(client_set(c, "\x00", "\x00"))
+ case 2:
+ fmt.Print(client_set(c, flag.Arg(1), "\x00"))
+ case 3:
+ fmt.Print(client_set(c, flag.Arg(1), flag.Arg(2)))
+ }
+}
View
171 gocode.go
@@ -3,12 +3,8 @@ package main
import (
"flag"
"fmt"
- "io/ioutil"
- "net/rpc"
"os"
"path/filepath"
- "strconv"
- "time"
)
var (
@@ -24,172 +20,7 @@ func get_socket_filename() string {
if user == "" {
user = "all"
}
- return fmt.Sprintf("%s/gocode-daemon.%s", os.TempDir(), user)
-}
-
-func do_server() int {
- g_config.read()
-
- addr := *g_addr
- if *g_sock == "unix" {
- addr = get_socket_filename()
- if file_exists(addr) {
- fmt.Printf("unix socket: '%s' already exists\n", addr)
- return 1
- }
- }
- g_daemon = new_daemon(*g_sock, addr)
- if *g_sock == "unix" {
- // cleanup unix socket file
- defer os.Remove(addr)
- }
-
- rpc.Register(new(RPC))
-
- g_daemon.loop()
- return 0
-}
-
-func cmd_status(c *rpc.Client) {
- fmt.Printf("%s\n", client_status(c, 0))
-}
-
-func cmd_auto_complete(c *rpc.Client) {
- var file []byte
- var err error
-
- if *g_input != "" {
- file, err = ioutil.ReadFile(*g_input)
- } else {
- file, err = ioutil.ReadAll(os.Stdin)
- }
-
- if err != nil {
- panic(err.Error())
- }
-
- var skipped int
- file, skipped = filter_out_shebang(file)
-
- filename := *g_input
- cursor := -1
-
- offset := ""
- switch flag.NArg() {
- case 2:
- offset = flag.Arg(1)
- case 3:
- filename = flag.Arg(1) // Override default filename
- offset = flag.Arg(2)
- }
-
- if offset != "" {
- if offset[0] == 'c' || offset[0] == 'C' {
- cursor, _ = strconv.Atoi(offset[1:])
- cursor = char_to_byte_offset(file, cursor)
- } else {
- cursor, _ = strconv.Atoi(offset)
- }
- }
-
- cursor -= skipped
- if filename != "" && !filepath.IsAbs(filename) {
- cwd, _ := os.Getwd()
- filename = filepath.Join(cwd, filename)
- }
-
- f := get_formatter(*g_format)
- f.write_candidates(client_auto_complete(c, file, filename, cursor))
-}
-
-func cmd_close(c *rpc.Client) {
- client_close(c, 0)
-}
-
-func cmd_drop_cache(c *rpc.Client) {
- client_drop_cache(c, 0)
-}
-
-func cmd_set(c *rpc.Client) {
- switch flag.NArg() {
- case 1:
- fmt.Print(client_set(c, "\x00", "\x00"))
- case 2:
- fmt.Print(client_set(c, flag.Arg(1), "\x00"))
- case 3:
- fmt.Print(client_set(c, flag.Arg(1), flag.Arg(2)))
- }
-}
-
-func try_run_server() error {
- path := get_executable_filename()
- args := []string{os.Args[0], "-s", "-sock", *g_sock, "-addr", *g_addr}
- cwd, _ := os.Getwd()
- procattr := os.ProcAttr{Dir: cwd, Env: os.Environ(), Files: []*os.File{nil, nil, nil}}
- p, err := os.StartProcess(path, args, &procattr)
-
- if err != nil {
- return err
- }
- return p.Release()
-}
-
-func try_to_connect(network, address string) (client *rpc.Client, err error) {
- t := 0
- for {
- client, err = rpc.Dial(network, address)
- if err != nil && t < 1000 {
- time.Sleep(10 * time.Millisecond)
- t += 10
- continue
- }
- break
- }
-
- return
-}
-
-func do_client() int {
- addr := *g_addr
- if *g_sock == "unix" {
- addr = get_socket_filename()
- }
-
- // client
- client, err := rpc.Dial(*g_sock, addr)
- if err != nil {
- if *g_sock == "unix" && file_exists(addr) {
- os.Remove(addr)
- }
-
- err = try_run_server()
- if err != nil {
- fmt.Printf("%s\n", err.Error())
- return 1
- }
- client, err = try_to_connect(*g_sock, addr)
- if err != nil {
- fmt.Printf("%s\n", err.Error())
- return 1
- }
- }
- defer client.Close()
-
- if flag.NArg() > 0 {
- switch flag.Arg(0) {
- case "autocomplete":
- cmd_auto_complete(client)
- case "close":
- cmd_close(client)
- case "status":
- cmd_status(client)
- case "drop-cache":
- cmd_drop_cache(client)
- case "set":
- cmd_set(client)
- }
- }
- return 0
+ return filepath.Join(os.TempDir(), fmt.Sprintf("gocode-daemon.%s", user))
}
func main() {
View
25 server.go
@@ -4,8 +4,33 @@ import (
"net"
"net/rpc"
"runtime"
+ "fmt"
+ "os"
)
+func do_server() int {
+ g_config.read()
+
+ addr := *g_addr
+ if *g_sock == "unix" {
+ addr = get_socket_filename()
+ if file_exists(addr) {
+ fmt.Printf("unix socket: '%s' already exists\n", addr)
+ return 1
+ }
+ }
+ g_daemon = new_daemon(*g_sock, addr)
+ if *g_sock == "unix" {
+ // cleanup unix socket file
+ defer os.Remove(addr)
+ }
+
+ rpc.Register(new(RPC))
+
+ g_daemon.loop()
+ return 0
+}
+
//-------------------------------------------------------------------------
// daemon
//-------------------------------------------------------------------------

0 comments on commit 4e9d7cf

Please sign in to comment.