| 1 |
/*! |
| 2 |
\file |
| 3 |
\brief 通信の記録 |
| 4 |
|
| 5 |
\author Satofumi KAMIMURA |
| 6 |
|
| 7 |
$Id$ |
| 8 |
*/ |
| 9 |
|
| 10 |
#include "ConnectionLogger.h" |
| 11 |
#include "TcpipCtrl.h" |
| 12 |
#include <string> |
| 13 |
#include <fstream> |
| 14 |
|
| 15 |
using namespace beego; |
| 16 |
|
| 17 |
|
| 18 |
/*! |
| 19 |
\brief ConnectionLogger の内部クラス |
| 20 |
*/ |
| 21 |
struct ConnectionLogger::pImpl { |
| 22 |
ConnectionInterface* con; |
| 23 |
std::string base_name; |
| 24 |
std::fstream fd_recv; |
| 25 |
std::fstream fd_send; |
| 26 |
|
| 27 |
pImpl(ConnectionInterface* con_obj, bool open_logfile) |
| 28 |
: con(con_obj), base_name("connection") { |
| 29 |
if (con == NULL) { |
| 30 |
// !!! LogManager |
| 31 |
throw; |
| 32 |
} |
| 33 |
if (open_logfile) { |
| 34 |
open(); |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
~pImpl(void) { |
| 39 |
terminate(); |
| 40 |
delete con; |
| 41 |
} |
| 42 |
|
| 43 |
void open(void) { |
| 44 |
if (! fd_recv.is_open()) { |
| 45 |
std::string recv_file = base_name + "_recv.txt"; |
| 46 |
fd_recv.open(recv_file.c_str(), std::ios::out | std::ios::binary); |
| 47 |
|
| 48 |
std::string send_file = base_name + "_send.txt"; |
| 49 |
fd_send.open(send_file.c_str(), std::ios::out | std::ios::binary); |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
void terminate(void) { |
| 54 |
if (fd_recv.is_open()) { |
| 55 |
fd_recv.close(); |
| 56 |
fd_send.close(); |
| 57 |
} |
| 58 |
} |
| 59 |
}; |
| 60 |
|
| 61 |
|
| 62 |
ConnectionLogger::ConnectionLogger(ConnectionInterface* con, |
| 63 |
bool open_logfile) |
| 64 |
: pimpl(new pImpl(con, open_logfile)) { |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
ConnectionLogger::~ConnectionLogger(void) { |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
const char* ConnectionLogger::what(void) { |
| 73 |
return pimpl->con->what(); |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
bool ConnectionLogger::connect(const char* host, long port) { |
| 78 |
|
| 79 |
pimpl->open(); |
| 80 |
return pimpl->con->connect(host, port); |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
void ConnectionLogger::disconnect(void) { |
| 85 |
pimpl->con->disconnect(); |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
bool ConnectionLogger::isConnected(void) { |
| 90 |
return pimpl->con->isConnected(); |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
bool ConnectionLogger::changeBaudrate(long baudrate) { |
| 95 |
return pimpl->con->changeBaudrate(baudrate); |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
int ConnectionLogger::send(const char* data, int size) { |
| 100 |
|
| 101 |
if (size > 0) { |
| 102 |
// data の内容をファイルに書き出す |
| 103 |
pimpl->fd_send.write(data, size); |
| 104 |
pimpl->fd_send.flush(); |
| 105 |
} |
| 106 |
return pimpl->con->send(data, size); |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
int ConnectionLogger::recv(char* data, int size, int timeout) { |
| 111 |
|
| 112 |
int n = pimpl->con->recv(data, size, timeout); |
| 113 |
|
| 114 |
if (n > 0) { |
| 115 |
// data の内容をファイルに書き出す |
| 116 |
pimpl->fd_recv.write(data, n); |
| 117 |
pimpl->fd_recv.flush(); |
| 118 |
} |
| 119 |
return n; |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
int ConnectionLogger::size(int timeout) { |
| 124 |
return pimpl->con->size(timeout); |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
void ConnectionLogger::clear(void) { |
| 129 |
return pimpl->con->clear(); |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
void ConnectionLogger::setLogfileName(const char* logfileName) { |
| 134 |
pimpl->base_name = logfileName; |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
void ConnectionLogger::skip(int total_timeout) { |
| 139 |
return pimpl->con->skip(total_timeout); |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
void ConnectionLogger::flush(void) { |
| 144 |
pimpl->con->flush(); |
| 145 |
} |