1 | package com.bowman.cardserv;
|
---|
2 |
|
---|
3 | import com.bowman.cardserv.cws.ServiceMapping;
|
---|
4 | import com.bowman.cardserv.crypto.DESUtil;
|
---|
5 |
|
---|
6 | import java.util.*;
|
---|
7 | import java.io.*;
|
---|
8 |
|
---|
9 | /**
|
---|
10 | * Created by IntelliJ IDEA.
|
---|
11 | * User: bowman
|
---|
12 | * Date: Jan 6, 2010
|
---|
13 | * Time: 7:33:15 AM
|
---|
14 | */
|
---|
15 | public class CspNetMessage implements Serializable {
|
---|
16 |
|
---|
17 | public static final int TYPE_NEW = 0, TYPE_ECMREQ = 1, TYPE_DCWRPL = 2;
|
---|
18 | public static final int TYPE_FULLSTATE = 3, TYPE_STATEACK = 4;
|
---|
19 | public static final int TYPE_INCRSTATE = 5;
|
---|
20 |
|
---|
21 | public static final int STATE_DELETE = 0xFF, STATE_SIDS = 1, STATE_PROVIDERS = 2, STATE_CUSTOM = 3, STATE_EXTRA = 4;
|
---|
22 |
|
---|
23 | private int type, seqNr, originId;
|
---|
24 |
|
---|
25 | private List statusUpdates;
|
---|
26 | private Map statusLookup;
|
---|
27 | private CamdNetMessage camdMessage;
|
---|
28 |
|
---|
29 | public static void parseStatusChange(DataInputStream is, CspNetMessage msg) throws IOException {
|
---|
30 | msg.originId = is.readInt(); // 4 bytes, id of remote proxy
|
---|
31 | int count = is.readUnsignedByte(); // 1 byte, number of status updates in this message
|
---|
32 |
|
---|
33 | StatusChange sc;
|
---|
34 | msg.statusUpdates = new ArrayList(count); msg.statusLookup = new HashMap();
|
---|
35 |
|
---|
36 | int operation, stateType, itemCount;
|
---|
37 | int onid, caid; List updates; ProfileKey key;
|
---|
38 | for(int i = 0; i < count; i++) {
|
---|
39 | operation = is.readUnsignedByte(); // 1 byte, 0 for remove/cannot decode, 1 for add/can decode
|
---|
40 | stateType = is.readUnsignedByte(); // 1 byte, type of state update
|
---|
41 | onid = is.readUnsignedShort(); // 2 bytes, dvb original network id
|
---|
42 | caid = is.readUnsignedShort(); // 2 bytes, ca-id
|
---|
43 | key = new ProfileKey(onid, caid);
|
---|
44 |
|
---|
45 | updates = (List)msg.statusLookup.get(key);
|
---|
46 | if(updates == null) {
|
---|
47 | updates = new ArrayList();
|
---|
48 | msg.statusLookup.put(key, updates);
|
---|
49 | }
|
---|
50 | sc = new StatusChange(key, stateType, operation);
|
---|
51 | itemCount = is.readUnsignedShort(); // 2 bytes, number of items affected (0 = this is just a notification for the onid, no known state)
|
---|
52 |
|
---|
53 | if(itemCount > 0) {
|
---|
54 | switch(stateType) {
|
---|
55 | case STATE_PROVIDERS:
|
---|
56 | Integer[] items = new Integer[itemCount];
|
---|
57 | for(int n = 0; n < itemCount; n++) items[n] = new Integer(is.readInt());
|
---|
58 | sc.setUpdatedItems(items);
|
---|
59 | break;
|
---|
60 | case STATE_SIDS:
|
---|
61 | case STATE_EXTRA:
|
---|
62 | Integer[] shortItems = new Integer[itemCount];
|
---|
63 | for(int n = 0; n < itemCount; n++) shortItems[n] = new Integer(is.readUnsignedShort());
|
---|
64 | sc.setUpdatedItems(shortItems);
|
---|
65 | break;
|
---|
66 | case STATE_CUSTOM:
|
---|
67 | Long[] longItems = new Long[itemCount];
|
---|
68 | for(int n = 0; n < itemCount; n++) longItems[n] = new Long(is.readLong());
|
---|
69 | sc.setUpdatedItems(longItems);
|
---|
70 | break;
|
---|
71 | default:
|
---|
72 | throw new IOException("unknown state type:" + stateType);
|
---|
73 | }
|
---|
74 | }
|
---|
75 |
|
---|
76 | msg.statusUpdates.add(sc);
|
---|
77 | updates.add(sc);
|
---|
78 | }
|
---|
79 | }
|
---|
80 |
|
---|
81 | public static int statusHashCode(List updates) {
|
---|
82 | String s = updates.toString();
|
---|
83 | // System.out.println(s.hashCode() + " -> " + s);
|
---|
84 | return updates.toString().hashCode();
|
---|
85 | }
|
---|
86 |
|
---|
87 | public static List buildProfileUpdate(CaProfile profile, boolean sendExtra) {
|
---|
88 |
|
---|
89 | if(profile.getNetworkId() <= 0 || profile.getCaId() <= 0) return null;
|
---|
90 |
|
---|
91 | // todo CaProfile.MULTIPLE
|
---|
92 |
|
---|
93 | List updates = new ArrayList();
|
---|
94 | ProfileKey key = new ProfileKey(profile);
|
---|
95 |
|
---|
96 | // split mappings into sid and cid to avoid sending custom if not present for profile
|
---|
97 | ServiceMapping[] sm = profile.getServices(true);
|
---|
98 | Integer[] sids = new Integer[sm.length];
|
---|
99 | Long[] customs = new Long[sm.length];
|
---|
100 | boolean hasCustom = false;
|
---|
101 | for(int i = 0; i < sm.length; i++) {
|
---|
102 | sids[i] = new Integer(sm[i].serviceId);
|
---|
103 | customs[i] = new Long(sm[i].getCustomData());
|
---|
104 | if(sm[i].getCustomId() != 0 || sm[i].getProviderIdent() != ServiceMapping.NO_PROVIDER) hasCustom = true;
|
---|
105 | }
|
---|
106 | StatusChange sc;
|
---|
107 | for(int i = 1; i < 5; i++) {
|
---|
108 | sc = new StatusChange(key, i, 1);
|
---|
109 | switch(i) {
|
---|
110 | case STATE_SIDS:
|
---|
111 | sc.setUpdatedItems(sids);
|
---|
112 | break;
|
---|
113 | case STATE_PROVIDERS:
|
---|
114 | sc.setUpdatedItems(profile.getProviderIdents());
|
---|
115 | break;
|
---|
116 | case STATE_CUSTOM:
|
---|
117 | if(hasCustom) sc.setUpdatedItems(customs);
|
---|
118 | break;
|
---|
119 | case STATE_EXTRA: // extra profile meta-data
|
---|
120 | if(sendExtra) {
|
---|
121 | ProxyConfig config = ProxyConfig.getInstance();
|
---|
122 | int mcww = (int)config.getConnManager().getMaxCwWait(profile);
|
---|
123 | int mcaw = (int)config.getCacheHandler().getMaxCacheWait(mcww);
|
---|
124 | sc.setUpdatedItems(new Integer[] {new Integer(mcww), new Integer(mcaw)});
|
---|
125 | }
|
---|
126 | break;
|
---|
127 | }
|
---|
128 | updates.add(sc);
|
---|
129 | }
|
---|
130 |
|
---|
131 | // special case for sids already known not to decode
|
---|
132 | hasCustom = false;
|
---|
133 | sm = profile.getServices(false);
|
---|
134 | sids = new Integer[sm.length]; customs = new Long[sm.length];
|
---|
135 | for(int i = 0; i < sm.length; i++) {
|
---|
136 | sids[i] = new Integer(sm[i].serviceId);
|
---|
137 | customs[i] = new Long(sm[i].getCustomData());
|
---|
138 | if(sm[i].getCustomId() != 0 || sm[i].getProviderIdent() != ServiceMapping.NO_PROVIDER) hasCustom = true;
|
---|
139 | }
|
---|
140 | sc = new StatusChange(key, STATE_SIDS, 0);
|
---|
141 | sc.setUpdatedItems(sids);
|
---|
142 | updates.add(sc);
|
---|
143 | if(hasCustom) {
|
---|
144 | sc = new StatusChange(key, STATE_CUSTOM, 0);
|
---|
145 | sc.setUpdatedItems(customs);
|
---|
146 | updates.add(sc);
|
---|
147 | }
|
---|
148 |
|
---|
149 | return updates;
|
---|
150 | }
|
---|
151 |
|
---|
152 | public static List getStatusItems(int type, boolean available, List updates) {
|
---|
153 | if(updates == null) return null;
|
---|
154 | StatusChange sc;
|
---|
155 | for(Iterator iter = updates.iterator(); iter.hasNext(); ) {
|
---|
156 | sc = (StatusChange)iter.next();
|
---|
157 | if(sc.type == type && sc.available == available) return sc.list;
|
---|
158 | }
|
---|
159 | return Collections.EMPTY_LIST;
|
---|
160 | }
|
---|
161 |
|
---|
162 | public static boolean isDeletion(List updates) {
|
---|
163 | if(updates == null || updates.isEmpty()) return false;
|
---|
164 | StatusChange sc = (StatusChange)updates.get(0);
|
---|
165 | return sc.type == STATE_DELETE;
|
---|
166 | }
|
---|
167 |
|
---|
168 | public CspNetMessage(int type) {
|
---|
169 | this.type = type;
|
---|
170 | }
|
---|
171 |
|
---|
172 | public CspNetMessage(int type, int seqNr) {
|
---|
173 | this(type);
|
---|
174 | this.seqNr = seqNr;
|
---|
175 | }
|
---|
176 |
|
---|
177 | public int getType() {
|
---|
178 | return type;
|
---|
179 | }
|
---|
180 |
|
---|
181 | public int getSeqNr() {
|
---|
182 | return seqNr;
|
---|
183 | }
|
---|
184 |
|
---|
185 | public void setSeqNr(int seqNr) {
|
---|
186 | this.seqNr = seqNr;
|
---|
187 | }
|
---|
188 |
|
---|
189 | public CamdNetMessage getCamdMessage() {
|
---|
190 | return camdMessage;
|
---|
191 | }
|
---|
192 |
|
---|
193 | public void setCamdMessage(CamdNetMessage camdMessage) {
|
---|
194 | this.camdMessage = camdMessage;
|
---|
195 | }
|
---|
196 |
|
---|
197 | public int getOriginId() {
|
---|
198 | return originId;
|
---|
199 | }
|
---|
200 |
|
---|
201 | public void setOriginId(int originId) {
|
---|
202 | this.originId = originId;
|
---|
203 | }
|
---|
204 |
|
---|
205 | public boolean isKeepAlive() {
|
---|
206 | return type == TYPE_INCRSTATE && isEmpty();
|
---|
207 | }
|
---|
208 |
|
---|
209 | public boolean isEmpty() {
|
---|
210 | return statusUpdates == null || statusUpdates.isEmpty();
|
---|
211 | }
|
---|
212 |
|
---|
213 | public int getUpdateCount() {
|
---|
214 | if(statusUpdates == null) return 0;
|
---|
215 | else return statusUpdates.size();
|
---|
216 | }
|
---|
217 |
|
---|
218 | public List getStatusUpdates() {
|
---|
219 | return statusUpdates;
|
---|
220 | }
|
---|
221 |
|
---|
222 | public List getStatusUpdatesForKey(ProfileKey key) {
|
---|
223 | return (List)statusLookup.get(key);
|
---|
224 | }
|
---|
225 |
|
---|
226 | public Set getProfileKeys() {
|
---|
227 | return statusLookup.keySet();
|
---|
228 | }
|
---|
229 |
|
---|
230 | public void addStatusUpdate(StatusChange sc) {
|
---|
231 | if(statusUpdates == null) statusUpdates = new ArrayList();
|
---|
232 | statusUpdates.add(sc);
|
---|
233 | }
|
---|
234 |
|
---|
235 | public void addSidUpdate(ProfileKey key, Integer sid, boolean available) {
|
---|
236 | StatusChange sc = new StatusChange(key, STATE_SIDS, available?1:0);
|
---|
237 | sc.setUpdatedItems(new Integer[] {sid});
|
---|
238 | addStatusUpdate(sc);
|
---|
239 | }
|
---|
240 |
|
---|
241 | public void addDeleteUpdate(ProfileKey key) {
|
---|
242 | StatusChange sc = new StatusChange(key, STATE_DELETE, 0);
|
---|
243 | addStatusUpdate(sc);
|
---|
244 | }
|
---|
245 |
|
---|
246 | public void addStatusUpdates(ProfileKey key, List updates) {
|
---|
247 | if(statusUpdates == null) statusUpdates = new ArrayList();
|
---|
248 | if(statusLookup == null) statusLookup = new HashMap();
|
---|
249 | statusUpdates.addAll(updates);
|
---|
250 | statusLookup.put(key, updates);
|
---|
251 | }
|
---|
252 |
|
---|
253 | public void setStatusUpdates(List statusUpdates) {
|
---|
254 | this.statusUpdates = statusUpdates;
|
---|
255 | }
|
---|
256 |
|
---|
257 | public static class StatusChange implements Serializable {
|
---|
258 |
|
---|
259 | ProfileKey key;
|
---|
260 | int type;
|
---|
261 | boolean available; // add or remove
|
---|
262 | // private Set set = new TreeSet();
|
---|
263 | private List list = new ArrayList();
|
---|
264 |
|
---|
265 | public StatusChange(ProfileKey key, int type, int available) {
|
---|
266 | this.key = key;
|
---|
267 | this.type = type;
|
---|
268 | this.available = (available == 1);
|
---|
269 | }
|
---|
270 |
|
---|
271 | public void setUpdatedItems(Integer[] items) {
|
---|
272 | list.addAll(Arrays.asList(items));
|
---|
273 | }
|
---|
274 |
|
---|
275 | public void setUpdatedItems(Long[] items) {
|
---|
276 | list.addAll(Arrays.asList(items));
|
---|
277 | }
|
---|
278 |
|
---|
279 | public Integer[] getUpdatedItemsInt() {
|
---|
280 | return (Integer[])list.toArray(new Integer[list.size()]);
|
---|
281 | }
|
---|
282 |
|
---|
283 | public Long[] getUpdatedItemsLong() {
|
---|
284 | return (Long[])list.toArray(new Long[list.size()]);
|
---|
285 | }
|
---|
286 |
|
---|
287 | public String toString() {
|
---|
288 | if(list.isEmpty()) return "";
|
---|
289 | else return type + " " + list;
|
---|
290 | }
|
---|
291 |
|
---|
292 | }
|
---|
293 |
|
---|
294 | public static class ProfileKey implements Serializable {
|
---|
295 | public int onid, caid;
|
---|
296 |
|
---|
297 | public ProfileKey(String s) {
|
---|
298 | String[] pair = s.split("-");
|
---|
299 | onid = Integer.parseInt(pair[0], 16);
|
---|
300 | caid = Integer.parseInt(pair[1], 16);
|
---|
301 | }
|
---|
302 |
|
---|
303 | public ProfileKey(CaProfile profile) {
|
---|
304 | this.onid = profile.getNetworkId();
|
---|
305 | this.caid = profile.getCaId();
|
---|
306 | }
|
---|
307 |
|
---|
308 | public ProfileKey(int onid, int caid) {
|
---|
309 | this.onid = onid;
|
---|
310 | this.caid = caid;
|
---|
311 | }
|
---|
312 |
|
---|
313 | public boolean equals(Object o) {
|
---|
314 | if(this == o) return true;
|
---|
315 | if(o == null || getClass() != o.getClass()) return false;
|
---|
316 | ProfileKey that = (ProfileKey)o;
|
---|
317 | if(caid != that.caid) return false;
|
---|
318 | if(onid != that.onid) return false;
|
---|
319 | return true;
|
---|
320 | }
|
---|
321 |
|
---|
322 | public int hashCode() {
|
---|
323 | int result = onid;
|
---|
324 | result = 31 * result + caid;
|
---|
325 | return result;
|
---|
326 | }
|
---|
327 |
|
---|
328 | public String toString() {
|
---|
329 | return DESUtil.intToHexString(onid, 4) + "-" + DESUtil.intToHexString(caid, 4);
|
---|
330 | }
|
---|
331 | }
|
---|
332 | }
|
---|