1 | function matperfClient_StartTrial(length, windows, reqband, UdpTcp, mode, time,address,reqport) |
---|
2 | |
---|
3 | % initialize the variables |
---|
4 | retrying = 0; |
---|
5 | |
---|
6 | % server address received when a udp package was received from the server |
---|
7 | address = strcat(num2str(address(1)),{'.'},num2str(address(2)),{'.'},num2str(address(3)),{'.'},num2str(address(4))); |
---|
8 | |
---|
9 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
10 | % when this function is called, it prepares a packet to send to the server |
---|
11 | % that includes Udp/Tcp length, bandwidth, time interval, port #, and |
---|
12 | % window size. |
---|
13 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
14 | |
---|
15 | if UdpTcp == 1, |
---|
16 | reqpack = [1 UdpTcp length reqband time reqport]; |
---|
17 | elseif UdpTcp ==2, |
---|
18 | reqpack = [1 UdpTcp windows time reqport]; |
---|
19 | end |
---|
20 | |
---|
21 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
22 | % below commands set the time the matlab will wait for iperf in order to |
---|
23 | % check if iperf has completed successfully or not. time set here is the |
---|
24 | % time the trial takes with 2 more seconds since there are chances |
---|
25 | % that iperf can run over the set time. |
---|
26 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
27 | |
---|
28 | if mode == 3, |
---|
29 | time = 2 * time + 2; |
---|
30 | else |
---|
31 | time = time + 2; |
---|
32 | end |
---|
33 | |
---|
34 | udp = pnet('udpsocket',3333); |
---|
35 | |
---|
36 | while 1, |
---|
37 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
38 | % in this while loop, it sends the prepared packet with 1 in front so that |
---|
39 | % it tells server that client wants server to awknowledge a trial |
---|
40 | % request. |
---|
41 | % Then it waits for the server to send 1, which tells a server is ready |
---|
42 | % for a trial run. When the client or server does not somehow recognize the |
---|
43 | % signal, then the client sends a packet again. |
---|
44 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
45 | |
---|
46 | pnet(udp,'write', reqpack); |
---|
47 | pnet(udp,'writepacket',char(address),3333); % sends a packet carrying signal 1 |
---|
48 | |
---|
49 | pnet(udp,'setreadtimeout',3); % wait to receive a response for signal 1 |
---|
50 | len = pnet(udp,'readpacket'); |
---|
51 | |
---|
52 | if len > 0, |
---|
53 | data = pnet(udp,'read',1000,'double'); |
---|
54 | if data == 1, |
---|
55 | disp('server approved trial request'); |
---|
56 | else |
---|
57 | disp('fix your code StartTrial line 46'); |
---|
58 | continue |
---|
59 | end |
---|
60 | else |
---|
61 | disp('sending trial request reapproval...'); |
---|
62 | pnet('closeall'); |
---|
63 | udp = pnet('udpsocket',3333); |
---|
64 | continue |
---|
65 | end |
---|
66 | |
---|
67 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
68 | % when the server approves the trial, the below commands begin to |
---|
69 | % create a command that calls iperf with known data. |
---|
70 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
71 | if data == 1, |
---|
72 | systemcall = 'iperf -c'; |
---|
73 | if UdpTcp == 1, |
---|
74 | systemcall = strcat(systemcall,{' '}, address, {' -u'},{' -b '},num2str(reqband),{' -l '},num2str(length),{' -t '},num2str(time), {' -p '},num2str(reqport)); |
---|
75 | elseif UdpTcp == 2, |
---|
76 | systemcall = strcat(systemcall,{' '}, address, {' -w '},num2str(windows),{' -t '},num2str(time),{' -p '},num2str(reqport)); |
---|
77 | end |
---|
78 | if mode == 2, |
---|
79 | systemcall = strcat(systemcall, {' -d'}); |
---|
80 | elseif mode == 3, |
---|
81 | systemcall = strcat(systemcall, {' -r'}); |
---|
82 | end |
---|
83 | systemcall = strcat(systemcall,' -f B -f b > tempo_result.txt &'); |
---|
84 | end |
---|
85 | |
---|
86 | %%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
87 | % calls iperf |
---|
88 | system(char(systemcall)); |
---|
89 | %%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
90 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
91 | % use python language to check the output |
---|
92 | % weird.txt is a python script that checks if iperf is still running or |
---|
93 | % not. It is very simple. It loads the file jobs_l and check line by |
---|
94 | % line to see if the word iperf exists or not. Because ps -a checks all |
---|
95 | % programs that are in process, if iperf is terminated, it won't be on |
---|
96 | % the list. And python scripts will output 0. If not, it will output 1. |
---|
97 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
98 | |
---|
99 | |
---|
100 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
101 | % it is also the way to check if iperf client is not acting properly. |
---|
102 | % if it takes more than a few seconds after a given amount of time, |
---|
103 | % matlab will consider the client is not acting properly. |
---|
104 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
105 | |
---|
106 | % delay |
---|
107 | pnet(udp,'setreadtimeout',time); % wait until the iperf finishes its job |
---|
108 | pnet(udp,'readpacket'); |
---|
109 | |
---|
110 | system('ps -a >jobs_l'); % dump ps -a result to jobs_l |
---|
111 | system('python weird'); % run the python scripts to check if iperf is still running. |
---|
112 | jobs_l = load('check_jobs'); % load the result to mamtlab |
---|
113 | |
---|
114 | |
---|
115 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
116 | % matlab checks whether iperf is running after a given certain time + |
---|
117 | % alpha. Sometimes in wireless, even we set the time to 10 as default, |
---|
118 | % iperf runs over 11 or 12. This program checks if iperf is completed |
---|
119 | % or not. And if not, it checks again if iperf is running after 2sec |
---|
120 | % and if it is it kills the iperf and restarts the trial. |
---|
121 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
122 | if jobs_l == 1, |
---|
123 | disp('first job wrong'); |
---|
124 | % delay |
---|
125 | pnet(udp,'setreadtimeout',3); |
---|
126 | pnet(udp,'readpacket'); |
---|
127 | |
---|
128 | system('ps -a >jobs_l'); |
---|
129 | system('python weird'); |
---|
130 | jobs_ll = load('check_jobs'); |
---|
131 | else |
---|
132 | jobs_ll = 0; |
---|
133 | end |
---|
134 | |
---|
135 | if jobs_ll == 1, |
---|
136 | disp('2nd jobs wrong'); |
---|
137 | %delay |
---|
138 | pnet(udp,'setreadtimeout',3); |
---|
139 | pnet(udp,'readpacket'); |
---|
140 | |
---|
141 | system('ps -a >jobs_l'); |
---|
142 | system('python weird'); |
---|
143 | jobs_lll = load('check_jobs'); |
---|
144 | else |
---|
145 | jobs_lll = 0; |
---|
146 | end |
---|
147 | |
---|
148 | if jobs_lll ==1. |
---|
149 | system('killall iperf'); |
---|
150 | disp('3rd jobs wrong'); |
---|
151 | continue; |
---|
152 | end |
---|
153 | |
---|
154 | clear_set; % remove all data recorded files that are unnecessary |
---|
155 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
156 | % this while loop checks the signal received from the server whether |
---|
157 | % the trial has succeeded or failed. If the server does not respond |
---|
158 | % consecutively for 5 times when client asks, client will consider it |
---|
159 | % as server got stuck and restart from the beginning. When it sends 3, |
---|
160 | % it will restart again. when it sends 2, then it will proceed to next |
---|
161 | % while loop. |
---|
162 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
163 | while 1, |
---|
164 | udp = pnet('udpsocket',3333); |
---|
165 | pnet(udp,'write',5); |
---|
166 | pnet(udp,'setwritetimeout',.5); %give a delay so that server can be ready when sending a packet |
---|
167 | pnet(udp,'writepacket',char(address),3333); % sends a signal 5 to see if the server succeed its performance. |
---|
168 | |
---|
169 | % now check if server sends 2 or 3 |
---|
170 | pnet(udp,'setreadtimeout',3); |
---|
171 | len1 = pnet(udp,'readpacket'); |
---|
172 | if len1 > 0, |
---|
173 | data = pnet(udp,'read',1000,'double'); |
---|
174 | checking = data; |
---|
175 | break; |
---|
176 | else |
---|
177 | disp('retrying to send 5'); |
---|
178 | retrying = retrying + 1; |
---|
179 | if retrying == 5, |
---|
180 | break; |
---|
181 | end |
---|
182 | continue; |
---|
183 | end |
---|
184 | |
---|
185 | end |
---|
186 | if retrying == 5, |
---|
187 | retrying = 0; |
---|
188 | system('killall iperf'); |
---|
189 | pnet('closeall'); |
---|
190 | udp = pnet('udpsocket',3333); |
---|
191 | continue; |
---|
192 | end |
---|
193 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
194 | % this if statement asks server to restart the iperf server and returns |
---|
195 | % to the first while loop in order to restart the process. |
---|
196 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
197 | if checking == 3, |
---|
198 | disp('trial failed..restarting the trial'); |
---|
199 | pnet(udp,'write', reqpack); |
---|
200 | pnet(udp,'writepacket',char(address),3333); |
---|
201 | system('killall iperf'); |
---|
202 | pnet('closeall'); |
---|
203 | udp = pnet('udpsocket',3333); |
---|
204 | continue; |
---|
205 | end |
---|
206 | |
---|
207 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
208 | % this while loop sends signal 6 to the server. 6 means the client is |
---|
209 | % approving server to parse the output into a specific vector, pick up |
---|
210 | % the numbers it needs and structure them as a result struct. |
---|
211 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
212 | while 1, |
---|
213 | pnet(udp,'write',6); |
---|
214 | pnet(udp,'writepacket',char(address),3333); |
---|
215 | |
---|
216 | pnet(udp,'setreadtimeout', 1); |
---|
217 | len2 = pnet(udp,'readpacket'); |
---|
218 | if len2 > 0, |
---|
219 | data = pnet(udp,'read',1000,'double'); |
---|
220 | if data == 1, |
---|
221 | break; |
---|
222 | end |
---|
223 | else |
---|
224 | continue; |
---|
225 | end |
---|
226 | |
---|
227 | end |
---|
228 | break; |
---|
229 | end |
---|
230 | pnet('closeall'); |
---|
231 | return |
---|