diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/main/kotlin/Connection.kt (renamed from src/main/kotlin/NinePConnection.kt) | 63 | ||||
-rw-r--r-- | src/main/kotlin/ProtocolTranslator.kt | 8 |
2 files changed, 50 insertions, 21 deletions
diff --git a/src/main/kotlin/NinePConnection.kt b/src/main/kotlin/Connection.kt index 90b734f..7addc10 100644 --- a/src/main/kotlin/NinePConnection.kt +++ b/src/main/kotlin/Connection.kt @@ -3,22 +3,22 @@ import java.math.BigInteger /** * This class represents a 9P connection. It provides a practical implementation with networking to the 9P methods - * described in [NinePTranslator]. Details about methods related to 9P can be found in [NinePTranslator]. Remember to - * disconnect using [disconnect] after use. + * described in [ProtocolTranslator]. Details about methods related to 9P can be found in [ProtocolTranslator]. Remember + * to disconnect using [disconnect] after use. * - * Details about network-related topics can be found in [NetworkPacketTransporter] and the implementation of choice. + * Details about network-related topics can be found in [TransportLayer] and the implementation of choice. * - * Details about 9P messages and methods can be found in [NinePTranslator]. + * Details about 9P messages and methods can be found in [ProtocolTranslator]. * - * @param netPackTrans The networking API backend of choice. + * @param transLay The networking API backend of choice. * - * @throws UnresolvableHostException if the host resolution made by [netPackTrans] failed. + * @throws UnresolvableHostException if the host resolution made by [transLay] failed. */ -class NinePConnection(netPackTrans: NetworkPacketTransporter) : NinePTranslator { +class Connection(transLay: TransportLayer) : ProtocolTranslator { /** * Networking API. */ - private val npt: NetworkPacketTransporter = netPackTrans + private val tl: TransportLayer = transLay /** * Tag generator. @@ -29,7 +29,16 @@ class NinePConnection(netPackTrans: NetworkPacketTransporter) : NinePTranslator * Maximum size for messages negotiated between the client and the server. */ private var maxSize: UInt = 0u + + /** + * Each FID associated with each file. + */ + val fids: Map<UInt, String> = emptyMap() + + // 9P constants. + val DEFAULT_VERSION = "9P2000" val NOTAG = 0.toUShort().inv() + val NOFID = 0.toUInt().inv() /** @@ -38,7 +47,7 @@ class NinePConnection(netPackTrans: NetworkPacketTransporter) : NinePTranslator * @throws IOException if an I/O error occurred while closing the socket. */ fun disconnect() { - this.npt.close() + this.tl.close() } /** @@ -51,7 +60,7 @@ class NinePConnection(netPackTrans: NetworkPacketTransporter) : NinePTranslator * @throws java.io.IOException if the message could not be correctly received. */ private fun readInteger(len: ULong): BigInteger { - val bytes = this.npt.receive(len) + val bytes = this.tl.receive(len) var value = 0.toBigInteger() for (i in 0..<bytes.size) { value += bytes[i].toInt().toBigInteger().shl(i*8) @@ -70,7 +79,7 @@ class NinePConnection(netPackTrans: NetworkPacketTransporter) : NinePTranslator */ private fun readString(): String { val length = readInteger(2u).toShort().toUShort() - val rawString = this.npt.receive(length.toULong()) + val rawString = this.tl.receive(length.toULong()) return String(ByteArray(rawString.size) { i -> rawString[i].toByte() }) } @@ -111,6 +120,8 @@ class NinePConnection(netPackTrans: NetworkPacketTransporter) : NinePTranslator if (ta == tag) { found = true + } else { + // discard? } } return Pair(s, ty ?: NinePMessageType.RERROR) @@ -131,9 +142,9 @@ class NinePConnection(netPackTrans: NetworkPacketTransporter) : NinePTranslator /** * Send 9P request and check for exceptions or an Rerror response. */ - private fun responseError(msg: NinePMessage): String? { + private fun responseError(msg: Message): String? { try { - msg.write(this.npt) + msg.write(this.tl) } catch (ex: Exception) { return "Exception thrown while sending message: (" + ex.javaClass.toString() + ") " + ex.message } @@ -146,7 +157,7 @@ class NinePConnection(netPackTrans: NetworkPacketTransporter) : NinePTranslator } override fun version(msize: UInt, version: String): String? { - val msg = NinePMessage(NinePMessageType.TVERSION, this.NOTAG, listOf("msize", "version"), + val msg = Message(NinePMessageType.TVERSION, this.NOTAG, listOf("msize", "version"), mapOf( "msize" to BigInteger(msize.toString()) ), @@ -159,6 +170,7 @@ class NinePConnection(netPackTrans: NetworkPacketTransporter) : NinePTranslator if (error != null) { return error } + waitForTag(msg.tag) val rmsize = readInteger(4u).toInt().toUInt() val rversion = readString() // this check should not be necessary, but you never know @@ -194,12 +206,26 @@ class NinePConnection(netPackTrans: NetworkPacketTransporter) : NinePTranslator TODO("Not yet implemented") } - override fun read(path: String) { - TODO("Not yet implemented") + override fun read(fid: UInt, offset: ULong, count: UInt): String? { + val msg = Message(NinePMessageType.TREAD, tagGen.generate(), listOf("fid", "offset", "count"), + mapOf( + "fid" to BigInteger(fid.toString()), + "offset" to BigInteger(offset.toString()), + "count" to BigInteger(count.toString()) + ), + emptyMap(), + this.maxSize + ) + val error = responseError(msg) + if (error != null) { + return error + } + + return null } - override fun write(path: String) { - TODO("Not yet implemented") + override fun write(fid: UInt, offset: ULong, count: UInt, data: Iterable<UByte>): String? { + } override fun clunk(path: String) { @@ -217,5 +243,4 @@ class NinePConnection(netPackTrans: NetworkPacketTransporter) : NinePTranslator override fun wstat(path: String) { TODO("Not yet implemented") } - }
\ No newline at end of file diff --git a/src/main/kotlin/ProtocolTranslator.kt b/src/main/kotlin/ProtocolTranslator.kt index bc64002..3dd5d70 100644 --- a/src/main/kotlin/ProtocolTranslator.kt +++ b/src/main/kotlin/ProtocolTranslator.kt @@ -57,9 +57,13 @@ interface ProtocolTranslator { fun create(path: String) /** - * Transfer data from file. + * 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): String? + fun read(fid: UInt, offset: ULong, count: UInt): Pair<String?, Array<UByte>> /** * Transfer data to file. |