//**************************************************************************
#include "dataclient.h"
//**************************************************************************
#ifndef _DATACLIENT_RECV_H_
#define _DATACLIENT_RECV_H_
//**************************************************************************
template <class Etype>
int DataClient::RecvVariable(char *keyword, Etype *a)
{
int handle;
if (!isValidKeyword(keyword, &handle)) return CSL_FAILURE;
if (!isVariable(handle)) return CSL_FAILURE;
if (!VerifyElementSize(handle, sizeof(Etype))) return CSL_FAILURE;

*a = 0;
int size = QueryKeyword(keyword);
if (size) RemoteSocket::RecvVar(a);

return CSL_SUCCESS;
}
//**************************************************************************
template <class Etype>
Etype DataClient::RecvVariable(char *keyword)
{
Etype a;
RecvVariable(keyword, &a);

return a;
}
//**************************************************************************
inline int DataClient::RecvArray(char *keyword, void *a)
{
int handle;
if (!isValidKeyword(keyword, &handle)) return CSL_FAILURE;
if (!isStaticArray(handle)) return CSL_FAILURE;

int size = QueryKeyword(keyword);
int elemsize = RecvVar<int>();
RecvArrayGeneric(a, size, elemsize);

return CSL_SUCCESS;
}
//**************************************************************************
template <class Etype>
inline int DataClient::RecvArray1D(char *keyword, Etype *a)
{
int handle;
if (!isValidKeyword(keyword, &handle)) return CSL_FAILURE;
if (!VerifyElementSize(handle, sizeof(Etype))) return CSL_FAILURE;
if (!isDynamicArray(handle, 1)) return CSL_FAILURE;

int n1 = QueryKeyword(keyword);
RemoteSocket::RecvArray1D(a, n1);

return CSL_SUCCESS;
}
//**************************************************************************
template <class Etype>
inline int DataClient::RecvArray1D(char *keyword, Etype **ptr)
{
int handle;
if (!isValidKeyword(keyword, &handle)) return CSL_FAILURE;
if (!VerifyElementSize(handle, sizeof(Etype))) return CSL_FAILURE;
if (!isDynamicArray(handle, 1)) return CSL_FAILURE;

Etype *a;
int n1 = QueryKeyword(keyword);
ALLOC1D(&a, n1);
RemoteSocket::RecvArray1D(a, n1);

*ptr = a;
return CSL_SUCCESS;
}
//**************************************************************************
template <class Etype>
inline int DataClient::RecvArray2D(char *keyword, Etype **a)
{
int handle;
if (!isValidKeyword(keyword, &handle)) return CSL_FAILURE;
if (!VerifyElementSize(handle, sizeof(Etype))) return CSL_FAILURE;
if (!isDynamicArray(handle, 2)) return CSL_FAILURE;

int n1 = QueryKeyword(keyword);
int n2 = RecvVar<int>();
RemoteSocket::RecvArray2D(a, n1, n2);

return CSL_SUCCESS;
}
//**************************************************************************
template <class Etype>
inline int DataClient::RecvArray2D(char *keyword, Etype ***ptr)
{
int handle;
if (!isValidKeyword(keyword, &handle)) return CSL_FAILURE;
if (!VerifyElementSize(handle, sizeof(Etype))) return CSL_FAILURE;
if (!isDynamicArray(handle, 2)) return CSL_FAILURE;

Etype **a;
int n1 = QueryKeyword(keyword);
int n2 = RecvVar<int>();
ALLOC2D(&a, n1, n2);
RemoteSocket::RecvArray2D(a, n1, n2);

*ptr = a;
return CSL_SUCCESS;
}
//**************************************************************************
template <class Etype>
inline int DataClient::RecvArray3D(char *keyword, Etype ***a)
{
int handle;
if (!isValidKeyword(keyword, &handle)) return CSL_FAILURE;
if (!VerifyElementSize(handle, sizeof(Etype))) return CSL_FAILURE;
if (!isDynamicArray(handle, 3)) return CSL_FAILURE;

int n1 = QueryKeyword(keyword);
int n2 = RecvVar<int>();
int n3 = RecvVar<int>();
RemoteSocket::RecvArray3D(a, n1, n2, n3);

return CSL_SUCCESS;
}
//**************************************************************************
template <class Etype>
inline int DataClient::RecvArray3D(char *keyword, Etype ****ptr)
{
int handle;
if (!isValidKeyword(keyword, &handle)) return CSL_FAILURE;
if (!VerifyElementSize(handle, sizeof(Etype))) return CSL_FAILURE;
if (!isDynamicArray(handle, 3)) return CSL_FAILURE;

Etype ***a;
int n1 = QueryKeyword(keyword);
int n2 = RecvVar<int>();
int n3 = RecvVar<int>();
ALLOC3D(&a, n1, n2, n3);
RemoteSocket::RecvArray3D(a, n1, n2, n3);

*ptr = a;
return CSL_SUCCESS;
}
//**************************************************************************
template <class Etype>
inline int DataClient::RecvArray4D(char *keyword, Etype ****a)
{
int handle;
if (!isValidKeyword(keyword, &handle)) return CSL_FAILURE;
if (!VerifyElementSize(handle, sizeof(Etype))) return CSL_FAILURE;
if (!isDynamicArray(handle, 4)) return CSL_FAILURE;

int n1 = QueryKeyword(keyword);
int n2 = RecvVar<int>();
int n3 = RecvVar<int>();
int n4 = RecvVar<int>();
RemoteSocket::RecvArray4D(a, n1, n2, n3, n4);

return CSL_SUCCESS;
}
//**************************************************************************
template <class Etype>
inline int DataClient::RecvArray4D(char *keyword, Etype *****ptr)
{
int handle;
if (!isValidKeyword(keyword, &handle)) return CSL_FAILURE;
if (!VerifyElementSize(handle, sizeof(Etype))) return CSL_FAILURE;
if (!isDynamicArray(handle, 4)) return CSL_FAILURE;

Etype ****a;
int n1 = QueryKeyword(keyword);
int n2 = RecvVar<int>();
int n3 = RecvVar<int>();
int n4 = RecvVar<int>();
ALLOC4D(&a, n1, n2, n3, n4);
RemoteSocket::RecvArray4D(a, n1, n2, n3, n4);

*ptr = a;
return CSL_SUCCESS;
}
//**************************************************************************
template <class myStruct>
inline double DataClient::RecvStruct(char *keyword, myStruct *S, Semaphore *sem)
{
if (!isValidKeyword(keyword)) return CSL_FAILURE;
SendKeyword(keyword);
return RemoteSocket::RecvStruct(S, sem);
}
//**************************************************************************
inline int DataClient::RecvFile(char *filename, char *destfile)
{
SendKeyword("internal_ftp");
SendLine(filename);
int status;
if (destfile == NULL)
        status = RemoteSocket::RecvFile(filename);
else
        status = RemoteSocket::RecvFile(destfile);
return status;
}
//**************************************************************************
#endif // _DATACLIENT_RECV_H_
//**************************************************************************