-
Notifications
You must be signed in to change notification settings - Fork 0
/
packet.cpp
64 lines (60 loc) · 1.14 KB
/
packet.cpp
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
#include <iostream>
#include <string>
#include <string.h>
using namespace std;
#include "Packet.h"
void Packet :: build(string srcNodeId,string key,int hopCount,/*message_type type,*/string message){
this->srcNodeId=srcNodeId;
this->key=key;
this->hopCount=hopCount;
// this->type=type;
this->message=message;
}
int Packet :: deserialise(char * s){
/*converts s[] to packet*/
int len=strlen(s);
int i=0,count=0;
for(i = 0; i < len; ++i){
if (s[i]==';')
{
count++;
continue;
}
if (count==0)
{
this->srcNodeId+=s[i];
}else if (count==1)
{
this->key+=s[i];
}else if (count==2)
{
this->hopCount+=s[i];
}else if(count==3){
//this->type+=s[i];
}else if (count==4)
{
this->message+=s[i];
}
}
if (count==5)
{
return 1; //success
}
return 0;
}
char* Packet :: serialise(){
/* convert packet to character stream*/
string s="";
s += this->srcNodeId+";";
s+=this->key+";";
s+=this->hopCount+";";
// s+=this->type+";";
s+=this->message+";";
int i=0;
int length = s.length();
char* temp=(char*)malloc(sizeof(char)*(length+1));
for(i = 0; i < length; ++i){
temp[i]=s[i];
}temp[i]='\0';
return temp;
}