示例#1
0
/************************************************************************
*	Function :	removeServiceTable
*
*	Parameters :
*		IXML_Node *node ;	XML node information
*		service_table *in ;	service table from which services will be 
*							removed
*
*	Description :	This function assumes that services for a particular 
*		root device are placed linearly in the service table, and in the 
*		order in which they are found in the description document
*		all services for this root device are removed from the list
*
*	Return : int ;
*
*	Note :
************************************************************************/
int
removeServiceTable( IXML_Node * node,
                    service_table * in )
{
    IXML_Node *root = NULL;
    IXML_Node *currentUDN = NULL;
    DOMString UDN = NULL;
    IXML_NodeList *deviceList = NULL;
    service_info *current_service = NULL;
    service_info *start_search = NULL;
    service_info *prev_service = NULL;
    long unsigned int NumOfDevices = 0lu;
    long unsigned int i = 0lu;

    if( getSubElement( "root", node, &root ) ) {
        current_service = in->serviceList;
        start_search = in->serviceList;
        deviceList =
            ixmlElement_getElementsByTagName( ( IXML_Element * ) root,
                                              "device" );
        if( deviceList != NULL ) {
            NumOfDevices = ixmlNodeList_length( deviceList );
            for( i = 0lu; i < NumOfDevices; i++ ) {
                if( ( start_search )
                    && ( ( getSubElement( "UDN", node, &currentUDN ) )
                         && ( UDN = getElementValue( currentUDN ) ) ) ) {
                    current_service = start_search;
                    /*Services are put in the service table in the order in which they appear in the  */
                    /*description document, therefore we go through the list only once to remove a particular */
                    /*root device */
                    while( ( current_service )
                           && ( strcmp( current_service->UDN, UDN ) ) ) {
                        current_service = current_service->next;
			if( current_service != NULL) 
                        	prev_service = current_service->next;
                    }
                    while( ( current_service )
                           && ( !strcmp( current_service->UDN, UDN ) ) ) {
                        if( prev_service ) {
                            prev_service->next = current_service->next;
                        } else {
                            in->serviceList = current_service->next;
                        }
                        if( current_service == in->endServiceList )
                            in->endServiceList = prev_service;
                        start_search = current_service->next;
                        freeService( current_service );
                        current_service = start_search;
                    }
                    ixmlFreeDOMString( UDN );
                    UDN = NULL;
                }
            }

            ixmlNodeList_free( deviceList );
        }
    }
    return 1;
}
///<summary> Completely initializes a new POLY connection that has been recieved. Uses synchronous reads and sends. </summary>
///<param name='connection'> Connection socket for the incoming connection. </param>
///<param name='connection_info'> The connection info object associated with this connection. </param>
///<param name='out_connectionPointer'> (OUT) The connection object for the newly initialized connection. </param>
///<returns> Error code on failure, connection realm on success. <returns>
int initializeIncomingConnection(void *connection,  void** out_connectionPointer)
{

	// send greeting
	if (sendGreeting(connection) == POLY_REALM_FAILED) return POLY_REALM_FAILED;

	uint8_t buffer[8]; // create a re-usable buffer for operations

	// attempt to recieve encryption setting
	if (2 != sockRecv(connection, buffer, 2)) return POLY_REALM_FAILED;

	// handle encryption negotiation
	if (getShortFromBuffer(buffer) != 0) return POLY_REALM_FAILED; // TODO: encryption not yet implemented

	// attempt to recieve realm code
	if (2 != sockRecv(connection, buffer, 2)) return POLY_REALM_FAILED;

	POLYM_CONNECTION_INFO *connection_info;

	// connection realm
	switch (getShortFromBuffer(buffer))
	{

	case POLY_REALM_PEER:

	{
		uint16_t connectionID = allocateNewPeer(&connection_info);

		// initalize the peer connection
		initializeNewPeerInfo(connection_info, connection, connectionID);

		//construct the return buffer
		buffer[0] = 0;
		buffer[1] = 0; // set the first two bytes (result code) to 0 to indicate success

		if (2 != sockSend(connection, buffer, 2)) //send the success code and peer ID
		{
			freePeer(connectionID);
			return POLY_REALM_FAILED;
		}
		return POLY_REALM_PEER;
	}

	case POLY_REALM_SERVICE:
	{
		// this connection is a service
		

		//attempt to recieve service string size
		if (2 != sockRecv(connection, buffer, 2)) return POLY_REALM_FAILED;

		// check validity of service string size, initialize string
		int serviceStringSize = getShortFromBuffer(buffer);
		if (serviceStringSize == 0) return POLY_REALM_FAILED;

		// initialize new service connection
		uint16_t connectionID = allocateNewService(&connection_info);
		connection_info->realm_info.service.serviceString = allocateServiceString(serviceStringSize);

		//get the service string
		if (serviceStringSize != sockRecv(connection, connection_info->realm_info.service.serviceString->buf, serviceStringSize))
		{
			freeService(connectionID);
			return POLY_REALM_FAILED;
		}

		// TODO: require user approval

		// construct the last send buffer to send
		buffer[0] = 0;
		buffer[1] = 0; // set the first two bytes (result code) to 0 to indicate success

		if (2 != sockSend(connection, buffer, 2))
		{
			// if for some reason this fails, we need to remove the connection from the service list
			freeService(connectionID);
			return POLY_REALM_SERVICE; // do not return fail so that the connection isn't closed twice
		}
		connection_info->connectionID = connectionID;
		connection_info->realm = POLY_REALM_SERVICE;
		return POLY_REALM_SERVICE;
	}

	case POLY_REALM_CLIENT:

	{
		uint16_t connectionID = allocateNewClient(&connection_info);

		// initalize the client connection
		connection_info->connectionID = connectionID;
		connection_info->realm = POLY_REALM_CLIENT;

		//construct the return buffer
		buffer[0] = 0;
		buffer[1] = 0; // set the first two bytes (result code) to 0 to indicate success

		if (2 != sockSend(connection, buffer, 2)) //send the success code and peer ID
		{
			freeClient(connectionID);
			return POLY_REALM_FAILED;
		}
		
		return POLY_REALM_CLIENT;

	default:
		return POLY_REALM_FAILED;

	}
	}
}