summaryrefslogtreecommitdiff
path: root/orkbasecxx/OrkTrack.cpp
blob: f0531c10cbf44618e524882afcf812632d1d3664 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
 * Oreka -- A media capture and retrieval platform
 *
 * Copyright (C) 2005, orecx LLC
 *
 * http://www.orecx.com
 *
 */

#include "OrkTrack.h"
#include "LogManager.h"
#include "ace/Thread_Manager.h"
#include "messages/InitMsg.h"
#include "ConfigManager.h"

#ifdef WIN32
#define snprintf _snprintf
#endif

void OrkTrack::Initialize()
{
	CStdString logMsg;

	for(std::list<CStdString>::iterator it = CONFIG.m_trackerHostname.begin(); it != CONFIG.m_trackerHostname.end(); it++)
	{
		CStdString trackerHostname = *it;
		OrkTrackHost *oth = (OrkTrackHost *)malloc(sizeof(OrkTrackHost));

		memset(oth, 0, sizeof(OrkTrackHost));
		snprintf(oth->m_serverHostname, sizeof(oth->m_serverHostname), "%s", trackerHostname.c_str());
		oth->m_serverPort = CONFIG.m_trackerTcpPort;

		if (!ACE_Thread_Manager::instance()->spawn(ACE_THR_FUNC(OrkTrack::Run), (void*)oth, THR_DETACHED))
		{
			logMsg.Format("OrkTrack::Initialize(): Failed to start thread for %s,%d", oth->m_serverHostname, oth->m_serverPort);
			LOG4CXX_WARN(LOG.rootLog, logMsg);
			free(oth);
		}
	}
}

void OrkTrack::Run(void* args)
{
	OrkTrackHostRef hostRef;
	OrkTrackHost *pHost = (OrkTrackHost *)args;
	InitMsgRef msgRef(new InitMsg());
	char host[255];
	time_t reportErrorLastTime = 0;
	CStdString serverHostname;
	CStdString logMsg;

	ACE_OS::hostname(host, sizeof(host));

	hostRef.reset(pHost);
	serverHostname = hostRef->m_serverHostname;
	msgRef->m_name.Format("orkaudio-%s", host);
	msgRef->m_hostname = host;
	msgRef->m_type = "A";
	msgRef->m_tcpPort = 59140;
	msgRef->m_contextPath = "/audio";
	msgRef->m_absolutePath = CONFIG.m_audioOutputPath;

	OrkHttpSingleLineClient c;
	SimpleResponseMsg response;
	CStdString msgAsSingleLineString = msgRef->SerializeSingleLine();
	bool success = false;

	while (!success)
	{
		if (c.Execute((SyncMessage&)(*msgRef.get()), (AsyncMessage&)response, serverHostname, hostRef->m_serverPort, CONFIG.m_trackerServicename, CONFIG.m_clientTimeout))
		{
			success = true;
			logMsg.Format("OrkTrack::Run(): [%s,%d] success:%s comment:%s", hostRef->m_serverHostname, hostRef->m_serverPort, (response.m_success == true ? "true" : "false"), response.m_comment);
			LOG4CXX_INFO(LOG.rootLog, logMsg);
		}
		else
		{
			if(((time(NULL) - reportErrorLastTime) > 60))
			{
				reportErrorLastTime = time(NULL);
				logMsg.Format("OrkTrack::Run(): [%s,%d] Could not contact orktrack", hostRef->m_serverHostname, hostRef->m_serverPort);
				LOG4CXX_WARN(LOG.rootLog, logMsg);
			}

			ACE_OS::sleep(CONFIG.m_clientTimeout + 10);
		}
	}
}