Update heartbeat code according to review

This commit is contained in:
Mihnea Dobrescu-Balaur 2012-09-14 16:52:52 -07:00
Родитель 4f49e67adb
Коммит 9ce2b71bba
16 изменённых файлов: 52 добавлений и 103 удалений

Просмотреть файл

@ -6,10 +6,8 @@ SRCS=\
src/Buffer.cpp \
src/BufferedSocket.cpp \
src/CommandEventHandler.cpp \
src/CommandEventHandlerFactory.cpp \
src/EventHandler.cpp \
src/Hash.cpp \
src/HeartbeatEventHandlerFactory.cpp \
src/HeartbeatEventHandler.cpp \
src/Logger.cpp \
src/Logging.cpp \

Просмотреть файл

@ -751,3 +751,10 @@ CommandEventHandler::ver(std::vector<std::string>& args)
{
return std::string("SUTAgentAndroid Version 1.13");
}
EventHandler*
CommandEventHandlerFactory::createEventHandler(PRFileDesc* socket)
{
return new CommandEventHandler(socket);
}

Просмотреть файл

@ -79,4 +79,11 @@ private:
std::string systime();
};
class CommandEventHandlerFactory: public EventHandlerFactory
{
public:
virtual EventHandler* createEventHandler(PRFileDesc* socket);
};
#endif

Просмотреть файл

@ -1,14 +0,0 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "CommandEventHandlerFactory.h"
#include "CommandEventHandler.h"
EventHandler*
CommandEventHandlerFactory::createEventHandler(PRFileDesc* socket)
{
return new CommandEventHandler(socket);
}

Просмотреть файл

@ -1,18 +0,0 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef negatus_command_event_handler_factory_h
#define negatus_command_event_handler_factory_h
#include "EventHandler.h"
#include "EventHandlerFactory.h"
class CommandEventHandlerFactory: public EventHandlerFactory
{
public:
virtual EventHandler* createEventHandler(PRFileDesc* socket);
};
#endif

Просмотреть файл

@ -31,4 +31,11 @@ private:
bool mClosed;
};
class EventHandlerFactory {
public:
virtual EventHandler* createEventHandler(PRFileDesc* socket) = 0;
};
#endif

Просмотреть файл

@ -1,6 +0,0 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "EventHandlerFactory.h"

Просмотреть файл

@ -1,16 +0,0 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef negatus_event_handler_factory_h
#define negatus_event_handler_factory_h
#include "EventHandler.h"
class EventHandlerFactory {
public:
virtual EventHandler* createEventHandler(PRFileDesc* socket) = 0;
};
#endif

Просмотреть файл

@ -5,7 +5,6 @@
#include "HeartbeatEventHandler.h"
#include "Shell.h"
#include "Strings.h"
#include "Subprocess.h"
#include <stdio.h>
#include <prinrval.h>
@ -86,8 +85,8 @@ HeartbeatEventHandler::handleEvent(PRPollDesc desc)
void
HeartbeatEventHandler::handleTimeout()
{
sendThump();
setTimeout(PR_SecondsToInterval(TIMEOUT));
sendThump();
}
std::string
@ -97,7 +96,7 @@ HeartbeatEventHandler::timestamp()
PRExplodedTime ts;
PR_ExplodeTime(now, PR_LocalTimeParameters, &ts);
char buffer[BUFSIZE];
char buffer[20];
sprintf(buffer, "%d%02d%02d-%02d:%02d:%02d", ts.tm_year, ts.tm_month,
ts.tm_mday, ts.tm_hour, ts.tm_min, ts.tm_sec);
@ -125,3 +124,10 @@ HeartbeatEventHandler::sendTraceOutput()
// devmgrSUT.py expects NULL terminated str
mBufSocket.write(output.c_str(), output.size() + 1);
}
EventHandler*
HeartbeatEventHandlerFactory::createEventHandler(PRFileDesc* socket)
{
return new HeartbeatEventHandler(socket);
}

Просмотреть файл

