From 726f9a4b0c3a2c21209abd143c1a4a5cabac5dc7 Mon Sep 17 00:00:00 2001 From: Gavin Norman Date: Mon, 8 Jan 2018 12:18:22 +0100 Subject: [PATCH] Adapt Mirror to use new SupportedCode from swarm Part of #78. --- src/dhtproto/client/request/internal/Mirror.d | 42 ++++++++++--------- src/dhtproto/common/Mirror.d | 20 +-------- src/dhtproto/node/neo/request/Mirror.d | 2 +- src/fakedht/neo/request/Mirror.d | 9 +++- 4 files changed, 34 insertions(+), 39 deletions(-) diff --git a/src/dhtproto/client/request/internal/Mirror.d b/src/dhtproto/client/request/internal/Mirror.d index 7dcfc695..2df6abf9 100644 --- a/src/dhtproto/client/request/internal/Mirror.d +++ b/src/dhtproto/client/request/internal/Mirror.d @@ -163,6 +163,7 @@ private scope class MirrorHandler import swarm.neo.client.mixins.SuspendableRequestCore; import swarm.neo.request.RequestEventDispatcher; import swarm.neo.util.MessageFiber; + import swarm.neo.request.Command; import dhtproto.common.Mirror; import dhtproto.client.request.Mirror; @@ -215,7 +216,7 @@ private scope class MirrorHandler { auto request = createSuspendableRequest!(Mirror)(this.conn, this.context, &this.connect, &this.disconnected, &this.fillPayload, - &this.handleStatusCode, &this.handle); + &this.handleSupportedCode, &this.handle); request.run(); } @@ -276,36 +277,39 @@ private scope class MirrorHandler /*************************************************************************** HandleStatusCode policy, called from SuspendableRequestInitialiser - template to decide how to handle the status code received from the node. + template to decide how to handle the supported code received from the + node. Params: - status = status code received from the node in response to the + code = supported code received from the node in response to the initial message Returns: - true to continue handling the request (OK status); false to abort - (error status) + true to continue handling the request (supported); false to abort + (unsupported) ***************************************************************************/ - private bool handleStatusCode ( ubyte status ) + private bool handleSupportedCode ( ubyte code ) { - auto getall_status = cast(RequestStatusCode)status; - - if ( Mirror.handleGlobalStatusCodes(getall_status, this.context, - this.conn.remote_address) ) - return false; // Global code, e.g. request/version not supported + auto supported = cast(SupportedStatus)code; + if ( !Mirror.handleSupportedCodes(supported, + this.context, this.conn.remote_address) ) + { + return false; // Request/version not supported + } - // Mirror-specific codes - with ( RequestStatusCode ) switch ( getall_status ) + // Handle initial started/error message from node. + auto msg = conn.receiveValue!(MessageType)(); + with ( MessageType ) switch ( msg ) { case Started: - // Expected "request started" code - return true; + // Expected "request started" code. Continue handling request. + break; case Error: - // The node returned an error code. Notify the user and - // end the request. + // The node returned an error code. Notify the user and end the + // request. Mirror.Notification n; n.node_error = RequestNodeInfo( this.context.request_id, conn.remote_address); @@ -313,11 +317,11 @@ private scope class MirrorHandler return false; default: - // Treat unknown codes as internal errors. + // Treat unknown/unexpected codes as node errors. goto case Error; } - assert(false); + return true; } /*************************************************************************** diff --git a/src/dhtproto/common/Mirror.d b/src/dhtproto/common/Mirror.d index 8ad0cc22..c3c2df37 100644 --- a/src/dhtproto/common/Mirror.d +++ b/src/dhtproto/common/Mirror.d @@ -14,24 +14,6 @@ module dhtproto.common.Mirror; import swarm.neo.request.Command; -/******************************************************************************* - - Status code enum. Sent from the node to the client. - -*******************************************************************************/ - -public enum RequestStatusCode : StatusCode -{ - /// Invalid, default value - None, - - /// Mirror started - Started, - - /// Internal node error occurred - Error -} - /******************************************************************************* Message type enum. Each message sent between the client and the node as part @@ -53,6 +35,8 @@ public enum MessageType : ubyte // sent by the client to acknowledge the request finishing // Message types sent from the node to the client: + Started, // Request handling beginning + Error, // Internal node error occurred; request not being handled RecordChanged, // Sent by the node when it sends a record value RecordDeleted, // Sent by the node when it sends the key of a deleted record RecordRefreshBatch, // Sent by the node when it sends a batch of refreshed diff --git a/src/dhtproto/node/neo/request/Mirror.d b/src/dhtproto/node/neo/request/Mirror.d index 17f549e6..3dd96f2b 100644 --- a/src/dhtproto/node/neo/request/Mirror.d +++ b/src/dhtproto/node/neo/request/Mirror.d @@ -383,7 +383,7 @@ public abstract scope class MirrorProtocol_v0 ( RequestOnConnBase.EventDispatcher.Payload payload ) { payload.addConstant(ok - ? RequestStatusCode.Started : RequestStatusCode.Error); + ? MessageType.Started : MessageType.Error); } ); diff --git a/src/fakedht/neo/request/Mirror.d b/src/fakedht/neo/request/Mirror.d index 989c13b6..2dbe870d 100644 --- a/src/fakedht/neo/request/Mirror.d +++ b/src/fakedht/neo/request/Mirror.d @@ -45,16 +45,23 @@ public void handle ( Object shared_resources, RequestOnConn connection, Command.Version cmdver, Const!(void)[] msg_payload ) { auto resources = new SharedResources; + auto ed = connection.event_dispatcher; switch ( cmdver ) { case 0: + ed.send( + ( ed.Payload payload ) + { + payload.addConstant(SupportedStatus.RequestSupported); + } + ); + scope rq = new MirrorImpl_v0(resources); rq.handle(connection, msg_payload); break; default: - auto ed = connection.event_dispatcher; ed.send( ( ed.Payload payload ) {