Skip to content

Commit

Permalink
Adapt Mirror to use new SupportedCode from swarm
Browse files Browse the repository at this point in the history
  • Loading branch information
Gavin Norman committed Jan 10, 2018
1 parent 3777b13 commit 726f9a4
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 39 deletions.
42 changes: 23 additions & 19 deletions src/dhtproto/client/request/internal/Mirror.d
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -276,48 +277,51 @@ 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);
Mirror.notify(this.context.user_params, n);
return false;

default:
// Treat unknown codes as internal errors.
// Treat unknown/unexpected codes as node errors.
goto case Error;
}

assert(false);
return true;
}

/***************************************************************************
Expand Down
20 changes: 2 additions & 18 deletions src/dhtproto/common/Mirror.d
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/dhtproto/node/neo/request/Mirror.d
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
);

Expand Down
9 changes: 8 additions & 1 deletion src/fakedht/neo/request/Mirror.d
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
{
Expand Down

0 comments on commit 726f9a4

Please sign in to comment.