This repository was archived by the owner on Mar 6, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 387
/
Copy pathIOMessage.java
executable file
·173 lines (146 loc) · 3.75 KB
/
IOMessage.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
* socket.io-java-client IOMessage.java
*
* Copyright (c) 2012, Enno Boland
* socket.io-java-client is a implementation of the socket.io protocol in Java.
*
* See LICENSE file for more information
*/
package io.socket;
/**
* The Class IOMessage.
*/
class IOMessage {
/** Message type disconnect */
public static final int TYPE_DISCONNECT = 0;
/** Message type connect */
public static final int TYPE_CONNECT = 1;
/** Message type heartbeat */
public static final int TYPE_HEARTBEAT = 2;
/** Message type message */
public static final int TYPE_MESSAGE = 3;
/** Message type JSON message */
public static final int TYPE_JSON_MESSAGE = 4;
/** Message type event */
public static final int TYPE_EVENT = 5;
/** Message type acknowledge */
public static final int TYPE_ACK = 6;
/** Message type error */
public static final int TYPE_ERROR = 7;
/** Message type noop */
public static final int TYPE_NOOP = 8;
/** Index of the type field in a message */
public static final int FIELD_TYPE = 0;
/** Index of the id field in a message */
public static final int FIELD_ID = 1;
/** Index of the end point field in a message */
public static final int FIELD_ENDPOINT = 2;
/** Index of the data field in a message */
public static final int FIELD_DATA = 3;
/** Number of fields in a message. */
public static final int NUM_FIELDS = 4;
/** The field values */
private final String[] fields = new String[NUM_FIELDS];
/** Type */
private int type;
/**
* Instantiates a new IOMessage by given data.
*
* @param type
* the type
* @param id
* the id
* @param namespace
* the namespace
* @param data
* the data
*/
public IOMessage(int type, String id, String namespace, String data) {
this.type = type;
this.fields[FIELD_ID] = id;
this.fields[FIELD_TYPE] = "" + type;
this.fields[FIELD_ENDPOINT] = namespace;
this.fields[FIELD_DATA] = data;
}
/**
* Instantiates a new IOMessage by given data.
*
* @param type
* the type
* @param namespace
* the name space
* @param data
* the data
*/
public IOMessage(int type, String namespace, String data) {
this(type, null, namespace, data);
}
/**
* Instantiates a new IOMessage from a String representation. If the String
* is not well formated, the result is undefined.
*
* @param message
* the message
*/
public IOMessage(String message) {
String[] fields = message.split(":", NUM_FIELDS);
for (int i = 0; i < fields.length; i++) {
this.fields[i] = fields[i];
if(i == FIELD_TYPE)
this.type = Integer.parseInt(fields[i]);
}
}
/**
* Generates a String representation of this object.
*/
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
for(int i = 0; i < fields.length; i++) {
builder.append(':');
if (fields[i] != null)
builder.append(fields[i]);
}
return builder.substring(1);
}
/**
* Returns the type of this IOMessage.
*
* @return the type
*/
public int getType() {
return type;
}
/**
* Returns the id of this IOMessage.
*
* @return the id
*/
public String getId() {
return fields[FIELD_ID];
}
/**
* Sets the id of this IOMessage
*
* @param id
*/
public void setId(String id) {
fields[FIELD_ID] = id;
}
/**
* Returns the endpoint of this IOMessage.
*
* @return the endpoint
*/
public String getEndpoint() {
return fields[FIELD_ENDPOINT];
}
/**
* Returns the data of this IOMessage.
*
* @return the data
*/
public String getData() {
return fields[FIELD_DATA];
}
}