Skip to content

Instantly share code, notes, and snippets.

View odeheurles's full-sized avatar

Olivier Deheurles odeheurles

View GitHub Profile
public interface IRfq : IDisposable
{
void RequestQuote(IQuoteRequest quoteRequest);
void Cancel(long rfqId);
void Execute(IExecutionRequest quote);
IObservable<RfqUpdate> Updates { get; }
}
@odeheurles
odeheurles / ErrorHandling
Created June 25, 2014 10:48
Error handling
_stateMachine.OnUnhandledTrigger(OnUnhandledTrigger);
private void OnUnhandledTrigger(RfqState state, RfqEvent trigger)
{
var message = string.Format("State machine received an invalid trigger '{0}' in state '{1}'", trigger, state);
Console.WriteLine(message);
_rfqUpdateSubject.OnError(new ApplicationException(message));
}
@odeheurles
odeheurles / DefiningActions
Created June 25, 2014 10:47
Defining actions
_stateMachine.Configure(RfqState.Requesting)
.OnEntry(LogTransition)
.OnEntryFrom(_rfqEventUserRequests, OnEntryRequesting)
.Permit(RfqEvent.ServerNewQuote, RfqState.Quoted)
.Permit(RfqEvent.UserCancels, RfqState.Cancelling)
.Permit(RfqEvent.InternalError, RfqState.Error);
private void OnEntryRequesting(IQuoteRequest quoteRequest)
{
// here goes the code to send a quote request to the server
@odeheurles
odeheurles / FireEvent
Created June 25, 2014 10:45
Fire an event to the state machine
// for an event without parameters
_stateMachine.Fire(RfqEvent.ServerQuoteStreamComplete)
// for a strongly typed event
_stateMachine.Fire(_rfqEventServerSendsExecutionReport, executionReport)
@odeheurles
odeheurles / RFQTransitions
Created June 21, 2014 07:51
Defining RFQ Transitions
_stateMachine.Configure(RfqState.Input)
.Permit(RfqEvent.UserRequests, RfqState.Requesting);
_stateMachine.Configure(RfqState.Requesting)
.Permit(RfqEvent.ServerNewQuote, RfqState.Quoted)
.Permit(RfqEvent.UserCancels, RfqState.Cancelling)
.Permit(RfqEvent.InternalError, RfqState.Error);
_stateMachine.Configure(RfqState.Quoted)
.PermitReentry(RfqEvent.ServerNewQuote)
@odeheurles
odeheurles / StronglyTypedEvent
Created June 21, 2014 07:50
Defining a strongly typed event
_rfqEventServerSendsQuote = _stateMachine.SetTriggerParameters<IQuote>(RfqEvent.ServerNewQuote);
@odeheurles
odeheurles / RfqEvent
Created June 21, 2014 07:49
Possible RFQ Events
public enum RfqEvent
{
UserRequests,
UserCancels,
UserExecutes,
ServerNewQuote,
ServerQuoteError,
ServerQuoteStreamComplete,
ServerSendsExecutionReport,
@odeheurles
odeheurles / RFQStates
Created June 21, 2014 07:47
Possible states for a RFQ
public enum RfqState
{
Input,
Requesting,
Cancelling,
Cancelled,
Quoted,
Executing,
Error,
Done
public unsafe int Int32GetLittleEndian(int index)
{
return *(int*)(_pBuffer + index);
}
public int getInt(final int index, final ByteOrder byteOrder)
{
int bits = UNSAFE.getInt(byteArray, baseOffset + index);
if (NATIVE_BYTE_ORDER != byteOrder)
{
bits = Integer.reverseBytes(bits);
}
return bits;
}