<commit_before>/* -------------------------------------------------------------------- */
/*  ACCORDS PLATFORM                                                    */
/*  (C) 2011 by <NAME> (Prologue) <<EMAIL>>     */
/* -------------------------------------------------------------------- */
/* Licensed under the Apache License, Version 2.0 (the "License"); 	*/
/* you may not use this file except in compliance with the License. 	*/
/* You may obtain a copy of the License at 				*/
/*  									*/
/*  http://www.apache.org/licenses/LICENSE-2.0 				*/
/*  									*/
/* Unless required by applicable law or agreed to in writing, software 	*/
/* distributed under the License is distributed on an "AS IS" BASIS, 	*/
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 	*/
/* implied. 								*/
/* See the License for the specific language governing permissions and 	*/
/* limitations under the License. 					*/
/* -------------------------------------------------------------------- */
#ifndef _on_client_c
#define _on_client_c

#include "onclient.h"
#include "restpublic.h"

#define	_CORDS_NONE "none"

private	struct	on_config On = {
	(char *) 0,
	(char *) 0,

	(char *) 0,
	(char *) 0,

	(char *) 0,
	(char *) 0,

	(char *) 0,
	(char *) 0,

	(char *) 0,
	(char *) 0,

	0 

	};

public	struct	rest_header   *	on_authenticate();

/*	------------------------------------------------------------	*/
/*		l i b e r a t e _ o n _ r e s p o n s e			*/
/*	------------------------------------------------------------	*/
public	struct on_response * liberate_on_response( struct on_response * rptr )	
{
	if ( rptr )
	{
		if ( rptr->response )
			rptr->response = liberate_rest_response( rptr->response );
		if ( rptr->jsonroot )
			rptr->jsonroot = drop_data_element( rptr->jsonroot );
		if ( rptr->xmlroot )
			rptr->xmlroot = document_drop( rptr->xmlroot );
		if ( rptr->content )
			liberate( rptr->content );
		liberate( rptr );
	}
	return((struct on_response *) 0);
}

/*	----------------------------------------------------------	*/
/*		a l l o c a t e _ o n _ r e s p o n s e			*/
/*	----------------------------------------------------------	*/
private	struct	on_response * allocate_on_response()
{
	struct	on_response * rptr=(struct on_response *) 0;
	if (!( rptr = allocate( sizeof( struct on_response ) ) ))
		return( rptr );
	else
	{
		rptr->nature = _TEXT_NONE;
		rptr->content= (char *) 0;
		rptr->xmlroot = (struct xml_element *) 0;
		rptr->jsonroot = (struct data_element *) 0;
		rptr->response = (struct rest_response *) 0;
		return( rptr );
	}
}

/*	------------------------------------------------------------	*/
/*		 		o n _ c h e c k 			*/
/*	------------------------------------------------------------	*/
private	struct	on_response * on_check( struct rest_response * aptr )
{
	struct	on_response * rptr=(struct on_response *) 0;
	struct	rest_header * hptr=(struct rest_header *) 0;;
	if ( aptr )
	{
		if ( check_verbose() )
		{
			printf("   ON Client Response : %s %u %s\n",aptr->version,aptr->status,aptr->message);

			if ( check_debug() )
			{
				for (	hptr=aptr->first;
					hptr != (struct rest_header *) 0;
					hptr = hptr->next )
				{
					if (!( hptr->name )) 
						continue;
					else	printf("   %s: %s\n",hptr->name,hptr->value);
				}
				if ( aptr->body )
				{
					printf("   [ %s ] \n",aptr->body );
				}
			}
		}
	}
	if (!( rptr = allocate_on_response() ))
		return( rptr );
	else 	rptr->response = aptr;
	
	if (!( aptr->body ))
		return(rptr);
	else if (!( hptr = rest_resolve_header( aptr->first, "Content-Type" ) ))
		return(rptr);
	else if (!( rptr->content = allocate_string( hptr->value ) ))
		return( rptr );
	else if ((!( strcmp( rptr->content, "text/json" 	)))
	     ||  (!( strcmp( rptr->content, "application/json" 	)))
	     ||  (!( strcmp( rptr->content, "x-application/json"))))
	{
		rptr->nature = _TEXT_JSON;
		rptr->jsonroot = json_parse_file( aptr->body );
		return( rptr );
	}
	else if ((!( strcmp( rptr->content, "text/xml" 		)))
	     ||  (!( strcmp( rptr->content, "application/xml" 	)))
	     ||  (!( strcmp( rptr->content, "x-application/xml"))))
	{
		rptr->nature = _TEXT_XML;
		rptr->xmlroot = document_parse_file( aptr->body );
		return( rptr );
	}
	else
	{
		rptr->nature = _TEXT_XML;
		rptr->xmlroot = document_parse_file( aptr->body );
		return( rptr );
	}
}

