<commit_before>/*©mit**************************************************************************
*                                                                              *
* This file is part of FRIEND UNIFYING PLATFORM.                               *
* Copyright (c) Friend Software Labs AS. All rights reserved.                  *
*                                                                              *
* Licensed under the Source EULA. Please refer to the copy of the MIT License, *
* found in the file license_mit.txt.                                           *
*                                                                              *
*****************************************************************************©*/
/** @file
 * 
 *  User Manager Web body
 *
 * All functions related to User Manager web calls
 *
 *  @author PS (<NAME>)
 *  @date created 29/05/2017
 */

#include <core/types.h>
#include <core/nodes.h>
#include "user_manager_web.h"
#include <system/systembase.h>
#include <system/fsys/device_handling.h>
#include <system/user/user_sessionmanager.h>
#include <util/session_id.h>

//test
//#undef __DEBUG

inline static int killUserSession( SystemBase *l, UserSession *ses )
{
	int error = 0;
	char tmpmsg[ 2048 ];
	int lenmsg = sprintf( tmpmsg, "{\"type\":\"msg\",\"data\":{\"type\":\"server-notice\",\"data\":\"session killed\"}}" );
	
	int msgsndsize = WebSocketSendMessageInt( ses, tmpmsg, lenmsg );
	
	char *uname = NULL;
	if( ses->us_User != NULL )
	{
		uname = ses->us_User->u_Name;
	}
		
	DEBUG("[UMWebRequest] user %s session %s will be removed by user %s msglength %d\n", uname, ses->us_SessionID, uname, msgsndsize );
	
	// set flag to WS connection "te be killed"
	FRIEND_MUTEX_LOCK( &(ses->us_Mutex) );
	ses->us_InUseCounter--;
	if( ses->us_WSConnections != NULL && ses->us_WSConnections->wusc_Data != NULL )
	{
		ses->us_WSConnections->wusc_Status = WEBSOCKET_SERVER_CLIENT_TO_BE_KILLED;
	}
	FRIEND_MUTEX_UNLOCK( &(ses->us_Mutex) );
	
	// wait till queue will be empty
	while( TRUE )
	{
		if( ses->us_WSConnections ->wusc_Data->wsc_MsgQueue.fq_First == NULL )
		{
			break;
		}
		usleep( 1000 );
	}
	
	error = USMUserSessionRemove( l->sl_USM, ses );	
	return error;
}

inline static int killUserSessionByUser( SystemBase *l, User *u, char *deviceid )
{
	int error = 0;
	int nrSessions = 0;
	int i;
	
	UserSession **toBeRemoved = NULL;
	
	FRIEND_MUTEX_LOCK( &u->u_Mutex );
	UserSessListEntry *usl = u->u_SessionsList;
	if( deviceid != NULL )
	{<commit_msg>Seems to fix killUserSession() crash<commit_after>/*©mit**************************************************************************
*                                                                              *
* This file is part of FRIEND UNIFYING PLATFORM.                               *
* Copyright (c) Friend Software Labs AS. All rights reserved.                  *
*                                                                              *
* Licensed under the Source EULA. Please refer to the copy of the MIT License, *
* found in the file license_mit.txt.                                           *
*                                                                              *
*****************************************************************************©*/
/** @file
 * 
 *  User Manager Web body
 *
 * All functions related to User Manager web calls
 *
 *  @author PS (<NAME>)
 *  @date created 29/05/2017
 */

#include <core/types.h>
#include <core/nodes.h>
#include "user_manager_web.h"
#include <system/systembase.h>
#include <system/fsys/device_handling.h>
#include <system/user/user_sessionmanager.h>
#include <util/session_id.h>

//test
//#undef __DEBUG

inline static int killUserSession( SystemBase *l, UserSession *ses )
{
	int error = 0;
	char tmpmsg[ 2048 ];
	int lenmsg = sprintf( tmpmsg, "{\"type\":\"msg\",\"data\":{\"type\":\"server-notice\",\"data\":\"session killed\"}}" );
	
	int msgsndsize = WebSocketSendMessageInt( ses, tmpmsg, lenmsg );
	
	char *uname = NULL;
	if( ses->us_User != NULL )
	{
		uname = ses->us_User->u_Name;
	}
		
	DEBUG("[UMWebRequest] user %s session %s will be removed by user %s msglength %d\n", uname, ses->us_SessionID, uname, msgsndsize );
	
	// set flag to WS connection "te be killed"
	FRIEND_MUTEX_LOCK( &(ses->us_Mutex) );
	ses->us_InUseCounter--;
	if( ses->us_WSConnections != NULL && ses->us_WSConnections->wusc_Data != NULL )
	{
		ses->us_WSConnections->wusc_Status = WEBSOCKET_SERVER_CLIENT_TO_BE_KILLED;
	}
	FRIEND_MUTEX_UNLOCK( &(ses->us_Mutex) );
	
	// wait till queue will be empty
	while( TRUE )
	{
		FRIEND_MUTEX_LOCK( &(ses->us_Mutex) );
		if( ses->us_WSConnections->wusc_Data->wsc_MsgQueue.fq_First == NULL )
		{
			FRIEND_MUTEX_UNLOCK( &(ses->us_Mutex) );
			break;
		}
		FRIEND_MUTEX_UNLOCK( &(ses->us_Mutex) );
		usleep( 1000 );
	}
	
	error = USMUserSessionRemove( l->sl_USM, ses );	
	return error;
}

inline static int killUserSessionByUser( SystemBase *l, User *u, char *deviceid )
{
	int error = 0;
	int nrSessions = 0;
	int i;
	
	UserSession **toBeRemoved = NULL;
	
	FRIEND_MUTEX_LOCK( &u->u_Mutex );
	UserSessListEntry *usl = u->u_SessionsList;
	if( deviceid != NULL )
	{
{'fraction_non_alphanumeric': 0.18090452261306533, 'fraction_numerical': 0.007344414379590259, 'mean_word_length': 2.466175485599464, 'pattern_counts': {'":': 8, '<': 17, '<?xml version=': 0, '>': 56, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2473274', 'n_tokens_mistral': 1719, 'n_tokens_neox': 1632, 'n_words': 399}