@ -34,4 +34,11 @@ private:
void sendTraceOutput();
};
class HeartbeatEventHandlerFactory: public EventHandlerFactory
{
public:
virtual EventHandler* createEventHandler(PRFileDesc* socket);
};
#endif

Просмотреть файл

@ -1,15 +0,0 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "EventHandler.h"
#include "HeartbeatEventHandlerFactory.h"
#include "HeartbeatEventHandler.h"
EventHandler*
HeartbeatEventHandlerFactory::createEventHandler(PRFileDesc* socket)
{
return new HeartbeatEventHandler(socket);
}

Просмотреть файл

@ -1,18 +0,0 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef negatus_heartbeat_event_handler_factory_h
#define negatus_heartbeat_event_handler_factory_h
#include "EventHandler.h"
#include "EventHandlerFactory.h"
class HeartbeatEventHandlerFactory: public EventHandlerFactory
{
public:
virtual EventHandler* createEventHandler(PRFileDesc* socket);
};
#endif

Просмотреть файл

@ -17,8 +17,8 @@
#include <prtime.h>
#include <prtypes.h>
#include "CommandEventHandlerFactory.h"
#include "HeartbeatEventHandlerFactory.h"
#include "CommandEventHandler.h"
#include "HeartbeatEventHandler.h"
#include "Logger.h"
#include "Strings.h"
#include "Reactor.h"
@ -90,7 +90,7 @@ dict get_reg_data()
}
void setUpAcceptor(EventHandlerFactory* fact, std::string kind, PRInt16 port,
bool setUpAcceptor(EventHandlerFactory* fact, std::string kind, PRInt16 port,
PRNetAddr& acceptorAddr)
{
SocketAcceptor* acceptor = new SocketAcceptor(fact);
@ -101,8 +101,9 @@ void setUpAcceptor(EventHandlerFactory* fact, std::string kind, PRInt16 port,
if (status == PR_FAILURE)
{
std::cerr << "Failure to open socket: " << PR_GetError() << std::endl;
exit(1);
return false;
}
return true;
}
@ -147,9 +148,12 @@ int main(int argc, char **argv)
return 1;
PRNetAddr cmdAddr, heartbeatAddr;
setUpAcceptor(new CommandEventHandlerFactory(), "Command", port, cmdAddr);
setUpAcceptor(new HeartbeatEventHandlerFactory(), "Heartbeat", 20700,
heartbeatAddr);
if(!setUpAcceptor(new CommandEventHandlerFactory(), "Command", port,
cmdAddr))
return 1;
if(!setUpAcceptor(new HeartbeatEventHandlerFactory(), "Heartbeat", 20700,
heartbeatAddr))
return 1;
dict reg_data = get_reg_data();
reg_data["IPADDR"] = addrStr(cmdAddr);

Просмотреть файл

@ -6,6 +6,7 @@
#include "Subprocess.h"
#include <stdio.h>
#include <string.h>
std::string
id()

Просмотреть файл

@ -9,7 +9,7 @@
SocketAcceptor::SocketAcceptor(EventHandlerFactory* handlerFactory)
: mSocket(NULL), handlerFactory(handlerFactory)
: mSocket(NULL), mHandlerFactory(handlerFactory)
{
}
@ -85,5 +85,5 @@ SocketAcceptor::handleEvent(PRPollDesc desc)
sockOpt.option = PR_SockOpt_Nonblocking;
sockOpt.value.non_blocking = PR_TRUE;
PR_SetSocketOption(newSocket, &sockOpt);
EventHandler* handler = handlerFactory->createEventHandler(newSocket);
EventHandler* handler = mHandlerFactory->createEventHandler(newSocket);
}

Просмотреть файл

@ -6,7 +6,6 @@
#define negatus_socket_acceptor_h
#include "EventHandler.h"
#include "EventHandlerFactory.h"
#include <prio.h>
@ -23,7 +22,7 @@ public:
private:
PRFileDesc* mSocket;
EventHandlerFactory* handlerFactory;
EventHandlerFactory* mHandlerFactory;
};
#endif