-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPacket.cpp
More file actions
162 lines (146 loc) · 3.99 KB
/
Packet.cpp
File metadata and controls
162 lines (146 loc) · 3.99 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include "Packet.h"
Packet::Packet()
{
MakeNull();
BuildErrorPacket("Error_Zero_arg_constructor");
}
Packet::Packet(char msg[]):otherUsed(0)
{
MakeNull();
strCpy(Message,msg);
int length= strLen(msg);
//length -1 because of null at the end
if(length<100){
if(Message[0]=='{' && Message[length-1]=='}'){
Read(msg);
}
else{
BuildErrorPacket("Error_No_braces");
}
} else {
BuildErrorPacket("Error_Message_too_long");
}
}
void Packet::MakeNull(){
Message[0] = '\0';
Command[0] = '\0';
otherUsed = 0;
Sender[0] = '\0';
Recipient[0] = '\0';
}
void Packet::MakeMessage(char sender[], char recip[], const char comm[]/*, char other[5][], int o_used*/){
// takes one string taking all the variable
// ignoring other momentarily, might need it later...
strCpy(Sender, sender);
strCpy(Recipient, recip);
strCpy(Command, comm);
// otherUsed = o_used;
Message[0]='{';
Message[1]='\0';
append(Message," ");
append(Message, Sender);
append(Message," ");
append(Message, Recipient);
append(Message," ");
append(Message, Command);
append(Message," ");
for(int i=0; i<otherUsed; i++){
// Other[i][0] = other[i][0];
append(Message,Other[i]);
append(Message," ");
}
append(Message, "}"); //spaces before and after brace
}
//Packet::Packet(char sender[], char recipient[], char command[],
// String other):otherUsed(0)
//{
// int other_len= other.length()+1;
// if(other_len>100){
// BuildErrorPacket("Error_other_too_long");
// return;
// }
// char coord[101];
// other.toCharArray(coord, other_len); //takes received and turns it into a char array message
// char copy[101];
// StringStream coor_str(coord);
// int count;
// for(int i = 0; !coor_str.endOfStr(); i++){
// if(i>=5){
// BuildErrorPacket("Error_Too_many_others");
// return;
// }
// coor_str.getNext(copy);
// if(strLen(copy)>20){
// BuildErrorPacket("Error_Copy_too_long");
// return;
// }
// strCpy(Other[i],copy);
// count=i;
// }
// otherUsed=count;
// strCpy(Sender,sender);
// strCpy(Recipient,recipient);
// strCpy(Command,command);
// //creating a char message array
//// append(Message, "{ ");
// Message[0]='{';
// Message[1]='\0';
// append(Message," ");
// append(Message, Sender);
// append(Message," ");
// append(Message, Recipient);
// append(Message," ");
// append(Message, Command);
// append(Message," ");
// for(int i=0; i<otherUsed; i++){
// append(Message,Other[i]);
// append(Message," ");
// }
// append(Message, "}"); //spaces before and after brace
// Serial.println(Message);
//// Write(Message);
//}
Packet::Packet(char sender[],char recipient[], char command[], double other)
{
MakeNull();
BuildErrorPacket("Error_Joe_build_this");
}
void Packet::Read(char msg[])
{
//msg can't be more than 100. already checked
StringStream stream(msg);
char next[50]="?";
// for(int i=0;!strCmp(next,"}");i++){
for(int i = 0; !stream.endOfStr(); i++){
stream.getNext(next);
// Serial.print("next: ");
// Serial.println(next);
switch(i){
case 0: //brace so do nothing
break;
case 1:strCpy(Sender,next);
break;
case 2:strCpy(Recipient,next);
break;
case 3:strCpy(Command,next);
break;
default:
if(strCmp(next,"}"))
return;
strCpy(Other[i-4],next); //this make sense,(i guess)!!!!!!!!!!!!!
otherUsed++;
// why is this i-4....?
break;
}
}
}
void Packet::CamCoords(char Sendme[])
{
strCpy(Sendme, "{ Cam Con Location ");
}
void Packet::BuildErrorPacket(char msg[])
{
strCpy(Command,msg);
strCpy(Recipient,"Con");
strCpy(Sender,"?");
}