-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNode.cpp
More file actions
140 lines (127 loc) · 2.29 KB
/
Copy pathNode.cpp
File metadata and controls
140 lines (127 loc) · 2.29 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
/*
* Author: Sebastian Wolf
* Created: August 2018
*/
#include "Node.h"
Node::Node(int ID)
{
this->ID = new byte[2]();
this->ID[0] = (byte)ID;
this->ID[1] = (byte)(ID >> 8);
}
Node::Node(byte *ID)
{
this->ID = ID;
}
Node::~Node()
{
delete ID;
for (int i = 0; i < listNodes->size(); i++)
{
Node *node = listNodes->get(i);
if (node != NULL)
{
delete node;
}
}
if (listNodes != NULL)
{
delete listNodes;
}
for (int i = 0; i < listAttribute->size(); i++)
{
Attribute *attr = listAttribute->get(i);
if (attr != NULL)
{
delete attr;
}
}
if (listAttribute != NULL)
{
delete listAttribute;
}
}
void Node::addNode(Node *node)
{
listNodes->add(node);
}
void Node::addAttribute(Attribute *attribute)
{
listAttribute->add(attribute);
}
byte *Node::getID()
{
return ID;
}
int Node::getIDAsInt()
{
return ((ID[0] & 0xFF) | (ID[1] & 0xFF) << 8);
}
LinkedList<Node *> *Node::getNodes()
{
return listNodes;
}
/*
* Bitte nicht benutzen, auschließlich für ACK beim Registrieren gedacht
* Stattdessen bitte getNodes nutzen !
*/
Node *Node::getNodeByID(int ID)
{
for (int i = 0; i < listNodes->size(); i++)
{
if (listNodes->get(i)->getIDAsInt() == ID)
{
return listNodes->get(i);
}
}
return NULL;
}
LinkedList<Attribute *> *Node::getAttribute()
{
return listAttribute;
}
/*
* Bitte nicht benutzen, auschließlich für ACK beim Registrieren gedacht
* Stattdessen bitte getAttribute nutzen !
*/
Attribute *Node::getAttributeByID(int ID)
{
for (int i = 0; i < listAttribute->size(); i++)
{
if (listAttribute->get(i)->getIDAsInt() == ID)
{
return listAttribute->get(i);
}
}
return NULL;
}
byte *Node::get(int *length)
{
int pos = 0;
byte *buffer = new byte[1024];
memcpy(buffer, PACKET_LENGTH, 4);
pos += 4;
memcpy(buffer + pos, ID, 2);
pos += 2;
for (int i = 0; i < listAttribute->size(); i++)
{
Attribute *attr = listAttribute->get(i);
byte *result = attr->get();
memcpy(buffer + pos, result, attr->getSize());
pos += attr->getSize();
delete result;
}
for (int i = 0; i < listNodes->size(); i++)
{
Node *node = listNodes->get(i);
int length1 = 0;
byte *result = node->get(&length1);
memcpy(buffer + pos, result, length1);
pos += length1;
delete result;
}
memcpy(buffer + pos, END, 4);
pos += 4;
*length = pos;
return buffer;
}