import java.io.IOException import java.math.BigInteger /** * This class represents a 9P connection. It provides a practical implementation with networking to the 9P methods * 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 [TransportLayer] and the implementation of choice. * * Details about 9P messages and methods can be found in [ProtocolTranslator]. * * @param transLay The networking API backend of choice. * * @throws UnresolvableHostException if the host resolution made by [transLay] failed. */ class Connection(transLay: TransportLayer) : ProtocolTranslator { /** * Networking API. */ private val tl: TransportLayer = transLay /** * Tag generator. */ private val tagGen = TagGenerator(TagGenerator.TagGenerationMethod.INCREMENTAL, 1u) /** * Maximum size for messages negotiated between the client and the server. */ private var maxSize: UInt = 0u /** * Info about remote paths. */ val pathInfo: PathInfo = PathInfo() // 9P constants. val DEFAULT_VERSION = "9P2000" val NOTAG = 0.toUShort().inv() val NOFID = 0.toUInt().inv() /** * Disconnect from the remote host, * * @throws IOException if an I/O error occurred while closing the socket. */ fun disconnect() { this.tl.close() } /** * Handy function to create an [InMessage] instance and check for errors. After successfully using this function, it * is guaranteed that both no error occurred while reading the incoming message and the message is not of type * R-error. * * It uses [tl] and [maxSize] for instancing the [InMessage] class. * * @return A pair of: (1) a nullable string (which can be: `null` if no error occurred, empty if an error occurred * with no message, or non-empty with the error message) and (2) the optional [InMessage] instance (null if an error * occurred). * @throws InvalidMessageException if the received message is invalid. * @throws RErrorException if the received message is an R-error message. */ private fun checkedInMessage(reqTag: UShort): InMessage { val imsg = InMessage(this.tl, this.maxSize, reqTag) if (imsg.type == NinePMessageType.RERROR) { imsg.applyField(InMessage.Field("ename", InMessage.Field.Type.STRING, 0u)) throw RErrorException(imsg.fieldsStr["ename"]) } return imsg } override fun version(msize: UInt, version: String) { val omsg = OutMessage(NinePMessageType.TVERSION, this.NOTAG, listOf("msize", "version"), mapOf( "msize" to BigInteger(msize.toString()) ), mapOf( "version" to version ), emptyMap(), this.maxSize ) omsg.write(this.tl) val imsg = checkedInMessage(omsg.tag) imsg.applySchema(listOf( InMessage.Field("msize", InMessage.Field.Type.INTEGER, 4u), InMessage.Field("version", InMessage.Field.Type.STRING, 0u) )) val remoteMaxSize = imsg.fieldsInt["msize"]!!.toInt().toUInt() if (remoteMaxSize > this.maxSize) { throw MsizeValueTooBigException(msize, remoteMaxSize) } this.maxSize = remoteMaxSize if (!imsg.fieldsStr["version"]!!.startsWith("9P2000")) { throw UnknownVersionException(imsg.fieldsStr["version"]!!) } } override fun auth(afid: UInt, uname: String, aname: String) { // TODO: Leave this unimplemented until p9any and TLS are implemented TODO("Not yet implemented") } override fun flush(oldtag: UShort): String? { val omsg = OutMessage(NinePMessageType.TFLUSH, this.tagGen.generate(), listOf("oldtag"), mapOf( "oldtag" to BigInteger(oldtag.toString()) ), emptyMap(), emptyMap(), this.maxSize ) omsg.write(this.tl) val checkErr = checkedInMessage(omsg.tag) if (checkErr.first != null) { return checkErr.first } return null } override fun attach(fid: UInt, afid: UInt, uname: String, aname: String): String? { val omsg = OutMessage(NinePMessageType.TATTACH, this.tagGen.generate(), listOf("fid", "afid", "uname", "aname"), mapOf( "fid" to BigInteger(fid.toString()), "afid" to BigInteger(afid.toString()) ), mapOf( "uname" to uname, "aname" to aname ), emptyMap(), this.maxSize ) omsg.write(this.tl) val checkErr = checkedInMessage(omsg.tag) if (checkErr.first != null) { return checkErr.first } val imsg = checkErr.second!! imsg.applyField(InMessage.Field("qid", InMessage.Field.Type.RAW, 13u)) val qid = QID(imsg.fieldsRaw["qid"]!!.toList()) this.pathInfo.addPath(PathInfo.Path(aname.split("/"), fid, qid)) return null } override fun walk(fid: UInt, newfid: UInt, wname: List): String? { val nwname = wname.size val omsg = OutMessage(NinePMessageType.TWALK, this.tagGen.generate(), listOf("fid", "newfid", "nwname"), mapOf( "fid" to BigInteger(fid.toString()), "newfid" to BigInteger(newfid.toString()), "nwname" to BigInteger(nwname.toString()) ), emptyMap(), emptyMap(), this.maxSize ) omsg.write(this.tl) for (wn in wname) { // write wname elements OutMessage.writeString(this.tl, wn) } val checkErr = checkedInMessage(omsg.tag) if (checkErr.first != null) { return checkErr.first } val imsg = checkErr.second!! imsg.applyField(InMessage.Field("nwqid", InMessage.Field.Type.INTEGER, 2u)) val nwqid = imsg.fieldsInt["nwqid"]!!.toInt() if (nwqid < nwname) { return "cannot access ${wname.slice(0..nwqid).joinToString(separator = "/")}" } imsg.applyField(InMessage.Field("qids", InMessage.Field.Type.RAW, (nwqid * 13).toUInt())) val rawQids = imsg.fieldsRaw["qids"]!! val qids: MutableList = mutableListOf() for (i in 0..> { val omsg = OutMessage(NinePMessageType.TREAD, this.tagGen.generate(), listOf("fid", "offset", "count"), mapOf( "fid" to BigInteger(fid.toString()), "offset" to BigInteger(offset.toString()), "count" to BigInteger(count.toString()) ), emptyMap(), emptyMap(), this.maxSize ) omsg.write(this.tl) val checkErr = checkedInMessage(omsg.tag) if (checkErr.first != null) { return Pair(checkErr.first, emptyArray()) } val imsg = checkErr.second!! imsg.applyField(InMessage.Field("count", InMessage.Field.Type.INTEGER, 4u)) val count = imsg.fieldsInt["count"]!!.toInt().toUInt() imsg.applyField(InMessage.Field("data", InMessage.Field.Type.RAW, count)) return Pair(null, imsg.fieldsRaw["data"]!!) } override fun write(fid: UInt, offset: ULong, count: UInt, data: Iterable): Pair { val data = data.take(count.toInt()).toTypedArray() val omsg = OutMessage(NinePMessageType.TWRITE, this.tagGen.generate(), listOf("offset", "count", "data"), mapOf( "offset" to BigInteger(offset.toString()), "count" to BigInteger(count.toString()), ), emptyMap(), mapOf( "data" to data ), this.maxSize ) omsg.write(this.tl) val checkErr = checkedInMessage(omsg.tag) if (checkErr.first != null) { return Pair(checkErr.first, 0u) } val imsg = checkErr.second!! imsg.applyField(InMessage.Field("count", InMessage.Field.Type.INTEGER, 4u)) val count = imsg.fieldsInt["count"]!! return Pair(null, count.toInt().toUInt()) } override fun clunk(path: String) { TODO("Not yet implemented") } override fun remove(path: String) { TODO("Not yet implemented") } override fun stat(path: String) { TODO("Not yet implemented") } override fun wstat(path: String) { TODO("Not yet implemented") } }