1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package net.sf.dexterim.oscar.io;
20
21 import java.util.HashMap;
22 import java.util.Map;
23
24 import net.sf.dexterim.oscar.entity.Flap;
25 import net.sf.dexterim.oscar.entity.Snac;
26 import net.sf.dexterim.oscar.entity.Word;
27 import net.sf.dexterim.oscar.server.GenericSnacResponse;
28 import net.sf.dexterim.oscar.server.Response;
29
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32
33 /***
34 * @author christoph
35 */
36 public class SnacParser implements MessageParser {
37
38 private Map delegates;
39
40 private Log log;
41
42 public SnacParser() {
43 log = LogFactory.getLog(getClass());
44
45 delegates = new HashMap();
46 register(0x0001, new GenericServiceControlsParser());
47 }
48
49 public final void register(int family, SnacParserDelegate delegate) {
50 delegates.put(new Integer(family), delegate);
51 }
52
53
54
55
56
57
58 public Response parse(Flap flap) throws ParserException {
59 byte data[] = flap.getData();
60
61 byte familyArray[] = new byte[2];
62 familyArray[0] = data[0];
63 familyArray[1] = data[1];
64
65 int family = new Word(familyArray).getValue();
66
67 byte typeArray[] = new byte[2];
68 typeArray[0] = data[2];
69 typeArray[1] = data[3];
70
71 int type = new Word(typeArray).getValue();
72
73 byte flagsArray[] = new byte[2];
74 flagsArray[0] = data[4];
75 flagsArray[1] = data[5];
76 int flags = new Word(flagsArray).getValue();
77
78 byte requestArray[] = new byte[4];
79 requestArray[0] = data[6];
80 requestArray[1] = data[7];
81 requestArray[2] = data[8];
82 requestArray[3] = data[9];
83 int requestId = new Word(requestArray).getValue();
84
85 byte snacData[] = new byte[data.length - 10];
86 System.arraycopy(data, 10, snacData, 0, snacData.length);
87
88 Snac snac = new Snac(family, type, flags);
89 snac.setRequest(requestId);
90 snac.setData(snacData);
91
92 SnacParserDelegate delegate = (SnacParserDelegate)delegates
93 .get(new Integer(snac.getFamily()));
94
95 if (delegate == null) {
96 if (log.isDebugEnabled()) {
97 log.debug("No Parser for Family " + snac.getFamily() + " available");
98 }
99
100 return new GenericSnacResponse(snac);
101 }
102 else {
103 return delegate.parse(snac);
104 }
105 }
106 }