void outputEnvelope ( FudgeMsgEnvelope envelope ) { printf ( "Schema version: %d\nTaxonomy: %d\nMessage:\n{\n", ( int ) FudgeMsgEnvelope_getSchemaVersion ( envelope ), ( int ) FudgeMsgEnvelope_getTaxonomy ( envelope ) ); outputMessage ( FudgeMsgEnvelope_getMessage ( envelope ), 0 ); printf ( "}\n" ); }
// This must only be called from the thread that creates and connects the pipes. This then // doesn't need to acquire the pipe semaphore as the object won't be modified concurrently. // Another thread might be sending, but that's it. bool CClientService::DispatchAndRelease (FudgeMsgEnvelope env) { FudgeMsg msg = FudgeMsgEnvelope_getMessage (env); switch (FudgeMsgEnvelope_getDirectives (env)) { case MESSAGE_DIRECTIVES_CLIENT : { Operation op; if (ConnectorMessage_getOperation (msg, &op) == FUDGE_OK) { switch (op) { case HEARTBEAT : LOGDEBUG (TEXT ("Heartbeat received")); break; case POISON : // This shouldn't be sent by the Java stack LOGFATAL (TEXT ("Received poison from Java framework")); assert (0); break; case STASH : { FudgeMsg msgStash; if (ConnectorMessage_getStash (msg, &msgStash) == FUDGE_OK) { LOGDEBUG (TEXT ("Storing stash message")); m_oStateMutex.Enter (); if (m_poRunner) { m_poRunner->SetStash (msgStash); } else { LOGWARN (TEXT ("No runner thread")); } m_oStateMutex.Leave (); FudgeMsg_release (msgStash); } else { LOGWARN (TEXT ("No stash message attached")); } break; } default : LOGWARN (TEXT ("Invalid client message - operation ") << op); break; } } else { LOGWARN (TEXT ("Invalid client message")); } break; } case MESSAGE_DIRECTIVES_USER : m_oMessageReceivedMutex.Enter (); if (m_poMessageReceivedCallback) { m_poMessageReceivedCallback->OnMessageReceived (msg); } m_oMessageReceivedMutex.Leave (); break; default : LOGWARN (TEXT ("Unknown message delivery directive ") << FudgeMsgEnvelope_getDirectives (env)); break; } FudgeMsgEnvelope_release (env); return m_poPipes->IsConnected (); }
/// Creates a Fudge message from a binary encoding. /// /// @param[in] pData the encoded message data /// @param[in] cbData the length of the encoded data in bytes /// @return the message, or NULL if there is a problem static FudgeMsg _DecodeFudgeMsg (const void *pData, size_t cbData) { FudgeMsgEnvelope env; FudgeMsg msg = NULL; if (FudgeCodec_decodeMsg (&env, (fudge_byte*)pData, cbData) == FUDGE_OK) { msg = FudgeMsgEnvelope_getMessage (env); FudgeMsg_retain (msg); FudgeMsgEnvelope_release (env); } else { LOGWARN (TEXT ("Couldn't decode Fudge message")); } return msg; }
int main ( int argc, char * argv [ ] ) { FudgeStatus status; FudgeMsg message; FudgeMsgEnvelope envelope; AddressDetails * details [ 2 ]; fudge_i16 ordinal; fudge_byte * encoded; fudge_i32 encodedsize; /* Initialise the Fudge library */ if ( ( status = Fudge_init ( ) ) ) fatalFudgeError ( status, "Failed to initialise Fudge library" ); /* Register the AddressDetails type */ if ( ( status = FudgeRegistry_registerType ( FUDGE_TYPE_ADDRESSDETAILS, FUDGE_TYPE_PAYLOAD_BYTES, FudgeCodec_decodeFieldAddressDetails, FudgeCodec_encodeFieldAddressDetails, FudgeType_coerceAddressDetails ) ) ) fatalFudgeError ( status, "Failed to register AddressDetails type" ); /* Construct and encode two address details */ details [ 0 ] = constructAddressDetails ( Status_Past, 123, "Fake St.", "Some City", "P05 T4L" ); details [ 1 ] = constructAddressDetails ( Status_Active, 45, "Faux Road", "Some Town", "FUD 63C" ); /* Create a message and add the two details */ if ( ( status = FudgeMsg_create ( &message ) ) ) fatalFudgeError ( status, "Failed to create Fudge message" ); for ( ordinal = 0; ordinal < 2; ++ordinal ) FudgeMsg_addFieldAddressDetails ( message, 0, &ordinal, details [ ordinal ] ); /* Encode the message */ if ( ( status = FudgeMsgEnvelope_create ( &envelope, 0, 0, 0, message ) ) ) fatalFudgeError ( status, "Failed to create Fudge messag envelope" ); if ( ( status = FudgeCodec_encodeMsg ( envelope, &encoded, &encodedsize ) ) ) fatalFudgeError ( status, "Failed to encode Fudge message" ); /* Clean up source details and messge */ free ( details [ 0 ] ); free ( details [ 1 ] ); FudgeMsgEnvelope_release ( envelope ); FudgeMsg_release ( message ); /* Decode the message and release the encoded bytes array */ if ( ( status = FudgeCodec_decodeMsg ( &envelope, encoded, encodedsize ) ) ) fatalFudgeError ( status, "Failed to decode Fudge message" ); free ( encoded ); /* Retrieve, convert and display the fields */ for ( ordinal = 0; ordinal < 2; ++ordinal ) { FudgeField field; FudgeFieldData data; FudgeTypePayload payload; fudge_i32 datasize; char * ascii; if ( ( status = FudgeMsg_getFieldByOrdinal ( &field, FudgeMsgEnvelope_getMessage ( envelope ), ordinal ) ) ) fatalFudgeError ( status, "Failed to find field" ); /* Convert the field in to a string */ if ( ( status = FudgeMsg_getFieldAs ( &field, FUDGE_TYPE_STRING, &data, &payload, &datasize ) ) ) fatalFudgeError ( status, "Failed to convert field to string" ); /* This is a bit paranoid, but it's checking that the string conversion actually resulted in string payload */ if ( payload != FUDGE_TYPE_PAYLOAD_STRING ) { fprintf ( stderr, "FATAL ERROR: Retrieving field %d as a string returned a non-string!\n", ordinal ); exit ( 1 ); } FudgeString_convertToASCIIZ ( &ascii, data.string ); printf ( "Field %d: %s\n", ordinal, ascii ); free ( ascii ); FudgeString_release ( data.string ); } /* Clean up */ FudgeMsgEnvelope_release ( envelope ); return 0; }