isolationserver/isoserver/src/isoutils.c
changeset 10 59927b2d3b75
parent 0 d0f3a028347a
equal deleted inserted replaced
0:d0f3a028347a 10:59927b2d3b75
     1 /*
       
     2 * ============================================================================
       
     3 *  Name        : isoutils.c
       
     4 *  Part of     : isolation server.
       
     5 *  Version     : %version: 15 %
       
     6 *
       
     7 *  Copyright © 2007-2008 Nokia.  All rights reserved.
       
     8 *  All rights reserved.
       
     9 *  Redistribution and use in source and binary forms, with or without modification, 
       
    10 *  are permitted provided that the following conditions are met:
       
    11 *  Redistributions of source code must retain the above copyright notice, this list 
       
    12 *  of conditions and the following disclaimer.Redistributions in binary form must 
       
    13 *  reproduce the above copyright notice, this list of conditions and the following 
       
    14 *  disclaimer in the documentation and/or other materials provided with the distribution.
       
    15 *  Neither the name of the Nokia Corporation nor the names of its contributors may be used 
       
    16 *  to endorse or promote products derived from this software without specific prior written 
       
    17 *  permission.
       
    18 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
       
    19 *  EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
       
    20 *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
       
    21 *  SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
       
    22 *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 
       
    23 *  OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
       
    24 *  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
       
    25 *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 
       
    26 *  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
       
    27 * ============================================================================
       
    28 * Template version: 1.0
       
    29 */
       
    30 
       
    31 #include <stdio.h>
       
    32 #include <stdlib.h>
       
    33 #include <string.h>
       
    34 #include <glib.h>
       
    35 
       
    36 #include "isoutils.h"
       
    37 #include "msgliterals.h"
       
    38 #include "isoservermain.h"
       
    39 
       
    40 /*!	\file 
       
    41 *	Impliments the functions in isoutils.h
       
    42 */
       
    43 
       
    44 /*! \def Thread stack size
       
    45 */
       
    46 #define LOG_FILENAME "c:\\isoserver.txt"
       
    47 
       
    48 /*! \brief Documented in the header file
       
    49 */
       
    50 void  iso_logger ( gchar* fmt, ... )
       
    51 	{
       
    52 	#ifdef _DEBUG
       
    53 	
       
    54 	FILE* fp;
       
    55 	//open file to read..
       
    56 	fp = fopen( LOG_FILENAME,"a" ); 
       
    57 	if ( fp ) 
       
    58 	    {
       
    59 	    int ival;
       
    60 	    double dval;
       
    61 	    char* sval;
       
    62 	    char* p;
       
    63 	    va_list list;
       
    64 	    //initalize the variable arg to fmt..
       
    65 		va_start( list, fmt );
       
    66 		//Go char by char thru' format string
       
    67 		//example format string ("Hi, by %s", str)
       
    68 		for ( p = fmt; *p; p++ ) 
       
    69 			{
       
    70 			if ( *p != '%' )
       
    71 				{
       
    72 				//Not format specifiers just print them to file
       
    73 				//In example above printe "Hi, by " to file
       
    74 				putc( *p, fp  );
       
    75 				continue;	
       
    76 				}
       
    77 			switch( *++p ) 
       
    78 				{
       
    79 				case 'd' : 
       
    80 					//Next arg got is int
       
    81 					//read integer and print to file
       
    82 					ival = va_arg( list, int );
       
    83 					fprintf( fp, "%d", ival );
       
    84 					break;
       
    85 				
       
    86 				case 'f' : 
       
    87 					//Next arg got is float
       
    88 					//read float and print to file
       
    89 					dval = va_arg( list, double );
       
    90 					fprintf( fp, "%f", dval, fp );
       
    91 					break;
       
    92 				
       
    93 				case 's' :
       
    94 					//Next arg got is string
       
    95 					//read string and go char by char and print to file
       
    96 					for ( sval = va_arg( list, char * ); *sval; sval++ ) 
       
    97 			   			{
       
    98 			   			putc( *sval, fp );
       
    99 			   			}
       
   100 			   			break;
       
   101 			   	case 'u' : 
       
   102 					//Next arg got is int
       
   103 					//read integer and print to file
       
   104 					ival = va_arg( list, uint );
       
   105 					fprintf( fp, "%u", ival );
       
   106 					break;
       
   107 				default:
       
   108 					break;
       
   109 				}
       
   110 			}
       
   111 		//print a new line
       
   112 		fprintf(fp,"\n");
       
   113 		//close file
       
   114 		fclose(fp);
       
   115 		//close the variable arg list
       
   116 		va_end( list );
       
   117 	    }
       
   118 	#endif //_DEBUG
       
   119 	}
       
   120 
       
   121 /*! \brief Documented in the header file
       
   122 */
       
   123 gint parse_a_string( gchar* msg_buf, gchar** str, gint* len, gint msg_len ) 
       
   124 	{
       
   125 	//parsing the buffer into an array of strings(contacts)
       
   126 	gint userlen = 0;
       
   127 	gchar* users = NULL;
       
   128 	
       
   129 	while( '\0' != *( msg_buf + *len ) && *len < msg_len ) 
       
   130 		{
       
   131 		//len is different from userlen
       
   132 		//len is total no. of characters read in msg
       
   133 		//where as userlen is no. of char read for this contact
       
   134 		userlen++;
       
   135 		(*len)++;
       
   136 		}
       
   137 		
       
   138 	//the string is not present
       
   139 	//or,Reached the last byte of parse string and it is not '\0'
       
   140 	if ( 0 == userlen || 
       
   141 		( '\0' != *( msg_buf + *len ) && *len == msg_len ) )
       
   142 		{
       
   143 		//Got parse error..
       
   144 		//Invalid message format..
       
   145 		return INVALID_PARAMETERES;	
       
   146 		}
       
   147 	//Allocate memory and check for error
       
   148 	users = ( gchar* ) malloc ( userlen + 1 ); 
       
   149 	if ( NULL == users ) 
       
   150 		{
       
   151 		return MEM_ALLOCATION_ERROR;
       
   152 		}
       
   153 	memset( users, '\0', userlen + 1 );
       
   154 	//copy one contact...
       
   155 	memcpy( users, ( msg_buf + *len - userlen ), userlen + 1 );	
       
   156 	*str = users;
       
   157 	(*len)++;
       
   158 	//On success return 0
       
   159 	return 0;
       
   160 	}
       
   161 
       
   162 /*! \brief Documented in the header file
       
   163 */
       
   164 gint parse_into_array_of_strings( gchar* msg_buf, gchar*** contact_ids, 
       
   165 		gint* len, gint msg_len, gint* cntidscount ) 
       
   166 	{
       
   167 	gchar** cntids = NULL;
       
   168 	gchar* users = NULL;
       
   169 	gint err = 0;
       
   170 	
       
   171 	//to calc MAX_MSG_RECEIPIENTS dynamically one more loop needs to be done 
       
   172 	//for this
       
   173 	//Allocate memory and check for error returns
       
   174 	cntids = ( gchar** ) malloc( MAX_MSG_RECEIPIENTS * sizeof( gchar* ) );
       
   175 	if ( NULL == cntids ) 
       
   176 		{
       
   177 		return MEM_ALLOCATION_ERROR;
       
   178 		}
       
   179 	do
       
   180 		{
       
   181 		//get one string at a time..	
       
   182 		err = parse_a_string( msg_buf, &users, len, msg_len );
       
   183 		
       
   184 		if ( err < 0 ) 
       
   185 			{
       
   186 			return err;
       
   187 			}
       
   188 		//check if max no of contacts are read..
       
   189 		//if not, copy contact into 2D array of strings...
       
   190 		//else ignore the extra contacts
       
   191 		//do not over run the cntids , incase of 
       
   192 		//no. of contacts more than max msg recp. @ line 197
       
   193 		//cntids[*cntidscount] = NULL;
       
   194 		if ( *cntidscount < MAX_MSG_RECEIPIENTS - 1 ) 
       
   195 			{
       
   196 			cntids[*cntidscount] = users;	
       
   197 			( *cntidscount )++;
       
   198 			}
       
   199 		else 
       
   200 			{
       
   201 			//users to be freed..ignoring extra no. of recipients
       
   202 			free ( users );
       
   203 			}
       
   204 		
       
   205 		//increment the no. of contacts..
       
   206 		
       
   207 		//all contacts read break the loop
       
   208 		//
       
   209 		if ( '\0' == *( msg_buf + *len ) )
       
   210 			{
       
   211 			break;
       
   212 			}
       
   213 		//If maximum no. of receipients is reached break 
       
   214 		//so here of total no. of contacts, message is only sent to
       
   215 		//first MAX_MSG_RECEIPIENTS - 1
       
   216 		} while( *len < msg_len );
       
   217 	cntids[*cntidscount] = NULL;
       
   218 	
       
   219 	/*
       
   220 	//Did not find any use of this as of now..
       
   221 	//But in case of any mem issues can be looked into
       
   222 	if ( *cntidscount < MAX_MSG_RECEIPIENTS ) 
       
   223 		{
       
   224 		//realloc is debatable ...
       
   225 		cntids = ( char** ) realloc( cntids, ( *cntidscount + 1 ) * sizeof( char* ) );
       
   226 		}*/
       
   227 	//Assign to contact ids
       
   228 	*contact_ids = cntids;
       
   229 	(*len)++; //skip the last '\0' ... 2D arrays end with '\0'
       
   230 	return 0;	
       
   231 	}
       
   232 
       
   233 /*! \brief Documented in the header file
       
   234 */
       
   235 void free_msg_args( gchar** contactid, gint contacts_count, gchar* sendmsg ) 
       
   236 	{
       
   237 	gint i = 0;
       
   238 	//free all the contacts
       
   239 	if ( NULL != contactid ) 
       
   240 		{
       
   241 		for ( i = 0; i < contacts_count; i++ ) 
       
   242 			{
       
   243 			if ( NULL != (contactid)[i] )	
       
   244 				{
       
   245 				free ( (contactid)[i] );
       
   246 				}
       
   247 			}
       
   248 		}
       
   249 	//free the message
       
   250 	if ( NULL != sendmsg ) 
       
   251 		{
       
   252 		free ( sendmsg );
       
   253 		}
       
   254 	}
       
   255 
       
   256 /*! \brief Documented in the header file
       
   257 */	
       
   258 gint send_response_to_client( message_hdr_req* hdr_req, gint error, 
       
   259 		gboolean response )
       
   260 	{
       
   261 	int pri = MSG_PRI_NORMAL;
       
   262 	gint result = 0;
       
   263 	gint err = 0;
       
   264 	int timeout = NO_WAIT; 
       
   265 	message_hdr_resp* msg_struct = NULL;
       
   266 	
       
   267 	result = MsgQCreate( RESPONSE_QUEUE, MAX_MSG_Q_SIZE, MSG_Q_FIFO, &err );
       
   268 	if ( ERROR == result )
       
   269 		{
       
   270 		return ERROR;
       
   271 		}
       
   272 		
       
   273 	msg_struct = ( message_hdr_resp* ) malloc ( sizeof( message_hdr_resp ) );
       
   274 	if ( NULL == msg_struct ) 
       
   275 		{
       
   276 		return 	MEM_ALLOCATION_ERROR;
       
   277 		}
       
   278 
       
   279 	//intialize memory to 0
       
   280 	memset( msg_struct, '\0', sizeof( message_hdr_resp ) );
       
   281 	//Copy the header request into response header..
       
   282 	//This req header in resp will be used to map the req to resp.
       
   283 	msg_struct->hdr_req.message_type = hdr_req->message_type;
       
   284 	msg_struct->hdr_req.protocol_id = hdr_req->protocol_id;
       
   285 	msg_struct->hdr_req.session_id = hdr_req->session_id;
       
   286 	msg_struct->hdr_req.request_id = hdr_req->request_id;
       
   287 	
       
   288 	//set the error and responses..
       
   289    	msg_struct->error_type = error;
       
   290 	msg_struct->response = response;
       
   291 	
       
   292 	//send the message to the client			
       
   293 	result = MsgQSend( RESPONSE_QUEUE, (void*)msg_struct, sizeof( message_hdr_resp ),
       
   294 			 pri, timeout, &err );
       
   295 			 	
       
   296 	free ( msg_struct );
       
   297 	
       
   298 	if ( 0 > result ) 
       
   299 		{
       
   300 		return MSG_Q_SEND_FAILED;
       
   301 		}
       
   302 			
       
   303 	return 0;
       
   304 	
       
   305 	}