Documentation
¶
Overview ¶
Package ipc hands the command line of a second launch over to the running instance through a Unix socket (SPEC §10.11).
The first Emetos process of a user listens on a socket (Locations); a later launch connects, sends one versioned JSON message that holds its working directory and what it was asked to open, and exits with the exit code of the reply, which with --wait comes only when the files are closed. Join decides which of the two a process is, under a lock file, so that two launches at once end as one instance and one client. This is the only package that may import net (SPEC §12), and it opens Unix sockets only.
Index ¶
Constants ¶
const ( // DefaultIOTimeout is how long a message may take to be read or written (a request has to arrive // at once; the wait of --wait has no limit). DefaultIOTimeout = 5 * time.Second // DefaultLockTimeout is how long a launch waits for the lock that another launch holds while it decides. DefaultLockTimeout = 10 * time.Second )
Default timeouts.
const ( // Version is the version of the protocol. A server answers a message of another version with an error. Version = 1 // MaxMessageSize is the largest message, in bytes, a server or a client accepts (without the line end). MaxMessageSize = 1 << 20 )
const ( // TypeOpen is the request of a launch: open what the arguments say. TypeOpen = "open" // TypeOK is the reply to a request that is carried out and not waited for. TypeOK = "ok" // TypeDone is the reply to a request that waited (--wait): what it opened is closed. TypeDone = "done" // TypeError is the reply to a request that failed; Message says why. TypeError = "error" )
The types of messages.
Variables ¶
This section is empty.
Functions ¶
func Join ¶
Join decides what this process is. Under the lock file, so that two launches at once cannot both decide the same, it tries the socket: when an instance answers (accepts the connection), the result is a Client for it and no Server; when nobody does it removes a stale socket file (the file of an instance that died), listens, and the result is a Server and no Client. The lock is released before Join returns, and as soon as the server listens a launch that connects is queued until Serve accepts it, so there is no window in which the second launch finds nothing. The caller closes the Server (which removes the socket) or the Client.
The tags are those of Locations, IPCSocketPathInvalid, IPCSocketPathTooLong, IPCLockFailed, IPCLockTimeout, IPCDialFailed, IPCSocketBusy (the path exists and is not a socket, or not the user's), IPCStaleSocketRemoveFailed and IPCListenFailed.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is a connection to the running instance.
func Dial ¶
Dial connects to the instance that listens at loc, without the lock and without ever becoming a server (tests and tools; a launch uses Join). The tag is IPCDialFailed.
func (*Client) Send ¶
Send sends the request and returns the reply of the instance. A request with no Version, Type or PID gets them (Version, TypeOpen, the process id). A request that does not wait (Open.Wait false) gets its reply within the I/O timeout; one that waits has no limit but ctx, whose end closes the connection (the instance then stops waiting). The reply can be an error reply (TypeError): that is a Reply, not an error here. The tags are IPCSendFailed and IPCReplyFailed (the deadline of the connection could not be set, or the context ended), those of writeMessage and readMessage (IPCWriteFailed, IPCMessageTooLarge, IPCReadFailed, IPCConnectionClosed, IPCMessageInvalid: the connection ended or the reply did not come in time), IPCReplyInvalid and IPCVersionUnsupported (the reply is of another version).
type FileTarget ¶
type FileTarget struct {
// Path is the absolute path of the file.
Path string `json:"path"`
// Line and Column count from 1; zero is no position.
Line int `json:"line,omitempty"`
Column int `json:"column,omitempty"`
}
FileTarget is a file to open and the position to show in it.
type Handler ¶
Handler carries out the request of a client and gives the reply. ctx ends when the server stops or when the client goes away (killed, or it closed the connection), so a handler that waits (--wait) stops waiting. It runs on a goroutine of its own, concurrently with the handlers of other clients, and must not touch what belongs to the UI thread directly.
type Location ¶
type Location struct {
// Socket is the path of the Unix socket.
Socket string
// Lock is the path of the lock file that is held while a launch decides whether it is the instance.
Lock string
}
Location is where the socket of the instance and its lock file are.
func Locations ¶
Locations gives the socket and the lock file of the user uid: $XDG_RUNTIME_DIR/emetos-<uid>.sock and emetos-<uid>.lock (SPEC §10.11). Without a runtime folder (the variable is unset) they are in <temporary folder>/emetos-<uid>/, a folder that is made with mode 0700 and that must belong to the user and be private, as ssh does for its agent socket. The tags are IPCRuntimeDirInvalid (the folder cannot be made, is not the user's or not private) and IPCSocketPathTooLong (the path does not fit in the address of a Unix socket).
type OpenArgs ¶
type OpenArgs struct {
// Folder is the folder to open as the workspace, or empty.
Folder string `json:"folder,omitempty"`
// Files are the files to open, in order; the first is the one that is shown.
Files []FileTarget `json:"files,omitempty"`
// Diff holds the two paths of --diff, or none.
Diff []string `json:"diff,omitempty"`
// Merge holds the four paths of --merge (the two sides, the base and the result), or none.
Merge []string `json:"merge,omitempty"`
// Wait asks the reply to come only when the files that were opened are closed (--wait).
Wait bool `json:"wait,omitempty"`
// NewWindow is --new-window and ReuseWindow is --reuse-window.
NewWindow bool `json:"newWindow,omitempty"`
ReuseWindow bool `json:"reuseWindow,omitempty"`
// Profile is the name given to --profile, or empty.
Profile string `json:"profile,omitempty"`
// Tutor is --tutor: open a fresh practice copy of the Emetos Tutor in a new tab (SPEC §10.35). It comes with no
// folder and no files.
Tutor bool `json:"tutor,omitempty"`
}
OpenArgs are the arguments of a launch that the instance carries out (SPEC §10.11). Every path is absolute.
type Options ¶
type Options struct {
// Location is the socket and the lock file (Locations).
Location Location
// UID is the user that owns the socket: only a peer of this user is served. It is os.Getuid() when zero
// and UIDSet is false.
UID int
UIDSet bool
// Logger gets the debug and warning lines of the server; nothing when nil.
Logger *slog.Logger
// OnError is told of the failures of one connection (a refused peer, a bad message), which do not stop the
// server. Optional.
OnError func(err error)
// Credentials gives the user id of the peer of a connection; the SO_PEERCRED of the socket when nil.
// Tests replace it.
Credentials func(conn *net.UnixConn) (uid int, err error)
// IOTimeout is DefaultIOTimeout when zero, and LockTimeout is DefaultLockTimeout.
IOTimeout, LockTimeout time.Duration
}
Options say where the instance listens and how a server and a client behave. The zero value of every field but Location is the default.
type Reply ¶
type Reply struct {
// Version is the protocol version (Version).
Version int `json:"version"`
// Type is TypeOK, TypeDone or TypeError.
Type string `json:"type"`
// ExitCode is the exit code the client ends with.
ExitCode int `json:"exitCode"`
// Message is the text for the user (an error, a notice), or empty.
Message string `json:"message,omitempty"`
}
Reply is the message of the instance to a client.
type Request ¶
type Request struct {
// Version is the protocol version (Version).
Version int `json:"version"`
// Type is TypeOpen.
Type string `json:"type"`
// PID is the process id of the client, for the log of the instance.
PID int `json:"pid"`
// Cwd is the working directory of the client. The paths in Open are made absolute against it already;
// it is handed over for the instance's log and for what a later version resolves.
Cwd string `json:"cwd"`
// Open says what to open; it is required by TypeOpen.
Open *OpenArgs `json:"open,omitempty"`
}
Request is the message of a client to the instance: one JSON object on one line.
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server is the instance's end of the socket. Make one with Join.
func (*Server) Close ¶
Close stops listening, ends the connections that are open and removes the socket file, but only if the file is still the one this server made (a later instance may have replaced it). It is safe to call more than once and from the goroutine of Serve's context; the error is IPCCloseFailed.
func (*Server) Serve ¶
Serve accepts clients and gives their requests to h until ctx ends or Close is called. It then closes the server (Close) and returns its result: the handlers in flight are told to stop through their context and may still give a reply, which is written when it comes in time. Each client is served on a goroutine of its own. The tag of a failure of the listener is IPCAcceptFailed.