mirror of
https://github.com/deavmi/doap
synced 2024-07-04 15:10:11 +02:00
Merge branch 'master' into feature/timeoutable_gets
This commit is contained in:
commit
2e499d267f
|
@ -111,8 +111,13 @@ public class CoapClient
|
||||||
// Shutdown the messaging layer
|
// Shutdown the messaging layer
|
||||||
this.messaging.close();
|
this.messaging.close();
|
||||||
|
|
||||||
// TODO: We must wake up other sleeprs with an error
|
// Cancel all active request futures
|
||||||
// (somehow, pass it in, flag set)
|
this.requestsLock.lock();
|
||||||
|
foreach(CoapRequest curReq; outgoingRequests)
|
||||||
|
{
|
||||||
|
curReq.future.cancel();
|
||||||
|
}
|
||||||
|
this.requestsLock.unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
module doap.client;
|
module doap.client;
|
||||||
|
|
||||||
public import doap.client.client : CoapClient;
|
public import doap.client.client : CoapClient;
|
||||||
public import doap.client.request : CoapRequestBuilder, CoapRequestFuture;
|
public import doap.client.request : CoapRequestBuilder, CoapRequestFuture, RequestState;
|
|
@ -257,6 +257,29 @@ package class CoapRequestBuilder
|
||||||
import core.sync.mutex : Mutex;
|
import core.sync.mutex : Mutex;
|
||||||
import core.sync.condition : Condition;
|
import core.sync.condition : Condition;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The state of a `CoapRequestFuture`
|
||||||
|
*/
|
||||||
|
public enum RequestState
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* The future has been created
|
||||||
|
*/
|
||||||
|
CREATED,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The future has completed
|
||||||
|
* successfully
|
||||||
|
*/
|
||||||
|
COMPLETED,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The future was cancelled
|
||||||
|
*/
|
||||||
|
CANCELLED
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This is returned to the user when he
|
* This is returned to the user when he
|
||||||
* does a finalizing call on a `CoapRequestBuilder`
|
* does a finalizing call on a `CoapRequestBuilder`
|
||||||
|
@ -277,7 +300,12 @@ public class CoapRequestFuture
|
||||||
* This is filled in by the
|
* This is filled in by the
|
||||||
* messaging layer.
|
* messaging layer.
|
||||||
*/
|
*/
|
||||||
private CoapPacket response;
|
private CoapPacket response; // TODO: Volatility?
|
||||||
|
|
||||||
|
/**
|
||||||
|
* State of the future
|
||||||
|
*/
|
||||||
|
private RequestState state; // TODO: Volatility?
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Mutex (for the condition to use)
|
* Mutex (for the condition to use)
|
||||||
|
@ -298,6 +326,7 @@ public class CoapRequestFuture
|
||||||
*/
|
*/
|
||||||
package this()
|
package this()
|
||||||
{
|
{
|
||||||
|
this.state = RequestState.CREATED;
|
||||||
this.mutex = new Mutex();
|
this.mutex = new Mutex();
|
||||||
this.condition = new Condition(mutex);
|
this.condition = new Condition(mutex);
|
||||||
}
|
}
|
||||||
|
@ -316,6 +345,23 @@ public class CoapRequestFuture
|
||||||
// Set the received response
|
// Set the received response
|
||||||
this.response = response;
|
this.response = response;
|
||||||
|
|
||||||
|
// Set completion state
|
||||||
|
this.state = RequestState.COMPLETED;
|
||||||
|
|
||||||
|
// Wake up the sleepers
|
||||||
|
this.condition.notifyAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Cancels this future such that
|
||||||
|
* all calls to `get()` will
|
||||||
|
* unblock and throw an exception
|
||||||
|
*/
|
||||||
|
package void cancel()
|
||||||
|
{
|
||||||
|
// Set cancelled state
|
||||||
|
this.state = RequestState.CANCELLED;
|
||||||
|
|
||||||
// Wake up the sleepers
|
// Wake up the sleepers
|
||||||
this.condition.notifyAll();
|
this.condition.notifyAll();
|
||||||
}
|
}
|
||||||
|
@ -324,6 +370,8 @@ public class CoapRequestFuture
|
||||||
* Blocks until the response is received
|
* Blocks until the response is received
|
||||||
*
|
*
|
||||||
* Returns: the response as a `CoapPacket`
|
* Returns: the response as a `CoapPacket`
|
||||||
|
* Throws:
|
||||||
|
* CoapException on cancelled request
|
||||||
*/
|
*/
|
||||||
public CoapPacket get()
|
public CoapPacket get()
|
||||||
{
|
{
|
||||||
|
@ -337,7 +385,16 @@ public class CoapRequestFuture
|
||||||
// Upon waking up release lock
|
// Upon waking up release lock
|
||||||
this.mutex.unlock();
|
this.mutex.unlock();
|
||||||
|
|
||||||
return this.response;
|
// If successfully completed
|
||||||
|
if(this.state == RequestState.COMPLETED)
|
||||||
|
{
|
||||||
|
return this.response;
|
||||||
|
}
|
||||||
|
// On error
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw new CoapException("Request future cancelled");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public CoapPacket get(Duration timeout)
|
public CoapPacket get(Duration timeout)
|
||||||
|
@ -361,4 +418,14 @@ public class CoapRequestFuture
|
||||||
throw new CoapException("Timed out whilst waiting");
|
throw new CoapException("Timed out whilst waiting");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the state of this future
|
||||||
|
*
|
||||||
|
* Returns: the state
|
||||||
|
*/
|
||||||
|
public RequestState getState()
|
||||||
|
{
|
||||||
|
return this.state;
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -89,6 +89,10 @@ public class CoapPacket
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIXME: Add options encoding
|
// FIXME: Add options encoding
|
||||||
|
foreach(CoapOption option; orderOptions())
|
||||||
|
{
|
||||||
|
encoded ~= encodeOption(option);
|
||||||
|
}
|
||||||
|
|
||||||
// Set the payload marker
|
// Set the payload marker
|
||||||
encoded ~= PAYLOAD_MARKER;
|
encoded ~= PAYLOAD_MARKER;
|
||||||
|
@ -99,6 +103,20 @@ public class CoapPacket
|
||||||
return encoded;
|
return encoded;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: Make public in the future
|
||||||
|
private static ubyte[] encodeOption(CoapOption option)
|
||||||
|
{
|
||||||
|
// TODO: Implement this
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
private CoapOption[] orderOptions()
|
||||||
|
{
|
||||||
|
// TODO: Implement ordering here
|
||||||
|
return this.options;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public void setType(MessageType type)
|
public void setType(MessageType type)
|
||||||
{
|
{
|
||||||
this.type = type;
|
this.type = type;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user