Coverage Summary for Class: WriterMessageRecorder (co.rsk.net.eth)
Class |
Class, %
|
Method, %
|
Line, %
|
WriterMessageRecorder |
0%
(0/1)
|
0%
(0/3)
|
0%
(0/24)
|
1 /*
2 * This file is part of RskJ
3 * Copyright (C) 2017 RSK Labs Ltd.
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 package co.rsk.net.eth;
20
21 import co.rsk.net.NodeID;
22 import org.ethereum.net.message.Message;
23 import org.ethereum.util.ByteUtil;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 import java.io.BufferedWriter;
28 import java.io.IOException;
29 import java.text.SimpleDateFormat;
30 import java.util.Date;
31
32 /**
33 * Created by ajlopez on 26/04/2017.
34 */
35 public class WriterMessageRecorder implements MessageRecorder {
36 private static final Logger logger = LoggerFactory.getLogger("messagerecorder");
37 private BufferedWriter writer;
38 private MessageFilter filter;
39 private SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
40
41 public WriterMessageRecorder(BufferedWriter writer, MessageFilter filter) {
42 this.writer = writer;
43 this.filter = filter;
44 }
45
46 @Override
47 public synchronized void recordMessage(NodeID sender, Message message) {
48 if (this.filter != null && !this.filter.acceptMessage(message)) {
49 return;
50 }
51
52 try {
53 writer.write(formatter.format(new Date()));
54
55 writer.write(",");
56 writer.write(String.valueOf(message.getCode()));
57 writer.write(",");
58 writer.write(String.valueOf(message.getCommand()));
59 writer.write(",");
60
61 if (message instanceof RskMessage) {
62 writer.write(String.valueOf(((RskMessage) message).getMessage().getMessageType()));
63 }
64
65 writer.write(",");
66
67 writer.write(ByteUtil.toHexString(message.getEncoded()));
68
69 writer.write(",");
70
71 if (sender != null) {
72 writer.write(ByteUtil.toHexString(sender.getID()));
73 }
74
75 writer.newLine();
76 writer.flush();
77 }
78 catch (IOException ex) {
79 logger.error("Exception recording message: ", ex);
80 }
81 }
82 }