isolationserver/isoserver/src/isofetchcontactlist.c
changeset 10 59927b2d3b75
parent 0 d0f3a028347a
equal deleted inserted replaced
0:d0f3a028347a 10:59927b2d3b75
     1 /*
       
     2 * ============================================================================
       
     3 *  Name        : isofetchcontactlist.h
       
     4 *  Part of     : isolation server.
       
     5 *  Version     : %version: 17 %
       
     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 
       
    32 #include <stdio.h>
       
    33 #include <stdlib.h>
       
    34 #include <string.h>
       
    35 #include <signal.h>
       
    36 
       
    37 #include <dbus/dbus-glib.h>
       
    38 #include <glib.h>
       
    39 
       
    40 #include "isofetchcontactlist.h"
       
    41 #include "isoutils.h"
       
    42 #include "msg_enums.h"
       
    43 #include "msgliterals.h"
       
    44 
       
    45 const guint KMaxContactFetchCount = 500;
       
    46 
       
    47 static guint cur_mem_len;
       
    48 static guint local_mem_len;
       
    49 static guint remote_mem_len;
       
    50 
       
    51 static gint cur_last_index = 0;
       
    52 static gint local_last_index = 0;
       
    53 static gint remote_last_index = 0;
       
    54 
       
    55 
       
    56 /*! \brief This function is called as a callback to get the contact names from 
       
    57  *  contact handles. Contact names are sent to client in multiple packets of
       
    58  *  size MAX_MSG_SIZE
       
    59  *
       
    60  *  \param proxy unused
       
    61  *  \param handles_names 2D array of contact names
       
    62  *  \param error error if any 
       
    63  *  \param userdata msg_type
       
    64  */
       
    65 static void inspect_handles_cb( DBusGProxy *proxy,char **handles_names,
       
    66 				 GError *error, gpointer userdata ) 
       
    67 	{
       
    68 	guint i;
       
    69 	gint err = 0;
       
    70 	gint* msg_type = ( gint* ) userdata;
       
    71 	
       
    72 	gchar** members = NULL;
       
    73 	guint* mem_count = 0;
       
    74 	gint* last_index = NULL;
       
    75 	
       
    76 	
       
    77 	iso_logger( "%s", "in -- inspect_handles_cb" );
       
    78 	
       
    79 	if ( !handles_names || error )
       
    80 		{
       
    81 		//Handles error
       
    82 		iso_logger( "%s", "handle names error" );
       
    83 		free ( msg_type );
       
    84 		g_error_free(error);
       
    85 		return;
       
    86 		}
       
    87 	UNUSED_FORMAL_PARAM(proxy);
       
    88 	//Switch to the type of the message
       
    89 	//and get corresponding pointers to members and pointer to last index
       
    90 	switch ( *msg_type ) 
       
    91 		{
       
    92 		case ECurrent_Member_Contacts:
       
    93 			members = globalCon.current_members_names;
       
    94 			mem_count = &cur_mem_len;
       
    95 			last_index = &cur_last_index;
       
    96 			iso_logger( "%s count %d", "current members", mem_count );
       
    97 			break;
       
    98 		case ELocal_Pending_Contacts:
       
    99 			members = globalCon.local_pending_names;
       
   100 			mem_count = &local_mem_len;
       
   101 			last_index = &local_last_index;
       
   102 			iso_logger( "%s count %d", "local members", mem_count );
       
   103 			break;
       
   104 		case ERemote_Pending_Contacts:
       
   105 			members = globalCon.remote_pending_names;
       
   106 			mem_count = &remote_mem_len;
       
   107 			last_index = &remote_last_index;
       
   108 			iso_logger( "%s count %d", "remote members", mem_count );
       
   109 			break;
       
   110 			
       
   111 		default :
       
   112 		//Error in msg_type
       
   113 		//Wrong message type
       
   114 		//quit from here
       
   115 			free ( msg_type );
       
   116 			return;
       
   117 		}
       
   118 	
       
   119 	
       
   120 	//Add the contacts to the global member
       
   121 	for( i=0; handles_names[i]; i++ )
       
   122 		{
       
   123 		iso_logger( "members %s ",handles_names[i] );
       
   124 		members[*last_index] = handles_names[i];
       
   125 		(*last_index)++;
       
   126 		}
       
   127 		
       
   128 	iso_logger( "last index is %d", *last_index );
       
   129 	//If all the contacts are fetched send them to client
       
   130 	if ( *mem_count == *last_index )	
       
   131 		{
       
   132 		//send all contacts to client
       
   133 		err = send_contacts_to_client( members, *mem_count, *msg_type );
       
   134 		*last_index = 0;
       
   135 		if ( err ) 
       
   136 			{
       
   137 			//How to handle the error?
       
   138 			for ( i = 0; i < *mem_count; i++ ) 
       
   139 				{
       
   140 				free ( members[i] );
       
   141 				}
       
   142 			iso_logger( "%s", "There was error in send_contacts_to_client" );
       
   143 			free ( msg_type );
       
   144 			*mem_count = 0;
       
   145 			return;
       
   146 			}
       
   147 		*mem_count = 0;
       
   148 		
       
   149 		}
       
   150 	free ( msg_type );
       
   151 	iso_logger( "%s", "out -- inspect_handles_cb" );
       
   152 	}
       
   153 	
       
   154 /*! \brief This function is called if there are 0 contacts or there was an error
       
   155  *
       
   156  *  \param contact_type : Type of the contacts
       
   157  *  \param err : err if any, 0 otherwise
       
   158  */
       
   159 static gint send_fetch_contacts_error( gint contact_type , gint err ) 
       
   160 	{
       
   161 	
       
   162 	int result = 0;
       
   163 	int pri = MSG_PRI_NORMAL;
       
   164 	int timeout1 = TIME_OUT;
       
   165 	message_hdr_resp* msg_struct = NULL;
       
   166 	//control is here coz.. there are no contacts in list
       
   167 	
       
   168 	msg_struct = ( message_hdr_resp* ) malloc ( sizeof( message_hdr_resp ) );
       
   169 	if ( NULL == msg_struct ) 
       
   170 		{
       
   171 		return 	MEM_ALLOCATION_ERROR;
       
   172 		}
       
   173 	//initalize memory to zero
       
   174 	memset( msg_struct, '\0', sizeof( message_hdr_resp ) );
       
   175 	//create the header response
       
   176 	msg_struct->hdr_req.message_type = contact_type;
       
   177 	msg_struct->hdr_req.protocol_id = 0;
       
   178 	msg_struct->hdr_req.session_id = 0;
       
   179 	msg_struct->hdr_req.request_id = 0;
       
   180 	//Set error types and continue flag
       
   181 	if ( err < 0 ) 
       
   182 		{
       
   183 		//There was some error, pass on the mapped error
       
   184 		//to client
       
   185 		msg_struct->error_type = err; 
       
   186 		msg_struct->response = 0;	
       
   187 		}
       
   188 	else 
       
   189 		{
       
   190 		msg_struct->error_type = 0;
       
   191 		msg_struct->response = 1;	
       
   192 		}
       
   193 	msg_struct->continue_flag = 0;
       
   194 	//send the message to client that there are no contacts in the list
       
   195 	result = MsgQSend( RESPONSE_QUEUE, ( char* )msg_struct, sizeof( message_hdr_resp ),
       
   196 	 					pri, timeout1, &err );	
       
   197 	
       
   198 	free ( msg_struct );
       
   199 	 					
       
   200 	if ( result < 0 )
       
   201 		{
       
   202 		// failed to deliever
       
   203 		return MSG_Q_SEND_FAILED;
       
   204 		}
       
   205 	return 0;
       
   206 	}
       
   207 
       
   208 /*! \brief This function is a callback for request for the contacts 
       
   209  *  in local pending contatct list from server. This is 
       
   210  *  only called for publish channel members. fetch_contacts is called 
       
   211  *  to get the contact names from the handles. If there are no conatcts in 
       
   212  *  the list it calls send_fetch_contacts_error else requests for the contact names
       
   213  *  from the contact handles
       
   214  *
       
   215  *  \param proxy unused 
       
   216  *  \param current_members array of handles of current contatcs
       
   217  *  \param local_pending_members unused 
       
   218  *  \param remote_pending_members array of handles of remote pending contatcs
       
   219  *  \param error error if any
       
   220  *  \param userdata unused 
       
   221  */
       
   222 static void get_roster_local_member_cb( DBusGProxy *proxy,GArray* current_members, 
       
   223 			GArray* local_pending_members, GArray* remote_pending_members, 
       
   224 			GError *error, gpointer userdata ) 
       
   225 	{
       
   226 	
       
   227 	gint err = 0;
       
   228 	gint* msg_type = ( gint* ) malloc ( sizeof ( gint ) );
       
   229 	
       
   230 	iso_logger( "%s", "in -- get_roster_member_cb_local" );
       
   231 	UNUSED_FORMAL_PARAM(proxy);
       
   232 	UNUSED_FORMAL_PARAM(remote_pending_members);
       
   233 	UNUSED_FORMAL_PARAM(userdata);
       
   234 	UNUSED_FORMAL_PARAM(current_members);
       
   235 	if ( NULL != error ) 
       
   236 		{
       
   237 		//err in fetching contacts, send failed
       
   238 		//map the error code to isoserver
       
   239 		err =  error->code;
       
   240 		//Send that there are no contacts in the local pending
       
   241 		send_fetch_contacts_error( EFetch_Contacts_Error, err );
       
   242 		return;
       
   243 		}
       
   244 	//store the no. of lccal pending contacts
       
   245 	local_mem_len = local_pending_members->len; 
       
   246 	
       
   247 	if ( !local_last_index && local_mem_len ) 
       
   248 		{
       
   249 		//Is this the first time to be here..
       
   250 		//allocate memory
       
   251 		globalCon.local_pending_names = g_new0 ( char *, local_mem_len + 1);	
       
   252 		if ( NULL == globalCon.local_pending_names ) 
       
   253 			{
       
   254 			return ;
       
   255 			//err?
       
   256 			}
       
   257 		}
       
   258 	//ownership of this pointer is trnasfered to fetch_contacts
       
   259 	*msg_type = ELocal_Pending_Contacts;
       
   260 	//Fetch the contacts given the handles
       
   261 	err = fetch_contacts( local_pending_members, msg_type );
       
   262 	if ( err < 0 ) 
       
   263 		{
       
   264 		return;
       
   265 		}
       
   266 		
       
   267 	iso_logger( "%s", "out -- get_roster_member_cb_local" );		
       
   268 	}
       
   269 
       
   270 /*! \brief Requests the server to get contact names from the handles
       
   271  *  The contact names are returned in inspect_handles_cb. From there
       
   272  *  contacts are sent to client
       
   273  *
       
   274  *  \param contact_mem : handles to contact members
       
   275  *  \param msg_type : type of the message, which wil be passed to cb
       
   276  *  \return  Errors if any, else 0
       
   277  */
       
   278 static gint fetch_contacts( GArray* contact_mem, gint* msg_type ) 
       
   279 	{
       
   280 	
       
   281 	guint fetch_count=0;
       
   282 	guint fetch_loop_count=0;	
       
   283 	GArray* fetch_members = NULL;
       
   284 	
       
   285 	DBusGProxyCall* call_id = NULL;
       
   286 	TpConn* conn = globalCon.conn;
       
   287 	
       
   288 	gint i = 0;
       
   289 	
       
   290 	//allocate memory for the contacts to be fetched	
       
   291 	fetch_members = g_array_new (FALSE, FALSE, sizeof (guint32));
       
   292 	if ( NULL == fetch_members ) 
       
   293 		{
       
   294 		free ( msg_type );
       
   295 		return MEM_ALLOCATION_ERROR;
       
   296 		//err?
       
   297 		}
       
   298 	
       
   299 	//check if there are contacts to be retrived 
       
   300 	if ( contact_mem && contact_mem->len > 0 )
       
   301 		{
       
   302 
       
   303 		fetch_count = 0;
       
   304 	
       
   305 		//Divide contacts into a 'n' no. of groups .. n is calculated as below..
       
   306 		fetch_loop_count = ( contact_mem->len ) / KMaxContactFetchCount + ( 
       
   307 				( contact_mem->len % KMaxContactFetchCount ) ? 1 : 0 );
       
   308 		
       
   309 		//Get the contacts in terms of fetch_count
       
   310 		for ( i=0; i<fetch_loop_count; i++ )
       
   311 			{
       
   312 			g_array_remove_range( fetch_members,0,fetch_count);
       
   313 			
       
   314 			fetch_count = ( contact_mem->len <= KMaxContactFetchCount ) 
       
   315 					? contact_mem->len : KMaxContactFetchCount;
       
   316 			//Fetch the contacts 
       
   317 			g_array_append_vals (fetch_members,contact_mem->data,fetch_count);
       
   318 			g_array_remove_range(contact_mem,0,fetch_count);
       
   319 			//For the given handles contacts will be fetched in inspect_handles_cb
       
   320 			call_id = tp_conn_inspect_handles_async( DBUS_G_PROXY(conn),
       
   321 					       TP_CONN_HANDLE_TYPE_CONTACT ,fetch_members,
       
   322 					       inspect_handles_cb, msg_type );
       
   323 			//If call_id is null return from here..					       
       
   324 			if ( NULL == call_id ) 
       
   325 				{
       
   326 				free ( msg_type );
       
   327 				g_array_free( fetch_members, TRUE );
       
   328 				return MEM_ALLOCATION_ERROR;	
       
   329 				}
       
   330 					       	
       
   331 			}
       
   332 		}
       
   333 	else if ( contact_mem && contact_mem->len == 0 ) 
       
   334 		{
       
   335 		gint err = 0;
       
   336 		//There are no contacts for this type(cur or local or remote) of contacts
       
   337 		//Send that to client
       
   338 		err = send_fetch_contacts_error( *msg_type, 0 );
       
   339 		free ( msg_type );
       
   340 		if ( err < 0 ) 
       
   341 			{
       
   342 			//Free the msg_type.. Ownership was transfered to this function
       
   343 			
       
   344 			g_array_free( fetch_members, TRUE );
       
   345 			return err;	
       
   346 			}
       
   347 		
       
   348 		}
       
   349 	//msg_type is either freed in else part or is freed in inspect_handles_cb
       
   350 	//no need to free here
       
   351 	g_array_free( fetch_members, TRUE );
       
   352 	//Should the fetch_contacts be deleted?	
       
   353 	return 0;	
       
   354 	}
       
   355 	
       
   356 /*! \brief This function is a callback for request for the contacts 
       
   357  *  in msg_type(subscribe or publish) contatct list from server. This is 
       
   358  *  only called for subscribe channel members. fetch_contacts is called 
       
   359  *  to get the contact names from the handles. If there are no conatcts in 
       
   360  *  the list it calls send_fetch_contacts_error else requests for the contacts 
       
   361  *  from the handles
       
   362  *
       
   363  *  \param proxy unused 
       
   364  *  \param current_members array of handles of current contatcs
       
   365  *  \param local_pending_members unused 
       
   366  *  \param remote_pending_members array of handles of remote pending contatcs
       
   367  *  \param error error if any
       
   368  *  \param userdata unused 
       
   369  */
       
   370 static void get_roster_member_cb( DBusGProxy *proxy,GArray* current_members, 
       
   371 			GArray* local_pending_members, GArray* remote_pending_members, 
       
   372 			GError *error, gpointer userdata ) 
       
   373 	{
       
   374 	
       
   375 	int err = 0;
       
   376 	
       
   377 	guint j = 0;
       
   378 	
       
   379 	iso_logger( "%s", "in -- get_roster_member_cb" );
       
   380 	UNUSED_FORMAL_PARAM(proxy);
       
   381 	UNUSED_FORMAL_PARAM(userdata);
       
   382 	UNUSED_FORMAL_PARAM(local_pending_members);
       
   383 	if ( NULL != error ) 
       
   384 		{
       
   385 		//err in fetching contacts, send failed
       
   386 		//map the error code to isoserver
       
   387 		err =  error->code;
       
   388 		send_fetch_contacts_error( EFetch_Contacts_Error, err );
       
   389 		return;
       
   390 		}
       
   391 	//store the no. of current members
       
   392 	cur_mem_len = current_members->len;
       
   393 	//store the no. of remote members
       
   394 	remote_mem_len = remote_pending_members->len;
       
   395 	
       
   396 				
       
   397 	if ( !cur_last_index && cur_mem_len ) 
       
   398 		{
       
   399 		//Is this the first time to be here..
       
   400 		//allocate memory
       
   401 		globalCon.current_members_names = g_new0 ( char *, cur_mem_len + 1);	
       
   402 		if ( NULL == globalCon.current_members_names ) 
       
   403 			{
       
   404 			return ;
       
   405 			//err?
       
   406 			}
       
   407 		}
       
   408 		
       
   409 	if ( !remote_last_index && remote_mem_len ) 
       
   410 		{
       
   411 		//Is this the first time to be here..
       
   412 		//allocate memory
       
   413 		globalCon.remote_pending_names = g_new0 ( char *, remote_mem_len + 1);	
       
   414 		if ( NULL == globalCon.remote_pending_names ) 
       
   415 			{
       
   416 			return ;
       
   417 			//err?
       
   418 			}
       
   419 		}
       
   420 		
       
   421 	
       
   422 	//loop thru : 2 for current and remote
       
   423 	for ( j = 0; j < 2; j++ ) 
       
   424 		{
       
   425 		//This msg_type is sent to inspect_handles_cb
       
   426 		//Where it wil be deleeted
       
   427 		gint* msg_type = ( gint* ) malloc ( sizeof ( gint ) );
       
   428 		GArray* contact_mem = NULL;
       
   429 		switch ( j ) 
       
   430 			{
       
   431 			
       
   432 			case 0 : 
       
   433 			//ask for the current members first
       
   434 				*msg_type = ECurrent_Member_Contacts;
       
   435 				contact_mem = current_members;
       
   436 				break;
       
   437 				
       
   438 			case 1 :
       
   439 			//ask for remote pending contacts
       
   440 				*msg_type = ERemote_Pending_Contacts;
       
   441 				contact_mem = remote_pending_members;
       
   442 				break;
       
   443 				
       
   444 			default:
       
   445 				break;
       
   446 			}
       
   447 		//Fetch the contacts given the handles 
       
   448 		//ownership of msg_type is transfered to fetch_contacts
       
   449 		err = fetch_contacts( contact_mem, msg_type );
       
   450 		if ( err < 0 ) 
       
   451 			{
       
   452 			//There was some error
       
   453 			//return from here
       
   454 			return;
       
   455 			}
       
   456 		
       
   457 		}
       
   458 
       
   459 	iso_logger( "%s","out -- get_roster_member_cb" );
       
   460 	}
       
   461 
       
   462 
       
   463 /*! \brief this function requests for the contacts in its contatct list from server
       
   464  *  
       
   465  *  \param  type of the contacts channel( publish, subscribe )
       
   466  */
       
   467 void request_roster( enum channel_type type )
       
   468 	{
       
   469 
       
   470    	DBusGProxyCall* call_id = NULL;
       
   471    	
       
   472    
       
   473    	iso_logger( "%s", "in -- request_roster" );
       
   474 
       
   475 	//With the interface get the contacts 
       
   476 	//Request for all of the contacts in subscribe channel( remote and current 
       
   477 	//members )
       
   478 	if ( ESubscribe_Channel == type ) 
       
   479 		{
       
   480 		call_id = tp_chan_iface_group_get_all_members_async( globalCon.group_iface_subscribe, 
       
   481 				get_roster_member_cb, NULL );
       
   482 		//If call_id is NULL return from here..
       
   483 		if ( NULL == call_id ) 
       
   484 			{
       
   485 			return ;
       
   486 			}	
       
   487 		}
       
   488 	else if ( EPublish_Channel == type ) 
       
   489 		{
       
   490 		//Request for all of the contacts in publish channel
       
   491 		call_id = tp_chan_iface_group_get_all_members_async( globalCon.group_iface_publish, 
       
   492 				get_roster_local_member_cb, NULL );
       
   493 		//If call_id is NULL return from here..
       
   494 		if ( NULL == call_id ) 
       
   495 			{
       
   496 			return ;
       
   497 			}
       
   498 		}
       
   499 	
       
   500 	iso_logger( "%s", "out -- request_roster" );	
       
   501 	}
       
   502 
       
   503 /*! \brief Sends contacts to the client. Contact names are sent to client in multiple 
       
   504  *  packets of size less than or equal to MAX_MSG_SIZE. Contact will be sent as a whole. 
       
   505  *  A single contact will not be sent in two consecutive packets
       
   506  *
       
   507  *  \param members : Contacts to be sent to client
       
   508  *  \param mem_count : no. of contacts 
       
   509  *  \param contact_type : type of the contact
       
   510  *  \return error code..
       
   511  */
       
   512 gint send_contacts_to_client( gchar** members, gint mem_count, 
       
   513 	gint contact_type ) 
       
   514 	{
       
   515 	int result = 0;
       
   516 	int pri = MSG_PRI_NORMAL;
       
   517 	int timeout1 = TIME_OUT;
       
   518 	int err = 0;
       
   519 	
       
   520 	char* memBuf = NULL;
       
   521 	int i = 0;
       
   522 	int one_msg_size = sizeof ( message_hdr_resp );
       
   523 	int mem_len = 0;
       
   524 	message_hdr_resp* msg_struct = NULL;
       
   525 	
       
   526 	iso_logger( "%s", "in -- send_contacts_to_client" );
       
   527 	
       
   528 	//allocate memory and check for the returned value
       
   529 	msg_struct = ( message_hdr_resp* ) malloc ( sizeof( message_hdr_resp ) );
       
   530 	if ( NULL == msg_struct ) 
       
   531 		{
       
   532 		return 	MEM_ALLOCATION_ERROR;
       
   533 		}
       
   534 		
       
   535 	memBuf = ( gchar* ) malloc( MAX_MSG_SIZE );
       
   536 	if ( NULL == memBuf ) 
       
   537 		{
       
   538 		// ? 
       
   539 		free ( msg_struct );
       
   540 		return MEM_ALLOCATION_ERROR;
       
   541 		}
       
   542 
       
   543 	memset( msg_struct, '\0', sizeof( message_hdr_resp ) );
       
   544 	//create the response header with type contact fetch
       
   545 	msg_struct->hdr_req.message_type = contact_type;
       
   546 	msg_struct->hdr_req.protocol_id = 0;
       
   547 	msg_struct->hdr_req.session_id = 0;
       
   548 	msg_struct->hdr_req.request_id = 0;
       
   549 	
       
   550 	memset( memBuf, '\0', MAX_MSG_SIZE );
       
   551 	//sending the req to server
       
   552 	
       
   553 	//assuming that one contact size will not be 
       
   554 	//greater than max_msg_size
       
   555 	for ( i = 0; i <= mem_count - 1; i++ ) 
       
   556 		{
       
   557 		mem_len = strlen ( members[i] ) + 1;
       
   558 		//check if this contact fits in the current message 
       
   559 		//to be sent to client
       
   560 		//one_msg_size is used to navigate thru' the buffer
       
   561 		if ( one_msg_size + mem_len < MAX_MSG_SIZE ) 
       
   562 			{
       
   563 			strcpy( memBuf + one_msg_size, members[i] );
       
   564 			one_msg_size += mem_len;
       
   565 			//if i == mem_count - 1 no need to continue
       
   566 			//Control should fall off(i.e., contacts fit in
       
   567 			//this message )
       
   568 			if ( i < mem_count - 1 )
       
   569 				{
       
   570 				continue;	
       
   571 				}
       
   572 			//If falling off mem_len should be reset because
       
   573 			//it has been already added to one_msg_size
       
   574 			mem_len = 0;
       
   575 			}
       
   576 		msg_struct->error_type = 0;
       
   577 		msg_struct->response = 1;	
       
   578 		//if i == mem_count - 1 and all contacts fits in this message
       
   579 		//to be sent to server
       
   580 		if ( i == mem_count - 1 && one_msg_size + mem_len < MAX_MSG_SIZE )	
       
   581 			{
       
   582 			msg_struct->continue_flag = 0;	
       
   583 			}
       
   584 		else 
       
   585 			{
       
   586 			msg_struct->continue_flag = 1;	
       
   587 			}
       
   588 		if ( i == mem_count - 1 && one_msg_size + mem_len > MAX_MSG_SIZE ) 
       
   589 			{
       
   590 			//One more contact is there (in which case i == mem_count - 1))
       
   591 			//And that can't be sent because of limitation of 
       
   592 			//message size..
       
   593 			//so iterate one more time
       
   594 			i--;
       
   595 			}
       
   596 		//Append '\0' to mark the end..
       
   597 		one_msg_size++;
       
   598 		*( memBuf + one_msg_size ) = '\0';
       
   599 		memcpy( memBuf, msg_struct, sizeof( message_hdr_resp ) );
       
   600 		//send to the client
       
   601 		result = MsgQSend( RESPONSE_QUEUE, memBuf, one_msg_size, pri,
       
   602 				 timeout1, &err );
       
   603 				 
       
   604 		
       
   605 		if ( result < 0 )
       
   606 			{
       
   607 			free ( memBuf );
       
   608 			iso_logger( "%s", "in -- send_contacts_to_client MSG_Q_SEND_FAILED" );
       
   609 			// failed to deliever	
       
   610 			//free the allocated memory
       
   611 			free ( msg_struct );
       
   612 			
       
   613 			return MSG_Q_SEND_FAILED;
       
   614 			}
       
   615 		
       
   616 		memset( memBuf , '\0', MAX_MSG_SIZE );
       
   617 		//reset the one_msg_size for new message to be sent 
       
   618 		one_msg_size = sizeof ( message_hdr_resp );	
       
   619 
       
   620 		}
       
   621 	//free the buffer
       
   622 	free ( memBuf );
       
   623 	
       
   624 	free ( msg_struct );
       
   625 	//Should the members be freed here?
       
   626 	for ( i = 0; i < mem_count; i++ ) 
       
   627 		{
       
   628 		iso_logger( "freeing contact %s", members[i] );
       
   629 		free ( members[i] );
       
   630 		}
       
   631 	iso_logger( "%s", "out -- send_contacts_to_client" );
       
   632 			
       
   633 	return 0;
       
   634 	}
       
   635 
       
   636 /*! \brief Documented in the header file
       
   637 */
       
   638 void fetch_cached_contacts()
       
   639     {
       
   640     
       
   641     request_roster( EPublish_Channel );
       
   642     request_roster( ESubscribe_Channel );
       
   643     
       
   644     }