Commit | Line | Data |
---|---|---|
8e15b929 MK |
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 and API | |
11 | * Marc-Andre Laperle - Initial implementation and API | |
12 | **********************************************************************/ | |
13 | ||
9bc60be7 | 14 | package org.eclipse.tracecompass.internal.lttng2.control.core.relayd.lttngviewerCommands; |
8e15b929 MK |
15 | |
16 | import java.io.DataInputStream; | |
17 | import java.io.IOException; | |
18 | import java.nio.ByteBuffer; | |
19 | import java.nio.ByteOrder; | |
20 | ||
21 | /** | |
22 | * CONNECT payload. | |
23 | * | |
24 | * @author Matthew Khouzam | |
8e15b929 MK |
25 | */ |
26 | public class ConnectResponse implements IRelayResponse, IRelayCommand { | |
27 | ||
28 | /** | |
29 | * Response or command size | |
8adfbb73 MAL |
30 | * |
31 | * fViewerSessionId + fMajor + fMinor + fType | |
8e15b929 MK |
32 | */ |
33 | public static final int SIZE = (Long.SIZE + Integer.SIZE + Integer.SIZE + Integer.SIZE) / 8; | |
34 | /** session id, counts from 1 and increments by session */ | |
35 | private final long fViewerSessionId; | |
36 | /** | |
37 | * Major version, hint, it's at least 2 | |
38 | */ | |
39 | private final int fMajor; | |
40 | /** | |
41 | * Minor version, hint, it's at least 4 | |
42 | */ | |
43 | private final int fMinor; | |
44 | /** | |
45 | * type of connect to {@link ConnectionType} | |
46 | */ | |
47 | private final ConnectionType fType; | |
48 | ||
49 | /** | |
50 | * Connection response reply constructor | |
51 | * | |
52 | * @param inStream | |
53 | * the data input stream | |
54 | * @throws IOException | |
55 | * a network error | |
56 | */ | |
57 | public ConnectResponse(DataInputStream inStream) throws IOException { | |
58 | byte data[] = new byte[SIZE]; | |
59 | inStream.readFully(data); | |
60 | ByteBuffer bb = ByteBuffer.wrap(data); | |
61 | bb.order(ByteOrder.BIG_ENDIAN); | |
62 | fViewerSessionId = bb.getLong(); | |
63 | fMajor = bb.getInt(); | |
64 | fMinor = bb.getInt(); | |
fe25111b MAL |
65 | bb.getInt(); // Should not be used, see http://bugs.lttng.org/issues/728 |
66 | fType = ConnectionType.VIEWER_CLIENT_COMMAND; | |
8e15b929 MK |
67 | } |
68 | ||
69 | /** | |
70 | * Constructor for command | |
71 | * | |
72 | * @param sessionID | |
73 | * session id | |
74 | * @param major | |
75 | * the major version | |
76 | * @param minor | |
77 | * the minor version | |
78 | * @param connection | |
79 | * the connection type, typically VIEWER_CLIENT_COMMAND | |
80 | */ | |
81 | public ConnectResponse(long sessionID, int major, int minor, ConnectionType connection) { | |
82 | fViewerSessionId = sessionID; | |
83 | fMajor = major; | |
84 | fMinor = minor; | |
85 | fType = connection; | |
86 | } | |
87 | ||
88 | /** | |
89 | * get the major version | |
90 | * | |
91 | * @return the major version | |
92 | */ | |
93 | public int getMajor() { | |
94 | return fMajor; | |
95 | } | |
96 | ||
97 | /** | |
98 | * get the minor version | |
99 | * | |
100 | * @return the minor version | |
101 | */ | |
102 | public int getMinor() { | |
103 | return fMinor; | |
104 | } | |
105 | ||
106 | @Override | |
107 | public byte[] serialize() { | |
108 | byte data[] = new byte[SIZE]; | |
109 | ByteBuffer bb = ByteBuffer.wrap(data); | |
110 | bb.order(ByteOrder.BIG_ENDIAN); | |
111 | bb.putLong(fViewerSessionId); | |
112 | bb.putInt(fMajor); | |
113 | bb.putInt(fMinor); | |
114 | bb.putInt(fType.getCommand()); | |
115 | return data; | |
116 | } | |
117 | ||
118 | } |