int receiveFileAsynchronously(int connectionId, struct packet *recvPacket)
{
    struct list *connection = getNodeByID(connectionList, connectionId);
    struct host *source = (struct host *) connection->value;

    if (connection->filePointer == NULL) {
        printf("Started receiving file: %s at %s from %s/%s.\n$",
               recvPacket->header->fileName, printCurrentTime(), source->hostName, source->port);
        fflush(stdout);
        // Create file where data will be stored
        char *filename = recvPacket->header->fileName;
        char *tempfilename = stringConcat("./", filename);
        FILE *fp = fopen(tempfilename, "wb"); //for testing it should be filename
        if (fp == NULL) {
            printf("Error opening file.\n");
            deletePacketAndMessage(recvPacket);
            return -1;
        }

        //update the File pointer to the connection
        connection->filePointer = fp;

        //write the packet received
        int written_bytes = fwrite(recvPacket->message, 1, recvPacket->header->length, connection->filePointer);
        deletePacketAndMessage(recvPacket);

    }
    else {
        //write the packet received
        int written_bytes = fwrite(recvPacket->message, 1, recvPacket->header->length, connection->filePointer);
        deletePacketAndMessage(recvPacket);
    }
    return 0;
}
/** Sends a string to the specified node **/
void LithneClass::println( uint8_t _id, String _stringArg )
{
	/*	Retrieve the node the user refers to	*/
	Node * receivingNode	=	getNodeByID( _id );
	if (receivingNode != NULL)
	{
		println(receivingNode, _stringArg);
	}
}
/** Check whether the incomingMessage came from a particular node ID.
**/
bool LithneClass::isFromNodeID( uint8_t _id )
{
	bool _equal	=	false;
	Node * curNode = getNodeByID( _id );
	if( curNode != NULL )
	{
		_equal	=	equals(curNode->getAddress64(), incomingMessage.fromXBeeAddress64() );
	}
	return _equal;
}
/** Set the address of the receiving XBee by using the ID (not the same as array position in the Nodes array!) 
	The ID is supplied in the addNode function
**/
void LithneClass::toID( uint8_t _id )
{
	/*	Retrieve the node the user refers to	*/
	Node * receivingNode	=	getNodeByID( _id );
	/*	From this node, get the 16-bit and 64-bit address and
		write it to the outgoing message	*/
	if (receivingNode != NULL)
	{
		outgoingMessage.setRecipient64( receivingNode->getAddress64() );
		outgoingMessage.setRecipient16( receivingNode->getAddress16() );
		// delete receivingNode;
	}
}
/**	Checks whether there is a node with the specified ID in the memory	**/
bool LithneClass::nodeKnown( uint8_t _id )
{
	/*	getNodeByID returns the node with the specific identifier, or a new
		node with unknown data (id and address). If this is the case, the
		node is thus not known. Otherwise, it is known	*/
	if( getNodeByID( _id ) == NULL )
	{
		return false;
	}
	else
	{
		return true;
	}
}
void ShaderEditor::loadNodeConnections(Lumix::InputBlob& blob, Node& node)
{
	int size;
	blob.read(size);
	for(int i = 0; i < size; ++i)
	{
		int tmp;
		blob.read(tmp);
		node.m_inputs[i] = tmp < 0 ? nullptr : getNodeByID(tmp);
		blob.read(tmp);
		if(node.m_inputs[i]) node.m_inputs[i]->m_outputs[tmp] = &node;
	}

	blob.read(size);
	for(int i = 0; i < size; ++i)
	{
		int tmp;
		blob.read(tmp);
		node.m_outputs[i] = tmp < 0 ? nullptr : getNodeByID(tmp);
		blob.read(tmp);
		if(node.m_outputs[i]) node.m_outputs[i]->m_inputs[tmp] = &node;
	}
}
/** Check if a new DB measurement is available for a particular node (identified by Node ID) **/
bool LithneClass::newDBForNode( uint8_t _id )
{
	bool newMeasure = false;	//reset the flag
	Node * currentNode	= getNodeByID( _id );
	
	if (currentNode != NULL)
	{
		if( currentNode->isNewMeasurement() )
		{
			/*
			Serial.print("NEW BD INCOMING on node : ");
			Serial.println(_id);*/
			newMeasure = true;
		}
	}
	// delete currentNode;
	return newMeasure;
}
/** Return the DB measure for the specified node **/
uint16_t LithneClass::getDB( uint8_t id )
{
	Node * refNode = getNodeByID(id);
	uint16_t dbStrength = 0;
	 
	if (refNode != NULL) 
	{
		dbStrength = refNode->getDB();
		
		/*Serial.print("Getting DB for nodeId ");
		Serial.print(id);
		Serial.print("(getId:  ");
		Serial.print(refNode->getID());
		Serial.print("). found value ");
		Serial.println(dbStrength); */
	}
	return dbStrength;
}
int okPacketHandler(int connectionId, struct packet *recvPacket)
{
    struct list *connection = getNodeByID(connectionList, connectionId);
    struct host *source = (struct host *) connection->value;

    if (connection->filePointer != NULL) {
        //print completion of file download
        printf("Finished receiving file: %s at %s from %s/%s.\n",
               recvPacket->header->fileName, printCurrentTime(), source->hostName, source->port);

        //close the file pointer
        fclose(connection->filePointer);

        //make the file pointer in the connection to indicate that no file operation is in progress
        connection->filePointer = NULL;
    }
    return 0;
}
/** Transmit a DB request (RSSI, signal strength) by using the node identifier **/
void LithneClass::sendDBRequest( uint8_t _id )
{
	Node * receiverNode = getNodeByID( _id );
	
	if (receiverNode != NULL)
	{
		sendDBRequest( receiverNode->getAddress64(), receiverNode->getAddress16() );
	}
	// delete receiverNode;
	/*	This code has been changed, but not tested!
 	uint16_t _addr16 		=	receivingNode->getAddress16();
 	
 	if ( _addr16 != UNKNOWN_NODE ) 
	{
		sendDBRequest16( _addr16 );
	}
	// the 16-bit address is not known, so we send a 64B request 
	else 
 	{
 		sendDBRequest( receivingNode->getAddress64() );
 	}
 	*/
}