gds-server.scm 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. ;;;; Guile Debugger UI server
  2. ;;; Copyright (C) 2003 Free Software Foundation, Inc.
  3. ;;;
  4. ;; This library is free software; you can redistribute it and/or
  5. ;; modify it under the terms of the GNU Lesser General Public
  6. ;; License as published by the Free Software Foundation; either
  7. ;; version 2.1 of the License, or (at your option) any later version.
  8. ;;
  9. ;; This library is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. ;; Lesser General Public License for more details.
  13. ;;
  14. ;; You should have received a copy of the GNU Lesser General Public
  15. ;; License along with this library; if not, write to the Free Software
  16. ;; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. (define-module (ice-9 gds-server)
  18. #:export (run-server))
  19. ;; UI is normally via a pipe to Emacs, so make sure to flush output
  20. ;; every time we write.
  21. (define (write-to-ui form)
  22. (write form)
  23. (newline)
  24. (force-output))
  25. (define (trc . args)
  26. (write-to-ui (cons '* args)))
  27. (define (with-error->eof proc port)
  28. (catch #t
  29. (lambda () (proc port))
  30. (lambda args the-eof-object)))
  31. (define connection->id (make-object-property))
  32. (define (run-server port-or-path)
  33. (or (integer? port-or-path)
  34. (string? port-or-path)
  35. (error "port-or-path should be an integer (port number) or a string (file name)"
  36. port-or-path))
  37. (let ((server (socket (if (integer? port-or-path) PF_INET PF_UNIX)
  38. SOCK_STREAM
  39. 0)))
  40. ;; Initialize server socket.
  41. (if (integer? port-or-path)
  42. (begin
  43. (setsockopt server SOL_SOCKET SO_REUSEADDR 1)
  44. (bind server AF_INET INADDR_ANY port-or-path))
  45. (begin
  46. (catch #t
  47. (lambda () (delete-file port-or-path))
  48. (lambda _ #f))
  49. (bind server AF_UNIX port-or-path)))
  50. ;; Start listening.
  51. (listen server 5)
  52. (let loop ((clients '()) (readable-sockets '()))
  53. (define (do-read port)
  54. (cond ((eq? port (current-input-port))
  55. (do-read-from-ui))
  56. ((eq? port server)
  57. (accept-new-client))
  58. (else
  59. (do-read-from-client port))))
  60. (define (do-read-from-ui)
  61. (trc "reading from ui")
  62. (let* ((form (with-error->eof read (current-input-port)))
  63. (client (assq-ref (map (lambda (port)
  64. (cons (connection->id port) port))
  65. clients)
  66. (car form))))
  67. (with-error->eof read-char (current-input-port))
  68. (if client
  69. (begin
  70. (write (cdr form) client)
  71. (newline client))
  72. (trc "client not found")))
  73. clients)
  74. (define (accept-new-client)
  75. (let ((new-port (car (accept server))))
  76. ;; Read the client's ID.
  77. (let ((name-form (read new-port)))
  78. ;; Absorb the following newline character.
  79. (read-char new-port)
  80. ;; Check that we have a name form.
  81. (or (eq? (car name-form) 'name)
  82. (error "Invalid name form:" name-form))
  83. ;; Store an association from the connection to the ID.
  84. (set! (connection->id new-port) (cadr name-form))
  85. ;; Pass the name form on to Emacs.
  86. (write-to-ui (cons (connection->id new-port) name-form)))
  87. ;; Add the new connection to the set that we select on.
  88. (cons new-port clients)))
  89. (define (do-read-from-client port)
  90. (trc "reading from client")
  91. (let ((next-char (with-error->eof peek-char port)))
  92. ;;(trc 'next-char next-char)
  93. (cond ((eof-object? next-char)
  94. (write-to-ui (list (connection->id port) 'closed))
  95. (close port)
  96. (delq port clients))
  97. ((char=? next-char #\()
  98. (write-to-ui (cons (connection->id port)
  99. (with-error->eof read port)))
  100. clients)
  101. (else
  102. (with-error->eof read-char port)
  103. clients))))
  104. ;;(trc 'clients clients)
  105. ;;(trc 'readable-sockets readable-sockets)
  106. (if (null? readable-sockets)
  107. (loop clients (car (select (cons (current-input-port)
  108. (cons server clients))
  109. '()
  110. '())))
  111. (loop (do-read (car readable-sockets)) (cdr readable-sockets))))))
  112. ;; What happens if there are multiple copies of Emacs running on the
  113. ;; same machine, and they all try to start up the GDS server? They
  114. ;; can't all listen on the same TCP port, so the short answer is that
  115. ;; all of them except the first will get an EADDRINUSE error when
  116. ;; trying to bind.
  117. ;;
  118. ;; We want to be able to handle this scenario, though, so that Scheme
  119. ;; code can be evaluated, and help invoked, in any of those Emacsen.
  120. ;; So we introduce the idea of a "slave server". When a new GDS
  121. ;; server gets an EADDRINUSE bind error, the implication is that there
  122. ;; is already a GDS server running, so the new server instead connects
  123. ;; to the existing one (by issuing a connect to the GDS port number).
  124. ;;
  125. ;; Let's call the first server the "master", and the new one the
  126. ;; "slave". In principle the master can now proxy any GDS client
  127. ;; connections through to the slave, so long as there is sufficient
  128. ;; information in the protocol for it to decide when and how to do
  129. ;; this.
  130. ;;
  131. ;; The basic information and mechanism that we need for this is as
  132. ;; follows.
  133. ;;
  134. ;; - A unique ID for each Emacs; this can be each Emacs's PID. When a
  135. ;; slave server connects to the master, it announces itself by sending
  136. ;; the protocol (emacs ID).
  137. ;;
  138. ;; - A way for a client to indicate which Emacs it wants to use. At
  139. ;; the protocol level, this is an extra argument in the (name ...)
  140. ;; protocol. (The absence of this argument means "no preference". A
  141. ;; simplistic master server might then decide to use its own Emacs; a
  142. ;; cleverer one might monitor which Emacs appears to be most in use,
  143. ;; and use that one.) At the API level this can be an optional
  144. ;; argument to the `gds-connect' procedure, and the Emacs GDS code
  145. ;; would obviously set this argument when starting a client from
  146. ;; within Emacs.
  147. ;;
  148. ;; We also want a strategy for continuing seamlessly if the master
  149. ;; server shuts down.
  150. ;;
  151. ;; - Each slave server will detect this as an error on the connection
  152. ;; to the master socket. Each server then tries to bind to the GDS
  153. ;; port again (a race which the OS will resolve), and if that fails,
  154. ;; connect again. The result of this is that there should be a new
  155. ;; master, and the others all slaves connected to the new master.
  156. ;;
  157. ;; - Each client will also detect this as an error on the connection
  158. ;; to the (master) server. Either the client should try to connect
  159. ;; again (perhaps after a short delay), or the reconnection can be
  160. ;; delayed until the next time that the client requires the server.
  161. ;; (Probably the latter, all done within `gds-read'.)
  162. ;;
  163. ;; (Historical note: Before this master-slave idea, clients were
  164. ;; identified within gds-server.scm and gds*.el by an ID which was
  165. ;; actually the file descriptor of their connection to the server.
  166. ;; That is no good in the new scheme, because each client's ID must
  167. ;; persist when the master server changes, so we now use the client's
  168. ;; PID instead. We didn't use PID before because the client/server
  169. ;; code was written to be completely asynchronous, which made it
  170. ;; tricky for the server to discover each client's PID and associate
  171. ;; it with a particular connection. Now we solve that problem by
  172. ;; handling the initial protocol exchange synchronously.)
  173. (define (run-slave-server port)
  174. 'not-implemented)