mirror of
https://github.com/deavmi/doap
synced 2024-07-04 14:10:47 +02:00
CoapRequestFuture
- Added `RequestState` enum - State if `CREATED` on construction - Calling `receiveWake(CoapPacket)` now sets the state to `COMPLETED` - Calling `cancel()` now sets the state to `CANCELLED`
This commit is contained in:
parent
8a1b1f8ea1
commit
ae0161860a
|
@ -229,6 +229,14 @@ package class CoapRequestBuilder
|
||||||
import core.sync.mutex : Mutex;
|
import core.sync.mutex : Mutex;
|
||||||
import core.sync.condition : Condition;
|
import core.sync.condition : Condition;
|
||||||
|
|
||||||
|
|
||||||
|
private enum RequestState
|
||||||
|
{
|
||||||
|
CREATED,
|
||||||
|
COMPLETED,
|
||||||
|
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`
|
||||||
|
@ -249,7 +257,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)
|
||||||
|
@ -270,6 +283,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);
|
||||||
}
|
}
|
||||||
|
@ -288,15 +302,22 @@ public class CoapRequestFuture
|
||||||
// Set the received response
|
// Set the received response
|
||||||
this.response = response;
|
this.response = response;
|
||||||
|
|
||||||
// TODO: Set state as DONE
|
// Set completion state
|
||||||
|
this.state = RequestState.COMPLETED;
|
||||||
|
|
||||||
// Wake up the sleepers
|
// Wake up the sleepers
|
||||||
this.condition.notify();
|
this.condition.notify();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Cancels this future such that
|
||||||
|
* all calls to `get()` will
|
||||||
|
* unblock and throw an exception
|
||||||
|
*/
|
||||||
package void cancel()
|
package void cancel()
|
||||||
{
|
{
|
||||||
// TODO: Set state as cancelled
|
// Set cancelled state
|
||||||
|
this.state = RequestState.CANCELLED;
|
||||||
|
|
||||||
// Wake up the sleepers
|
// Wake up the sleepers
|
||||||
this.condition.notifyAll();
|
this.condition.notifyAll();
|
||||||
|
@ -306,6 +327,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()
|
||||||
{
|
{
|
||||||
|
@ -319,7 +342,18 @@ 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");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user