- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 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
public static final int TYPE_A_OK = 0;
public static final int TYPE_R_OPEN = 1;
public static final int TYPE_R_STOPSID = 2;
public static final int TYPE_A_STOPSID = 3;
//....
public static final int TYPE_R_ALARM = 26;
HashMap<String, Integer> typesMap = new HashMap<String, Integer>();
//в конструкторе
public Data() {
typesMap.put("A_OK", TYPE_A_OK);
typesMap.put("R_OPEN", TYPE_R_OPEN);
typesMap.put("R_STOPSID", TYPE_R_STOPSID);
//...
typesMap.put("R_ALARM", TYPE_R_ALARM);
//...
}
//в одном из методов
public boolean processPacket(Packet pack) {
//...
StringTokenizer strt = new StringTokenizer(body, "\n");
try {
id = strt.nextToken();
sign = strt.nextToken();
type = typesMap.get(strt.nextToken());
try {
commBody = strt.nextToken();
} catch (Exception e) {
// System.out.println("ERR: " + id + "; " + sign + "; " + type
// + "; ");
}
} catch (Exception e) {
System.err.println(sdf.format(Calendar.getInstance().getTime()) +"packet parsing error");
outBody += "A_ERR\n" + e.getMessage();
}
switch (type) {
case TYPE_A_OK:
dontsend = true;
break;
case TYPE_R_OPEN:
outBody = processROpen(comm);
break;
case TYPE_R_CLOSE:
outBody = processRClose(comm);
break;
//...
case TYPE_R_ALARM:
outBody = processRAlarm(comm);
break;
default:
outBody += "A_ERR";
break;
}
//...
if(debug)
System.err.println(outBody);
//...
return true;
}
ява она такая. Вот так. Вынужденно-китайский код. Не умеет свич со строками работать... и не хочется с хэшем заморачиваться. А скоро типов будет больше...