lttng: Move plugins to the Trace Compass namespace
[deliverable/tracecompass.git] / org.eclipse.tracecompass.lttng2.control.core / src / org / eclipse / linuxtools / internal / lttng2 / control / core / relayd / impl / LttngRelaydConnector_2_4.java
1 /**********************************************************************
2 * Copyright (c) 2014 Ericsson
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Contributors:
10 * Matthew Khouzam - Initial implementation
11 * Marc-Andre Laperle - Create session and split getNextIndex from getNextPacket
12 **********************************************************************/
13
14 package org.eclipse.linuxtools.internal.lttng2.control.core.relayd.impl;
15
16 import java.io.DataInputStream;
17 import java.io.DataOutputStream;
18 import java.io.IOException;
19 import java.util.List;
20
21 import org.eclipse.jdt.annotation.NonNull;
22 import org.eclipse.linuxtools.internal.lttng2.control.core.relayd.ILttngRelaydConnector;
23 import org.eclipse.linuxtools.internal.lttng2.control.core.relayd.lttngviewerCommands.AttachSessionRequest;
24 import org.eclipse.linuxtools.internal.lttng2.control.core.relayd.lttngviewerCommands.AttachSessionResponse;
25 import org.eclipse.linuxtools.internal.lttng2.control.core.relayd.lttngviewerCommands.Command;
26 import org.eclipse.linuxtools.internal.lttng2.control.core.relayd.lttngviewerCommands.ConnectResponse;
27 import org.eclipse.linuxtools.internal.lttng2.control.core.relayd.lttngviewerCommands.CreateSessionResponse;
28 import org.eclipse.linuxtools.internal.lttng2.control.core.relayd.lttngviewerCommands.GetMetadata;
29 import org.eclipse.linuxtools.internal.lttng2.control.core.relayd.lttngviewerCommands.GetNextIndex;
30 import org.eclipse.linuxtools.internal.lttng2.control.core.relayd.lttngviewerCommands.GetPacket;
31 import org.eclipse.linuxtools.internal.lttng2.control.core.relayd.lttngviewerCommands.IndexResponse;
32 import org.eclipse.linuxtools.internal.lttng2.control.core.relayd.lttngviewerCommands.ListSessionsResponse;
33 import org.eclipse.linuxtools.internal.lttng2.control.core.relayd.lttngviewerCommands.MetadataPacketResponse;
34 import org.eclipse.linuxtools.internal.lttng2.control.core.relayd.lttngviewerCommands.NewStreamsResponse;
35 import org.eclipse.linuxtools.internal.lttng2.control.core.relayd.lttngviewerCommands.NextIndexReturnCode;
36 import org.eclipse.linuxtools.internal.lttng2.control.core.relayd.lttngviewerCommands.SeekCommand;
37 import org.eclipse.linuxtools.internal.lttng2.control.core.relayd.lttngviewerCommands.SessionResponse;
38 import org.eclipse.linuxtools.internal.lttng2.control.core.relayd.lttngviewerCommands.StreamResponse;
39 import org.eclipse.linuxtools.internal.lttng2.control.core.relayd.lttngviewerCommands.TracePacketResponse;
40 import org.eclipse.linuxtools.internal.lttng2.control.core.relayd.lttngviewerCommands.ViewerCommand;
41
42 /**
43 * Lttng 2.4 implementation
44 *
45 * @author Matthew Khouzam
46 */
47 public class LttngRelaydConnector_2_4 implements ILttngRelaydConnector {
48
49 private final @NonNull DataInputStream fInNet;
50 private final @NonNull DataOutputStream fOutNet;
51
52 /**
53 * Constructor needs two network streams
54 *
55 * @param inNet
56 * network incoming data
57 * @param outNet
58 * network outgoing data
59 */
60 public LttngRelaydConnector_2_4(@NonNull DataInputStream inNet, @NonNull DataOutputStream outNet) {
61 fInNet = inNet;
62 fOutNet = outNet;
63 }
64
65 // ------------------------------------------------------------------------
66 // AutoCloseable
67 // ------------------------------------------------------------------------
68
69 @Override
70 public void close() throws IOException {
71 fInNet.close();
72 fOutNet.close();
73 }
74
75 // ------------------------------------------------------------------------
76 // ILttngRelaydConnector
77 // ------------------------------------------------------------------------
78
79 @Override
80 public List<SessionResponse> getSessions() throws IOException {
81 ViewerCommand listSessionsCmd = new ViewerCommand(Command.VIEWER_LIST_SESSIONS, 0,0);
82
83 fOutNet.write(listSessionsCmd.serialize());
84 fOutNet.flush();
85
86 return new ListSessionsResponse(fInNet).getSessionsList();
87 }
88
89 @Override
90 public CreateSessionResponse createSession() throws IOException {
91 ViewerCommand listSessionsCmd = new ViewerCommand(Command.VIEWER_CREATE_SESSION, 0, 0);
92 fOutNet.write(listSessionsCmd.serialize());
93 fOutNet.flush();
94
95 return new CreateSessionResponse(fInNet);
96 }
97
98 @Override
99 public AttachSessionResponse attachToSession(SessionResponse lttngViewerSession) throws IOException {
100 ViewerCommand listSessionsCmd = new ViewerCommand(Command.VIEWER_ATTACH_SESSION, 0,0);
101 fOutNet.write(listSessionsCmd.serialize());
102 /*
103 * only flush if you read after
104 */
105
106 AttachSessionRequest attachRequest = new AttachSessionRequest(lttngViewerSession.getId(), SeekCommand.VIEWER_SEEK_BEGINNING);
107 fOutNet.write(attachRequest.serialize());
108 fOutNet.flush();
109
110 return new AttachSessionResponse(fInNet);
111 }
112
113 @Override
114 public String getMetadata(AttachSessionResponse attachedSession) throws IOException {
115
116 for (StreamResponse stream : attachedSession.getStreamList()) {
117 if (stream.getMetadataFlag() == 1) {
118 issueCommand(Command.VIEWER_GET_METADATA);
119
120 GetMetadata metadataRequest = new GetMetadata(stream.getId());
121 fOutNet.write(metadataRequest.serialize());
122 fOutNet.flush();
123
124 MetadataPacketResponse metaDataPacket = new MetadataPacketResponse(fInNet);
125 return new String(metaDataPacket.getData());
126 }
127 }
128
129 return null;
130 }
131
132 @Override
133 public TracePacketResponse getPacketFromStream(IndexResponse index, long id) throws IOException {
134
135 issueCommand(Command.VIEWER_GET_PACKET);
136
137 GetPacket packetRequest = new GetPacket(id, index.getOffset(), (int) (index.getPacketSize() / 8));
138 fOutNet.write(packetRequest.serialize());
139 fOutNet.flush();
140
141 return new TracePacketResponse(fInNet);
142 }
143
144 @Override
145 public TracePacketResponse getNextPacket(StreamResponse stream) throws IOException {
146 IndexResponse indexReply = getNextIndex(stream);
147
148 TracePacketResponse packet = null;
149 if (indexReply.getStatus() == NextIndexReturnCode.VIEWER_INDEX_OK) {
150 packet = getPacketFromStream(indexReply, stream.getId());
151 }
152 return packet;
153 }
154
155 @Override
156 public IndexResponse getNextIndex(StreamResponse stream) throws IOException {
157 issueCommand(Command.VIEWER_GET_NEXT_INDEX);
158
159 GetNextIndex indexRequest = new GetNextIndex(stream.getId());
160 fOutNet.write(indexRequest.serialize());
161 fOutNet.flush();
162
163 return new IndexResponse(fInNet);
164 }
165
166 @Override
167 public List<StreamResponse> getNewStreams() throws IOException {
168
169 Command viewerGetNewStreams = Command.VIEWER_GET_NEW_STREAMS;
170
171 issueCommand(viewerGetNewStreams);
172
173 return new NewStreamsResponse(fInNet).getStreamList();
174 }
175
176 private void issueCommand(Command command) throws IOException {
177 ViewerCommand connectCommand = new ViewerCommand(command, ConnectResponse.SIZE, 0);
178 fOutNet.write(connectCommand.serialize());
179 fOutNet.flush();
180 }
181 }
This page took 0.035668 seconds and 5 git commands to generate.