/**************************************************/
/* scol v 4                                       */
/*                                                */
/* inet.cpp                                       */
/*                                                */
/* scol inet, linux-nox part                      */
/*                                                */
/* Loïc Berthelot, CryoNetworks, mars 2002        */
/**************************************************/


//
// Modifications History
//
//$LB (21/02/2002) : add threaded version of INETisConnected function
//


#include <stdlib.h>

#include "../../kernel/scol.h"
#include "../../kernel/include/kernel.h"
#include "../../kernel/mainscol.h"
#include "../../kernel/vm/mbytec.h"



//#include "../../kernel/include/inet.h"
#include "../../kernel/include/inetconnect.h"






//$LB (19/03/02) : don't need it for linux-nox
//int ObjInetConnectType;
//int ObjInetConnectHandle = 0;




/*
int InetConnectDestroy(mmachine m, int handsys, int mobj)
{
	return 0;
}
*/




















/*******************************************************************************************************************************/
/*******************************************************************************************************************************/
/**                                                                                                                           **/
/**             INETisConnected, synchronous version                                                                          **/
/**                                                                                                                           **/
/*******************************************************************************************************************************/
/*******************************************************************************************************************************/




/*************************************************/
/*                                               */
/* int SCINETisConnected (mmachine m)            */
/*                                               */
/* scol interface of INETisConnected             */
/*                                               */
/* returns 1 if physically connected to the net, */
/* 0 if not.                                     */
/* the parameter is the url on which the user    */
/* wants to test the connexion.                  */
/*                                               */
/* fun [S] I                                     */
/*************************************************/
int SCINETisConnected (mmachine m)
{
	MMset(m, 0, 2); // 2 <=> 1<<1
	return 0;
}












/*******************************************************************************************************************************/
/*******************************************************************************************************************************/
/**                                                                                                                           **/
/**             _rflINETisConnected, threaded version of INETisConnected                                                      **/
/**                                                                                                                           **/
/*******************************************************************************************************************************/
/*******************************************************************************************************************************/







//$LB (21/02/2002) : add threaded version of INETisConnected function
/*************************************************/
/*                                               */
/* int SCrflINETisConnected (mmachine m)         */
/*                                               */
/* create a thread and run INETisConnected in it.*/
/* the result of INETisConnected is returned in  */
/* using the callback pointer the user gave as   */
/* parameter.                                    */
/* the result is 1 if physically connected to    */
/* the net, 0 if not.                            */
/*                                               */
/* fun [S fun [S u0 I] u1 u0] I                  */
/*                                               */
/* parameters :                                  */
/*   - S : url on which the user wants to test   */
/*         the connexion.                        */
/*                                               */
/*   - fun [S u0 I] u1 : callback to check the   */
/*                       result of the test.     */
/*                       - S  : url tested       */
/*                       - u0 : user parameter   */
/*                       - I  : test result      */
/*                                               */
/*   - u0 : user parameter                       */
/*                                               */
/*************************************************/

int SCrflINETisConnected (mmachine m)
{
int murl, mcbk, muserparam, k;
char* url;


	muserparam = MMget(m, 0);
	mcbk       = MMget(m, 1);
	murl       = MMget(m, 2);

	url = (murl == NIL) ? NULL : MMstartstr (m, murl>>1);


	if (url == NULL)
	{
		MMechostr (MSKFOO, "_rflINETisConnected url NULL\n");
		SEDROP(m, 2);
		MMset(m,0,NIL);
		return 0;
	}

	if (mcbk == NIL)
	{
		MMechostr (MSKFOO, "_rflINETisConnected  : a callback has to be assigned!\n");
		SEDROP(m, 2);
		MMset(m,0,NIL);
		return 0;

	}


	//$LB (19/03/2002) : 
	// push the url
	if ((k = MMpush (m, murl))) return k;
	// push the user parameter
	if ((k = MMpush (m, MMget(m, 1)))) return k;
	// push the result of the test (1<<1 : means the test is true)
	if ((k = MMpush (m, 2))) return k;
	// push the callback pointer
	if ((k= MMpush (m, MMget(m, 4)))) return k;
	// and exec it
	if ((k = Minterpreter(m)) && (k != 1)) return k;
	
	

	SEDROP(m, 3);
	MMset(m,0,0);
	return 0;
}


















/*******************************************************************************************************************************/
/*******************************************************************************************************************************/
/**                                                                                                                           **/
/**             package definition                                                                                            **/
/**                                                                                                                           **/
/*******************************************************************************************************************************/
/*******************************************************************************************************************************/





//$ LB (17/07/2001) : add INETisConnected function
//$LB (21/02/2002) : add _rflINETisConnected function
#define INETCONNECT_PKG_NB 2

char* INETCONNECT_name[INETCONNECT_PKG_NB]={
		"INETisConnected", "_rflINETisConnected"
};

int (*INETCONNECT_fun[INETCONNECT_PKG_NB])(mmachine m)={
		SCINETisConnected, SCrflINETisConnected
};

int INETCONNECT_narg[INETCONNECT_PKG_NB]={
		1, 3
};

char* INETCONNECT_type[INETCONNECT_PKG_NB]={
		"fun [S] I", "fun [S fun [S u0 I] u1 u0] I"
};






int SCOLloadINETCONNECT (mmachine m)
{
	//$LB (05/03/02) : add the INETCONNECT type for a internal use.
	//                  usefull to manage the threaded version of INETisConnected function (_rflINETisConnected)
	//$LB (19/03/02) : don't need it for linux-nox
	//ObjInetConnectType = OBJregister(INETCONNECT_RFL_NB, 0, InetConnectDestroy, "INETCONNECT");
	
	return PKhardpak(m, "INETCONNECT.pkg", INETCONNECT_PKG_NB, INETCONNECT_name, INETCONNECT_fun, INETCONNECT_narg, INETCONNECT_type);
}


