/**********************************************************************
 * Copyright (c) 2005, 2007 Scapa Technologies Limited and others
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * $Id: rac.h,v 1.2 2007/04/06 01:09:33 jkubasta Exp $
 * 
 * Contributors: 
 * Scapa Technologies Limited - Initial API and implementation
 **********************************************************************/

#ifndef __RAC_H__
#define __RAC_H__

#include "DataTransferInterface.h"
#include "system.h"

#include "RASocket.h"
#include "RADataTransfer.h"
#include "RAShm.h"
#include "RABindings.h"
#include "hashtable.h"

//WINDOWS ONLY
#ifdef WIN32
	#include "winsock2.h" /* Bug 134924 */
	#include "windows.h"
	#include "winperf.h"
#endif
//END WINDOWS ONLY

#define RAC_HYADES_TYPED_EVENT          1
#define RAC_HYADES_MESSAGE_EVENT        2
#define RAC_HYADES_VERDICT_EVENT        3
#define RAC_HYADES_INVOCATION_EVENT     4

#ifdef __cplusplus
extern "C" {
#endif


//these two are not important - RACStartup and RACShutdown are all that are required
int RACLoadRacLibs();
int RACGetRacFuncs();

////////////////////////////
// Performance data related functions
////////////////////////////

typedef struct {
	char* name;
	int tracing;
	int index;
//	int type;

	//WINDOWS ONLY
  BOOL               enabled;
	#ifdef WIN32
	DWORD win_id;

    BOOL               isPrinted;
    int                type;
    int                id;
    LONGLONG           newTime;
    LONGLONG           oldTime;
    LONGLONG           perfFreq;
    LONGLONG           newTime100Ns ;
    LONGLONG           oldTime100Ns ;
    LONGLONG           counterValue[2];
    LONGLONG           oldCounterValue[2];
    PPERF_OBJECT_TYPE  perfObject;
    unsigned short     nameLength;
//    char              *name;
	LONGLONG		preservedOldTime100Ns;
	#endif
	//END WINDOWS ONLY
} PerformanceElement_t;

PerformanceElement_t* all_counters;
int all_counters_len;

int RACGetCounters(PerformanceElement_t** counters, int* counters_len);
int RACDestroyCountersArray(PerformanceElement_t* counters, int counters_len);
void RACPrintCounters(PerformanceElement_t* counters, int counters_len);
void RACSendAvailableCounters();
void RACSendCounterData();
void RACSetFrequency(int new_freq);
int RACGetFrequency();

//PRIVATE
int RACGetPerfNames(char** names);
int increaseBufferSize(char** buf, int* buflen);
void RACTraceCounters(PerformanceElement_t* counters, int counters_len);
int RACTraceStats(BOOL trace_counters);

//WINDOWS ONLY
#ifdef WIN32
int RACTracePerfData(PERF_DATA_BLOCK *perfData, XINT64 time, BOOL trace_counters);

void updateCounters(PerformanceElement_t* _perfElements, PERF_DATA_BLOCK *, PPERF_OBJECT_TYPE perfObj, PPERF_COUNTER_DEFINITION curCntr, PPERF_COUNTER_BLOCK ptrToCntr, BOOL instanceCounter, BOOL firstInstance, BOOL lastCounter);
void calculateCounters(PerformanceElement_t* _perfElements, PPERF_OBJECT_TYPE perfObj, PPERF_COUNTER_BLOCK ptrToCntr, char** buf, int* buf_ptr, int* buf_len, BOOL instanceCounter);
void calculate(PerformanceElement_t * element, char** buf, int* buf_ptr, int* buf_len);
void updatePerformanceElement(PerformanceElement_t* _perfElements,
							  PPERF_DATA_BLOCK perfData,
                              PPERF_OBJECT_TYPE perfObj,
                              PPERF_COUNTER_DEFINITION curCntr,
                              PPERF_COUNTER_BLOCK ptrToCntr,
                              BOOL instanceCounter, BOOL firstInstance, BOOL lastCounter);
#endif
//END WINDOWS ONLY


////////////////////////////
// RAC Bindings functions
////////////////////////////

typedef RA_AGENT_HANDLE (*ra_initializeBindings_t)(const char *name,
                                             const char *type,
                                             ra_commandHandler_t handler,
											 BOOL standalone);

typedef TID* (*ra_startListener_t)(RA_AGENT_HANDLE handle,
                             char block);

typedef void (*ra_stopListener_t)(RA_AGENT_HANDLE handle);

typedef void (*ra_finalizeBindings_t)(RA_AGENT_HANDLE handle);

typedef void (*ra_sendMessage_t)(RA_AGENT_HANDLE handle,
								 ra_message_t *message);


////////////////////////////
// RAC DataTransfer functions
////////////////////////////

typedef unsigned char* (*ra_allocateMessageBlock_t)(ra_uint_t length);

typedef unsigned char* (*ra_reallocateMessageBlock_t)(unsigned char *message, ra_uint_t length);

typedef void (*ra_freeMessageBlock_t)(unsigned char *message);

/** WRITE_MESSAGE_BLOCK *************************************************
  * DNS - replaced socket parm with target handle struct
  * Writes a message to the specified target using the specified format.
  * @param  handle - the handle of the target to write the data to.
  * @param  format - the format to use
  * @param message - the address of the start of the message.
  * @param  length - the number of bytes to write to fd.
  * @returns       - the number of bytes written to the socket.
  */
typedef ra_uint_t (*ra_writeMessageBlock_t)(ra_data_target_hdl_t *handle,
                                      ra_dataFormat_t format,
                                      unsigned char *message,
                                      ra_uint_t length);

/////////////////////////////
// RAC Shared memory
/////////////////////////////

typedef ra_shm_err_t (*ra_attachToShm_t)(char *name, ra_shm_handle_t **handle);

typedef ra_shm_err_t (*ra_stopFlushingShm_t)(ra_shm_handle_t **handle);


/////////////////////////////
// RAC Common
/////////////////////////////

typedef void (*ra_setLastError_t)(ra_uint_t major, ra_uint_t minor);


////////////////////////////
// Dynamically Loaded RAC functions
////////////////////////////

ra_initializeBindings_t ra_initializeBindings_f;
ra_finalizeBindings_t ra_finalizeBindings_f;
ra_startListener_t ra_startListener_f;
ra_stopListener_t ra_stopListener_f;
ra_sendMessage_t ra_sendMessage_f;

ra_allocateMessageBlock_t ra_allocateMessageBlock_f;
ra_reallocateMessageBlock_t ra_reallocateMessageBlock_f;
ra_freeMessageBlock_t ra_freeMessageBlock_f;
ra_writeMessageBlock_t ra_writeMessageBlock_f;

ra_attachToShm_t ra_attachToShm_f;
ra_stopFlushingShm_t ra_stopFlushingShm_f;

ra_setLastError_t ra_setLastError_f;

/***********************************************************************
 * EOF
 ***********************************************************************/

#ifdef __cplusplus
}
#endif


#endif
