20120603命令行下線程調用代碼----OK

dms_lib.cpp:

#include "dms_lib.h"
#include <pthread.h>
#include "PltUPnP.h"
#include "PltFileMediaServer.h"

NPT_SET_LOCAL_LOGGER("platinum.media.server")

PLT_UPnP upnp;
PLT_DeviceHostReference device;
pthread_t thread;

int stop_dms();
void* start_dms(void*)
{	
	// setup Neptune logging
    NPT_LogManager::GetDefault().Configure("plist:.level=INFO;.handlers=ConsoleHandler;.ConsoleHandler.colors=off;.ConsoleHandler.filter=42");
	NPT_LOG_INFO("enter start_dms()\n");
	/* for DLNA faster testing */
	PLT_Constants::GetInstance().SetDefaultDeviceLease(30.);
	device = new PLT_FileMediaServer("/data/local", 
                                "1111111111UPnP Media Server",
                                false,
                                "1111111111111");
    NPT_List<NPT_IpAddress> list;
    //NPT_CHECK_SEVERE(PLT_UPnPMessageHelper::GetIPAddresses(list));
    PLT_UPnPMessageHelper::GetIPAddresses(list);
    NPT_String ip = list.GetFirstItem()->ToString();

    device->m_ModelDescription = "Platinum File Media Server";
    device->m_ModelURL = "http://www.plutinosoft.com/";
    device->m_ModelNumber = "1.0";
    device->m_ModelName = "Platinum File Media Server";
    device->m_Manufacturer = "Plutinosoft";
    device->m_ManufacturerURL = "http://www.plutinosoft.com/";

    upnp.AddDevice(device);
    
    NPT_String uuid = device->GetUUID();
	upnp.Start();
    //NPT_CHECK_SEVERE(upnp.Start());

    NPT_LOG_INFO_2("baojinyu,debug info:ip-->%s,uuid--->%s", 
					(const char*)ip, 
					(const char*)uuid);
	while(1)
	{
	}
	stop_dms();
	pthread_exit(NULL);
	return NULL;
		
}

int stop_dms()
{
	NPT_LOG_INFO("baojinyu--------dms_lib---->stop_dms.\n");	
	upnp.Stop();
	return 0;
}

int dmsThreadBegin()
{
	int dmsT;
	int retval;
	retval = pthread_create(&thread, NULL, start_dms, (void*)dmsT); 
	if(retval != 0)
	{
		NPT_LOG_INFO("baojinyu---------------create thread fail!\n");
		
		return -1;
	}
	else
	{
		NPT_LOG_INFO("baojinyu----------------pthread_create success.\n");  
		
		return 0;
	}
	return 0;                                
}

int dmsThreadEnd()
{
	//pthread_join();
	stop_dms();
	pthread_exit(NULL);//should be:send command to thread to end itself.
	//pthread_cancel(thread); //there is no this function.
	return 0;
}





dms_lib.h:

#ifndef DMS_LIB_H
#define DMS_LIB_H

int dmsThreadBegin();
//int dmsThreadEnd();

#endif //DMS_LIB_H


main.cpp:

#include "dms_lib.h"
/*----------------------------------------------------------------------
|   main
+---------------------------------------------------------------------*/
int
main(int /* argc */, char** argv)
{
    dmsThreadBegin();
    while(1)
    {
	}
    //dmsThreadEnd();

    return 0;
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章