Add Registration functionality and tidy up
[WeStealzYourDataz.git] / src / uk / ac / ntu / n0521366 / wsyd / libs / logging / PacketHandler.java
1 /*
2  * The MIT License
3  *
4  * Copyright 2015 Eddie Berrisford-Lynch <dev@fun2be.me>.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 package uk.ac.ntu.n0521366.wsyd.libs.logging;
25
26 import java.io.IOException;
27 import java.util.logging.LogRecord;
28 import java.util.logging.Handler;
29 import java.util.logging.ErrorManager;
30 import java.net.InetSocketAddress;
31 import java.net.DatagramPacket;
32 import java.net.DatagramSocket;
33 import uk.ac.ntu.n0521366.wsyd.libs.message.MessageLogRecord;
34 import uk.ac.ntu.n0521366.wsyd.libs.net.NetworkMessage;
35
36 /**
37  * Publish log records over UDP encapsulated in a NetworkMessage.
38  *
39  * Sends the records to a remote NetworkServerUDP service.
40  *
41  * @see java.util.logging.Handler
42  * @see uk.ac.ntu.n0521366.wsyd.libs.net.NetworkMessage
43  * @author Eddie Berrisford-Lynch <dev@fun2be.me>
44  */
45 public class PacketHandler extends Handler {
46
47     /**
48      * Where to send the log record
49      */
50     private final InetSocketAddress _socketAddress;
51
52     @Override
53     public void close() {
54     }
55
56     @Override
57     public void flush() {
58     }
59
60     /**
61      * Send the record to the log service.
62      * 
63      * Wraps the record in a Message, and the Message in a NetworkMessage
64      * 
65      * @param record 
66      */
67     @Override
68     public void publish(LogRecord record) {
69         if (record == null) {
70             return;
71         }
72         if (!this.isLoggable(record)) {
73             return;
74         }
75         MessageLogRecord m = new MessageLogRecord(record);
76         NetworkMessage nm = NetworkMessage.createNetworkMessage("Log", "ServerLog", m);
77         try {
78             byte[] data = NetworkMessage.serialize(nm);
79             DatagramPacket packetMessage = new DatagramPacket(data, data.length);
80
81             // set remote log server host address and port
82             packetMessage.setSocketAddress(this._socketAddress);
83
84             try (
85                 DatagramSocket datagramSocket = new DatagramSocket(_socketAddress.getPort(), _socketAddress.getAddress())
86             )
87             {
88                 datagramSocket.send(packetMessage);
89             } catch (IOException e ) {
90                 reportError("Unable to create UDP socket", e, ErrorManager.WRITE_FAILURE);
91             }
92         } catch (IOException e) {
93             reportError("Unable to serialize NetworkMessage", e, ErrorManager.WRITE_FAILURE);
94         }
95     }
96
97     /**
98      * Create a handler that sends log records to a UDP log service.
99      * 
100      * @param logService address of the target log service
101      */
102     public PacketHandler(InetSocketAddress logService) {
103         this._socketAddress = logService;
104     }
105 }