2 * Copyright (C) 2013 - David Goulet <dgoulet@efficios.com>
4 * This library is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU Lesser General Public License, version 2.1 only,
6 * as published by the Free Software Foundation.
8 * This library is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this library; if not, write to the Free Software Foundation,
15 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 package org
.lttng
.ust
.jul
;
20 import java
.util
.concurrent
.Semaphore
;
21 import java
.nio
.ByteBuffer
;
22 import java
.nio
.ByteOrder
;
23 import java
.lang
.Integer
;
24 import java
.io
.IOException
;
25 import java
.io
.BufferedOutputStream
;
26 import java
.io
.ByteArrayOutputStream
;
27 import java
.io
.DataOutputStream
;
28 import java
.io
.DataInputStream
;
30 import java
.lang
.management
.ManagementFactory
;
31 import java
.util
.ArrayList
;
32 import java
.util
.HashMap
;
33 import java
.util
.List
;
34 import java
.util
.Timer
;
35 import java
.util
.TimerTask
;
36 import java
.util
.logging
.Logger
;
38 class USTRegisterMsg
{
39 public static int pid
;
42 public class LTTngTCPSessiondClient
{
43 /* Command header from the session deamon. */
44 private LTTngSessiondCmd2_4
.sessiond_hdr headerCmd
=
45 new LTTngSessiondCmd2_4
.sessiond_hdr();
47 private final String sessiondHost
;
48 private final int sessiondPort
;
49 private Socket sessiondSock
;
50 private boolean quit
= false;
52 private DataInputStream inFromSessiond
;
53 private DataOutputStream outToSessiond
;
55 private LTTngLogHandler handler
;
57 private Semaphore registerSem
;
59 private Timer eventTimer
;
60 private List
<LTTngEvent
> enabledEventList
= new ArrayList
<LTTngEvent
>();
62 * Map of Logger objects that have been enabled. They are indexed by name.
64 private HashMap
<String
, Logger
> enabledLoggers
= new HashMap
<String
, Logger
>();
65 /* Timer delay at each 5 seconds. */
66 private final static long timerDelay
= 5 * 1000;
67 private static boolean timerInitialized
;
69 public LTTngTCPSessiondClient(String host
, int port
, Semaphore sem
) {
70 this.sessiondHost
= host
;
71 this.sessiondPort
= port
;
72 this.registerSem
= sem
;
73 this.eventTimer
= new Timer();
74 this.timerInitialized
= false;
77 private void setupEventTimer() {
78 if (this.timerInitialized
) {
82 this.eventTimer
.scheduleAtFixedRate(new TimerTask() {
86 * We have to make a copy here since it is possible that the
87 * enabled event list is changed during an iteration on it.
89 List
<LTTngEvent
> tmpList
= new ArrayList
<LTTngEvent
>(enabledEventList
);
91 LTTngSessiondCmd2_4
.sessiond_enable_handler enableCmd
= new
92 LTTngSessiondCmd2_4
.sessiond_enable_handler();
93 for (LTTngEvent event
: tmpList
) {
98 * Check if this Logger name has been enabled already. Note
99 * that in the case of "*", it's never added in that hash
100 * table thus the enable command does a lookup for each
101 * logger name in that hash table for the * case in order
102 * to make sure we don't enable twice the same logger
103 * because JUL apparently accepts that the *same*
104 * LogHandler can be added twice on a Logger object...
107 logger
= enabledLoggers
.get(event
.name
);
108 if (logger
!= null) {
113 * Set to one means that the enable all event has been seen
114 * thus event from that point on must use loglevel for all
115 * events. Else the object has its own loglevel.
117 if (handler
.logLevelUseAll
== 1) {
118 event
.logLevel
.level
= handler
.logLevelAll
;
119 event
.logLevel
.type
= handler
.logLevelTypeAll
;
123 * The all event is a special case since we have to iterate
124 * over every Logger to see which one was not enabled.
126 if (event
.name
.equals("*")) {
127 enableCmd
.name
= event
.name
;
128 enableCmd
.lttngLogLevel
= event
.logLevel
.level
;
129 enableCmd
.lttngLogLevelType
= event
.logLevel
.type
;
131 * The return value is irrelevant since the * event is
132 * always kept in the list.
134 enableCmd
.execute(handler
, enabledLoggers
);
138 ret
= enableCmd
.enableLogger(handler
, event
, enabledLoggers
);
140 /* Enabled so remove the event from the list. */
141 enabledEventList
.remove(event
);
145 }, this.timerDelay
, this.timerDelay
);
147 this.timerInitialized
= true;
150 public void init(LTTngLogHandler handler
) throws InterruptedException
{
151 this.handler
= handler
;
161 * Connect to the session daemon before anything else.
166 * Register to the session daemon as the Java component of the
169 registerToSessiond();
170 this.registerSem
.release();
175 * Block on socket receive and wait for command from the
176 * session daemon. This will return if and only if there is a
177 * fatal error or the socket closes.
180 } catch (UnknownHostException uhe
) {
181 this.registerSem
.release();
182 System
.out
.println(uhe
);
183 } catch (IOException ioe
) {
184 this.registerSem
.release();
186 } catch (Exception e
) {
187 this.registerSem
.release();
193 public void destroy() {
195 this.eventTimer
.cancel();
198 if (this.sessiondSock
!= null) {
199 this.sessiondSock
.close();
201 } catch (Exception e
) {
207 * Receive header data from the session daemon using the LTTng command
208 * static buffer of the right size.
210 private void recvHeader() throws Exception
{
212 byte data
[] = new byte[this.headerCmd
.SIZE
];
214 read_len
= this.inFromSessiond
.read(data
, 0, data
.length
);
215 if (read_len
!= data
.length
) {
216 throw new IOException();
218 this.headerCmd
.populate(data
);
222 * Receive payload from the session daemon. This MUST be done after a
223 * recvHeader() so the header value of a command are known.
225 * The caller SHOULD use isPayload() before which returns true if a payload
226 * is expected after the header.
228 private byte[] recvPayload() throws Exception
{
229 byte payload
[] = new byte[(int) this.headerCmd
.data_size
];
231 /* Failsafe check so we don't waste our time reading 0 bytes. */
232 if (payload
.length
== 0) {
236 this.inFromSessiond
.read(payload
, 0, payload
.length
);
241 * Handle session command from the session daemon.
243 private void handleSessiondCmd() throws Exception
{
248 /* Get header from session daemon. */
251 if (headerCmd
.data_size
> 0) {
252 data
= recvPayload();
255 switch (headerCmd
.cmd
) {
258 LTTngSessiondCmd2_4
.sessiond_list_logger listLoggerCmd
=
259 new LTTngSessiondCmd2_4
.sessiond_list_logger();
260 listLoggerCmd
.execute(this.handler
);
261 data
= listLoggerCmd
.getBytes();
267 LTTngSessiondCmd2_4
.sessiond_enable_handler enableCmd
=
268 new LTTngSessiondCmd2_4
.sessiond_enable_handler();
270 enableCmd
.code
= LTTngSessiondCmd2_4
.lttng_jul_ret_code
.CODE_INVALID_CMD
;
273 enableCmd
.populate(data
);
274 event
= enableCmd
.execute(this.handler
, this.enabledLoggers
);
277 * Add the event to the list so it can be enabled if
278 * the logger appears at some point in time.
280 if (enabledEventList
.contains(event
) == false) {
281 enabledEventList
.add(event
);
284 data
= enableCmd
.getBytes();
289 LTTngSessiondCmd2_4
.sessiond_disable_handler disableCmd
=
290 new LTTngSessiondCmd2_4
.sessiond_disable_handler();
292 disableCmd
.code
= LTTngSessiondCmd2_4
.lttng_jul_ret_code
.CODE_INVALID_CMD
;
295 disableCmd
.populate(data
);
296 disableCmd
.execute(this.handler
);
297 data
= disableCmd
.getBytes();
303 ByteBuffer buf
= ByteBuffer
.wrap(data
);
304 buf
.order(ByteOrder
.BIG_ENDIAN
);
305 LTTngSessiondCmd2_4
.lttng_jul_ret_code code
=
306 LTTngSessiondCmd2_4
.lttng_jul_ret_code
.CODE_INVALID_CMD
;
307 buf
.putInt(code
.getCode());
312 /* Send payload to session daemon. */
313 this.outToSessiond
.write(data
, 0, data
.length
);
314 this.outToSessiond
.flush();
318 private void connectToSessiond() throws Exception
{
319 this.sessiondSock
= new Socket(this.sessiondHost
, this.sessiondPort
);
320 this.inFromSessiond
= new DataInputStream(
321 sessiondSock
.getInputStream());
322 this.outToSessiond
= new DataOutputStream(
323 sessiondSock
.getOutputStream());
326 private void registerToSessiond() throws Exception
{
327 byte data
[] = new byte[4];
328 ByteBuffer buf
= ByteBuffer
.wrap(data
);
329 String pid
= ManagementFactory
.getRuntimeMXBean().getName().split("@")[0];
331 buf
.putInt(Integer
.parseInt(pid
));
332 this.outToSessiond
.write(data
, 0, data
.length
);
333 this.outToSessiond
.flush();