Using NetMessenger to interface with message_daemon

For informative purposes, I'm providing the code from BeBlurb that is responsible for delivering Blurbs to remote machines on the network. There is no networking code anywhere in BeBlurb; this one function is responsible for message delivery to remote machines.

For more information, read the message_daemon documentation.


One possibly subtle point to make here: This is a static member function that serves as a thread entry routine. As establishing connections can hang until timeout, I do this in parallel such that the connections which can be made aren't forced to wait in queue until the hanging connection times out.

long
BlurbProgressWindow::DeliverBlurb( void *data )
{
	BlurbProgressWindow *object = (BlurbProgressWindow *)data;
	
	status_t status;
	BMessage *outgoing = object->_outgoing;
	ASSERT( outgoing != 0 );

	BlurbPerson person;
	status = outgoing->FindFlat( "person", &person );
	ASSERT( status == B_OK );

	object->SetMaxValue( 2.0 );	
	object->SetTrailingText( "Connecting" );

	try {
		NetMessenger msgr( person.Host(), kBlurbSignature, -1, &status, 5e-6 );	
		if ( status != B_OK ) throw( msgr.Error() );
	
		object->Update( 1.0 );
		object->SetTrailingText( "Delivering" );

		status = msgr.SendMessage( outgoing );
		if ( status != B_OK ) throw( msgr.Error() );
	
		object->Update( 1.0 );
		object->SetTrailingText( "Finishing" );

		object->PostMessage( B_QUIT_REQUESTED, object );
	
	}

	catch ( netstatus_t netstat )
	{
		object->Lock();
		char inform[255];
		switch ( netstat )
		{
			case NET_UNKNOWN_HOST:
				sprintf( inform, "Could not resolve hostname \"%s\".", person.Host() );
				break;

			case NET_TIMED_OUT:
				sprintf( inform, "The connection timed out." );
				break;

			case NET_CANT_CONNECT:
				sprintf( inform, "The connection request was rejected." );
				break;
						
			case NET_INTERNAL_ERROR:
				sprintf( inform, "A message_daemon internal error occurred. Try restarting it." );
				break;
			
			case NET_NO_DAEMON:
				sprintf( inform, "message_daemon must be running to use BeBlurb." );
				break;
		
			case NET_DISCONNECTED:
				sprintf( inform, "The connection was disrupted." );
				break;
			
			default:
				sprintf( inform, "An unknown error occurred." );
				break;
		}
		object->_report->SetText( (const char *)inform );
		object->_bar->RemoveSelf();
		object->_root->AddChild( object->_report );
		object->_root->Draw( object->_root->Frame() );
		object->Unlock();
	}
		
	return 0;
}


jason@jeff.ces.cwru.edu
Last Updated: 03 March 98