diff options
Diffstat (limited to 'src/main/kotlin')
-rw-r--r-- | src/main/kotlin/NinePConnection.kt | 19 | ||||
-rw-r--r-- | src/main/kotlin/NinePTranslator.kt | 6 |
2 files changed, 14 insertions, 11 deletions
diff --git a/src/main/kotlin/NinePConnection.kt b/src/main/kotlin/NinePConnection.kt index 535f20e..5a09e8b 100644 --- a/src/main/kotlin/NinePConnection.kt +++ b/src/main/kotlin/NinePConnection.kt @@ -21,6 +21,9 @@ class NinePConnection(netPackTrans: NetworkPacketTransporter) : NinePTranslator */ val npt: NetworkPacketTransporter = netPackTrans + val NOTAG = 0.inv().toUShort() + + /** * Disconnect from the remote host, * @@ -153,26 +156,27 @@ class NinePConnection(netPackTrans: NetworkPacketTransporter) : NinePTranslator } /** - * Write a message of the given fields. + * Write a message with the given type, tag, and fields. The message size is calculated automatically. * - * Important note: the field names in [fieldValuesInt] and [fieldValuesStr] (i.e. the keys of their maps) are - * mutually exclusive and the union of these two sets must result exactly in the set of field names listed in + * Important note: the field names in [fieldValuesInt] and [fieldValuesStr] (i.e. the keys of their maps) must be + * mutually exclusive and the union of these two maps' keys must result in a subset of (or a set equal to) * [fieldNames]. * * @param type The 9P message type. + * @param tag The tag given to the message. * @param fieldNames The names of the message fields, in the same order they are expected to be sent. * @param fieldValuesInt A map of each field name into its value. This map only stores integer values. * @param fieldValuesStr A map of each field name into its value. This map only stores string values. * @throws IllegalArgumentException if [fieldNames], [fieldValuesInt], and [fieldValuesStr] are incoherent. */ - private fun writeMessage(type: NinePMessageType, fieldNames: List<String>, fieldValuesInt: Map<String, SizedMessageField>, fieldValuesStr: Map<String, String>) { + private fun writeMessage(type: NinePMessageType, tag: UShort, fieldNames: List<String>, fieldValuesInt: Map<String, SizedMessageField>, fieldValuesStr: Map<String, String>) { // shorthands val insecInts = fieldNames.intersect(fieldValuesInt.keys) val insecStrs = fieldNames.intersect(fieldValuesStr.keys) // check that names in fieldNames exist as keys in either fieldValuesInt or fieldValuesStr but not both require(insecInts.size == fieldNames.size - insecStrs.size) - val totalSize = 4 + 1 + insecInts.sumOf { fieldValuesInt[it]!!.size } + insecStrs.sumOf { 2 + fieldValuesStr[it]!!.length } + val totalSize = 4 + 1 + 2 + insecInts.sumOf { fieldValuesInt[it]!!.size } + insecStrs.sumOf { 2 + fieldValuesStr[it]!!.length } writeMessageSizeType(totalSize, type) for (field in fieldNames) { @@ -184,10 +188,9 @@ class NinePConnection(netPackTrans: NetworkPacketTransporter) : NinePTranslator } } - override fun version(tag: SizedMessageField, msize: SizedMessageField, version: String): String? { - writeMessage(NinePMessageType.TVERSION, listOf("tag", "msize", "version"), + override fun version(msize: SizedMessageField, version: String): String? { + writeMessage(NinePMessageType.TVERSION, this.NOTAG, listOf("msize", "version"), mapOf( - "tag" to tag, "msize" to msize ), mapOf( diff --git a/src/main/kotlin/NinePTranslator.kt b/src/main/kotlin/NinePTranslator.kt index 0bbcb9d..ebb04c5 100644 --- a/src/main/kotlin/NinePTranslator.kt +++ b/src/main/kotlin/NinePTranslator.kt @@ -7,6 +7,8 @@ TODO: * The [NinePTranslator] interface provides methods that coincide 1:1 with each request and response type in the 9P * protocol, except for `Rerror` which is handled at occurrence. * + * Tags are supposed to be managed internally by the class that provides the implementation. + * * Trivia: comments for each method are taken from each message type's manual page in section 5. */ interface NinePTranslator { @@ -15,15 +17,13 @@ interface NinePTranslator { * * This must be the first message sent on the 9P connection and no other requests can be issued until a response has * been received. - * Tag should be NOTAG ((ushort)~0). * - * @param tag Should be NOTAG ((ushort)~0). * @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. * @return a possible error. */ - fun version(tag: SizedMessageField, msize: SizedMessageField, version: String): String? + fun version(msize: SizedMessageField, version: String): String? /** * Perform authentication. |