-
Notifications
You must be signed in to change notification settings - Fork 1
/
DataMessage.java
91 lines (88 loc) · 2.39 KB
/
DataMessage.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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
*
* @author asus
*/
public class DataMessage {
//<nxthop> <fromnbr> DATA <srcnode> <dstnode> <string>
public int nSourceId;
public int nNextHopId;
public int nFromId;
public int nDestId;
public String strMessage;
public DataMessage(int x_nSrc,int x_nNxtHopId,int x_nFrom,int x_nDestId,String x_strMessage)
{
nSourceId = x_nSrc;
nNextHopId = x_nNxtHopId;
nFromId = x_nFrom;
nDestId = x_nDestId;
strMessage = x_strMessage;
}
public DataMessage(String x_StrMessage)
{
FillFromString(x_StrMessage);
}
public String getMessage()
{
String msg="";
msg += "<" + nNextHopId + ">";
msg += "<" + nFromId + ">";
msg += " DATA ";
msg += "<" + nSourceId + ">";
msg += "<" + nDestId + ">";
msg += "<" + strMessage + ">";
return msg;
}
public void FillFromString(String x_strDataMessage)
{
int index_start_text = x_strDataMessage.lastIndexOf("<");
int index_finish_text = x_strDataMessage.lastIndexOf(">");
strMessage = x_strDataMessage.substring(index_start_text + 1, index_finish_text);
Pattern p = Pattern.compile("(\\d+)");
Matcher m = p.matcher(x_strDataMessage);
boolean bFormatOK = true;
if(m.find())
{
nNextHopId = Integer.parseInt(m.group());
}
else
{
bFormatOK = false;
}
if(m.find())
{
nFromId = Integer.parseInt(m.group());
}
else
{
bFormatOK = false;
}
if(m.find())
{
nSourceId = Integer.parseInt(m.group());
}
else
{
bFormatOK = false;
}
if(m.find())
{
nDestId = Integer.parseInt(m.group());
}
else
{
bFormatOK = false;
}
if(!bFormatOK)
{
System.out.println("Invalid DataMessage Format...");
System.exit(-1);
}
}
}