/*	----------------------------------------------------------------	*/
/*	 	o n _ c r e a t e _ n e t w o r k _ r e q u e s t		*/
/*	----------------------------------------------------------------	*/
public	char * on_create_network_request(
		char * name,
		char * address,
		char * size
		)
{
	char *	filename;
	FILE *	h;
	int	bytes;
	struct	rest_header * hptr;

	if (!( hptr = on_authenticate() ))
		return((char *) 0);
	else if (!( filename = rest_temporary_filename("xml")))
		return( filename );
	else if (!( h = fopen( filename,"wa" ) ))
		return( liberate( filename ) );
	else
	{
		fprintf(h,"<?xml version=%c1.0%c encoding=%cUTF-8%c?>\n",0x0022,0x0022,0x0022,0x0022);
		/* ----------------------------------------- */
		/* generate network creation request element */
		/* ----------------------------------------- */
		fprintf(h,"<NETWORK>\n");
		fprintf(h,"<NAME>%s</NAME>\n",name);
		fprintf(h,"<ADDRESS>%s</ADDRESS>\n",address);
		fprintf(h,"<SIZE>%s</SIZE>\n",size);
		fprintf(h,"</NETWORK>\n");
		fclose(h);
		return( filename );
	}
}

/*	----------------------------------------------------------------	*/
/*	 	o n _ c r e a t e _ s t o r a g e _ r e q u e s t		*/
/*	----------------------------------------------------------------	*/
public	char * on_create_storage_request(
		char * name,
		char * description,
		char * type,
		char * size
		)
{
	char *	filename;
	FILE *	h;
	int	bytes;
	struct	rest_header * hptr;

	if (!( hptr = on_authenticate() ))<commit_msg>Extension of Open Nebula Client Network Creation<commit_after>/* -------------------------------------------------------------------- */
/*  ACCORDS PLATFORM                                                    */
/*  (C) 2011 by <NAME> (Prologue) <<EMAIL>>     */
/* -------------------------------------------------------------------- */
/* Licensed under the Apache License, Version 2.0 (the "License"); 	*/
/* you may not use this file except in compliance with the License. 	*/
/* You may obtain a copy of the License at 				*/
/*  									*/
/*  http://www.apache.org/licenses/LICENSE-2.0 				*/
/*  									*/
/* Unless required by applicable law or agreed to in writing, software 	*/
/* distributed under the License is distributed on an "AS IS" BASIS, 	*/
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 	*/
/* implied. 								*/
/* See the License for the specific language governing permissions and 	*/
/* limitations under the License. 					*/
/* -------------------------------------------------------------------- */
#ifndef _on_client_c
#define _on_client_c

#include "onclient.h"
#include "restpublic.h"

#define	_CORDS_NONE "none"

private	struct	on_config On = {
	(char *) 0,
	(char *) 0,

	(char *) 0,
	(char *) 0,

	(char *) 0,
	(char *) 0,

	(char *) 0,
	(char *) 0,

	(char *) 0,
	(char *) 0,

	0 

	};

public	struct	rest_header   *	on_authenticate();

/*	------------------------------------------------------------	*/
/*		l i b e r a t e _ o n _ r e s p o n s e			*/
/*	------------------------------------------------------------	*/
public	struct on_response * liberate_on_response( struct on_response * rptr )	
{
	if ( rptr )
	{
		if ( rptr->response )
			rptr->response = liberate_rest_response( rptr->response );
		if ( rptr->jsonroot )
			rptr->jsonroot = drop_data_element( rptr->jsonroot );
		if ( rptr->xmlroot )
			rptr->xmlroot = document_drop( rptr->xmlroot );
		if ( rptr->content )
			liberate( rptr->content );
		liberate( rptr );
	}
	return((struct on_response *) 0);
}

/*	----------------------------------------------------------	*/
/*		a l l o c a t e _ o n _ r e s p o n s e			*/
/*	----------------------------------------------------------	*/
private	struct	on_response * allocate_on_response()
{
	struct	on_response * rptr=(struct on_response *) 0;
	if (!( rptr = allocate( sizeof( struct on_response ) ) ))
		return( rptr );
	else
	{
		rptr->nature = _TEXT_NONE;
		rptr->content= (char *) 0;
		rptr->xmlroot = (struct xml_element *) 0;
		rptr->jsonroot = (struct data_element *) 0;
		rptr->response = (struct rest_response *) 0;
		return( rptr );
	}
}

/*	------------------------------------------------------------	*/
/*		 		o n _ c h e c k 			*/
/*	------------------------------------------------------------	*/
private	struct	on_response * on_check( struct rest_response * aptr )
{
	struct	on_response * rptr=(struct on_response *) 0;
	struct	rest_header * hptr=(struct rest_header *) 0;;
	if ( aptr )
	{
		if ( check_verbose() )
		{
			printf("   ON Client Response : %s %u %s\n",aptr->version,aptr->status,aptr->message);

			if ( check_debug() )
			{
				for (	hptr=aptr->first;
					hptr != (struct rest_header *) 0;
					hptr = hptr->next )
				{
					if (!( hptr->name )) 
						continue;
					else	printf("   %s: %s\n",hptr->name,hptr->value);
				}
				if ( aptr->body )
				{
					printf("   [ %s ] \n",aptr->body );
				}
			}
		}
	}
	if (!( rptr = allocate_on_response() ))
		return( rptr );
	else 	rptr->response = aptr;
	
	if (!( aptr->body ))
		return(rptr);
	else if (!( hptr = rest_resolve_header( aptr->first, "Content-Type" ) ))
		return(rptr);
	else if (!( rptr->content = allocate_string( hptr->value ) ))
		return( rptr );
	else if ((!( strcmp( rptr->content, "text/json" 	)))
	     ||  (!( strcmp( rptr->content, "application/json" 	)))
	     ||  (!( strcmp( rptr->content, "x-application/json"))))
	{
		rptr->nature = _TEXT_JSON;
		rptr->jsonroot = json_parse_file( aptr->body );
		return( rptr );
	}
	else if ((!( strcmp( rptr->content, "text/xml" 		)))
	     ||  (!( strcmp( rptr->content, "application/xml" 	)))
	     ||  (!( strcmp( rptr->content, "x-application/xml"))))
	{
		rptr->nature = _TEXT_XML;
		rptr->xmlroot = document_parse_file( aptr->body );
		return( rptr );
	}
	else
	{
		rptr->nature = _TEXT_XML;
		rptr->xmlroot = document_parse_file( aptr->body );
		return( rptr );
	}
}

/*	----------------------------------------------------------------	*/
/*	 	o n _ c r e a t e _ n e t w o r k _ r e q u e s t		*/
/*	----------------------------------------------------------------	*/
public	char * on_create_network_request(
		char * name,
		char * address,
		char * size
		)
{
	char *	filename;
	FILE *	h;
	int	bytes;
	struct	rest_header * hptr;

	if (!( hptr = on_authenticate() ))
		return((char *) 0);
	else if (!( filename = rest_temporary_filename("xml")))
		return( filename );
	else if (!( h = fopen( filename,"wa" ) ))
		return( liberate( filename ) );
	else
	{
		fprintf(h,"<?xml version=%c1.0%c encoding=%cUTF-8%c?>\n",0x0022,0x0022,0x0022,0x0022);
		/* ----------------------------------------- */
		/* generate network creation request element */
		/* ----------------------------------------- */
		fprintf(h,"<NETWORK>\n");
		fprintf(h,"<NAME>%s</NAME>\n",name);
		if (!( rest_valid_string( address ) ))
			fprintf(h,"<ADDRESS/>\n");
		else 	fprintf(h,"<ADDRESS>%s</ADDRESS>\n",address);
		if (!( rest_valid_string( size ) ))
		{
			fprintf(h,"<SIZE>256</SIZE>\n");
			fprintf(h,"<PUBLIC>NO</PUBLIC>\n");
		}
		else if (!( strcasecmp( size, "B" )))
		{
			fprintf(h,"<SIZE>%u</SIZE>\n",65635);
			fprintf(h,"<PUBLIC>NO</PUBLIC>\n");
		}

		else if (!( strcasecmp( size, "C" )))
		{
			fprintf(h,"<SIZE>%u</SIZE>\n",256);
			fprintf(h,"<PUBLIC>NO</PUBLIC>\n");
		}

		else if (!( strcasecmp( size, "PB" )))
		{
			fprintf(h,"<SIZE>%u</SIZE>\n",65635);
			fprintf(h,"<PUBLIC>YES</PUBLIC>\n");
		}
		else if (!( strcasecmp( size, "PC" )))
		{
			fprintf(h,"<SIZE>%u</SIZE>\n",256);
			fprintf(h,"<PUBLIC>YES</PUBLIC>\n");
		}
		else
		{
			fprintf(h,"<SIZE>256</SIZE>\n");
			fprintf(h,"<PUBLIC>NO</PUBLIC>\n");
		}
		fprintf(h,"</NETWORK>\n");
		fclose(h);
		return( filename );
	}
}

/*	----------------------------------------------------------------	*/
/*	 	o n _ c r e a t e _ s t o r a g e _ r e q u e s t		*/
/*	----------------------------------------------------------------	*/
public	char * on_create_storage_request(
		char * name,
		char * description,
		char * type,
		char * size
		)
{
	char *	filename;
	FILE *	h;
	int	bytes;
	struct	rest_header * hptr;

	if (!( hptr = on_authenticate() ))
{'fraction_non_alphanumeric': 0.2984259556697719, 'fraction_numerical': 0.010118856408609059, 'mean_word_length': 1.6774887121049236, 'pattern_counts': {'":': 0, '<': 50, '<?xml version=': 2, '>': 142, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 24}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 28, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1039536', 'n_tokens_mistral': 4628, 'n_tokens_neox': 4185, 'n_words': 1006}