summaryrefslogtreecommitdiff
path: root/src/main/kotlin/ProtocolTranslator.kt
blob: 1b452521deefd44b4ecff9e7ecf39ccdcdd6e25b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
TODO:
    - add arguments to methods
    - switch from returned strings to exceptions
*/

/**
 * The [ProtocolTranslator] interface provides methods that coincide 1:1 with each request type in the 9P protocol.
 * Every method that can fail, that is, every request that can receive a response with `Rerror` type instead of the same
 * type as itself, returns a non-null `String` that contains the error message received in the response.
 *
 * Tags, FIDs, QIDs, and the `msize` value are supposed to be managed internally by the implementing class.
 *
 * When compared to 9P's formal message descriptions, like those which can be read in Plan 9's manual pages, some of the
 * methods might lack parameters. Those which can be inferred from the existing parameters are purposefully omitted. An
 * example is [walk], which omits `nwname` because it can be obtained by calculating the size of `wname`.
 *
 * Trivia: comments for each method are taken from each message type's manual page in section 5.
 */
interface ProtocolTranslator {
    /**
     * Negotiate protocol version.
     *
     * This must be the first message sent on the 9P connection and no other requests can be issued until a response has
     * been received.
     *
     * @param msize The maximum length, in bytes, that the client will ever generate or expect to receive in a single
     *              9P message.
     * @param version Should be "9P2000", which is the only defined value.
     * @throws except.InvalidMessageException if the received message is invalid.
     * @throws except.RErrorException if the received message is an R-error message.
     * @throws except.MsizeValueTooBigException if the received `msize` value is bigger than what the client requested.
     * @throws except.UnknownVersionException if the version negotiation failed.
     */
    fun version(msize: UInt, version: String)

    /**
     * Perform authentication.
     */
    fun auth(afid: UInt, uname: String, aname: String)

    /**
     * Abort a message.
     *
     * @param oldtag The tag of the message that needs to be purged.
     * @throws except.InvalidMessageException if the received message is invalid.
     * @throws except.RErrorException if the received message is an R-error message.
     */
    fun flush(oldtag: UShort)

    /**
     * Establish a connection.
     *
     * @param fid FID that represents the root directory of the desired file tree.
     * @param afid A FID previously established by an auth message.
     * @param uname A user identifier.
     * @param aname The desired file tree to access.
     * @throws except.InvalidMessageException if the received message is invalid.
     * @throws except.RErrorException if the received message is an R-error message.
     */
    fun attach(fid: UInt, afid: UInt, uname: String, aname: String)

    /**
     * Descend a directory hierarchy.
     *
     * @param fid The existing FID. It must represent a directory.
     * @param newfid The proposed FID, which is going to be associated with the result of walking the hierarchy. It must
     * be not in use, unless it is the same as [fid].
     * @param wname The successive path name elements which describe the file to walk to.
     * @return a possible error.
     */
    fun walk(fid: UInt, newfid: UInt, wname: List<String>): String?

    /**
     * Prepare an FID for I/O on an existing file.
     */
    fun open(path: String)

    /**
     * Prepare an FID for I/O on a new file.
     */
    fun create(path: String)

    /**
     * Transfer data from file. Due to the negotiated maximum size of 9P messages, called `msize`, one is supposed to
     * call this method multiple times, unless the content is smaller than `msize`.
     *
     * @return A pair of: (1) an optional error message and (2) the content read with the call just made. If the string
     * is not null, an error occurred and the array is going to be empty.
     */
    fun read(fid: UInt, offset: ULong, count: UInt): Pair<String?, Array<UByte>>

    /**
     * Transfer data to file. Due to the negotiated maximum size of 9P messages, called `msize`, one is supposed to
     * call this method multiple times, unless the content is smaller than `msize`.
     *
     * @param fid The FID to write to.
     * @param offset The distance between the beginning of the file and the first written byte.
     * @param data The raw bytes that are going to be written.
     * @return A pair of: (1) an optional error message and (2) the amount of bytes written with the call just made. If
     * the string is not null, an error occurred and the amount of written bytes is going to be zero.
     */
    fun write(fid: UInt, offset: ULong, count: UInt, data: Iterable<UByte>): Pair<String?, UInt>

    /**
     * Forget about an FID.
     */
    fun clunk(path: String)

    /**
     * Remove a file from a server.
     */
    fun remove(path: String)

    /**
     * Inquire file attributes.
     */
    fun stat(path: String)

    /**
     * Change file attributes.
     */
    fun wstat(path: String)
}