-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain_sample.cpp
151 lines (124 loc) · 2.94 KB
/
main_sample.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
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
/*
* MsgSched.h
*
* Created on: Aug 1, 2013
* Author: Ashish Choudhari
* Real-Time Systems
* ECE Department
* Southern Illinois University, Carbondale, IL
*/
#include "Tcp_Client.h"
#include "MsgSched.h"
#define ECN_TRANSPORT 10
MS_Agent *ms_Agent = MS_Agent::Get_Instance();
tcp_client client;
bool threadbreak = false;
bool PT_Done = false;
int peerID=8;
int TPT=20000, PT=0, PA=0;
// set DeltaMin=DeltaMax if you want non varying Delta
int DeltaMin=10, DeltaMax=10;
int Curr_Delta = DeltaMin;
int parseValue(char buff[], int hashPos)
{
int value[4]={0,0,0,0};
sscanf(buff, "*%d#%d#%d#%d#", &value[0], &value[1], &value[2], &value[3]);
return value[hashPos];
}
void receiverFunc()
{
char indata[100];
int it=0;
int i=0;
string ch;
// flush buffers
for(int k=0;k<100;k++)
indata[k]=' ';
while(1)
{
ch = client.receive(1);
if(ch[0]=='*')
{
while(1)
{
if(ch[0] == '#') i++;
indata[it] = ch[0];
if(i==4) break;
it++;
ch[0] = ' ';
ch = client.receive(1);
}
cout<<"\n"<<indata<<"\n";
PA = parseValue(indata,2);
ms_Agent->Event_Msg_Ack_Received(peerID, parseValue(indata,1), parseValue(indata,3), PT);
}
// flush buffers
for(int k=0;k<100;k++)
indata[k]=' ';
i=0;
it=0;
ch[0]=' ';
if(threadbreak)
{
client.closeSocket();
break;
}
if(PA == TPT)
{
cout << "\n\nAll Power Transfered\n\n";
PT_Done = true;
break;
}
}
}
int main()
{
MS_Tick_Type PhaseTime=20000, Obs_RTT=400;
char data[100];
int msgID=1;
int MyKmax=20;
//connect to host
client.conn( "localhost" , 4242 );
ms_Agent->Set_My_Kmax_And_Phase_Time(MyKmax, PhaseTime);
// set DeltaMin=DeltaMax if you want non varying Delta
ms_Agent->Set_DeltaMin_DeltaMax(DeltaMin, DeltaMax);
ms_Agent->Add_Peer(peerID, Obs_RTT, TPT);
boost::thread *receiverThread;
receiverThread = new boost::thread(boost::ref(receiverFunc));
while(1)
{
if(ms_Agent->Invariant_Check(peerID))
{
if(PT < TPT)
{
#ifdef ALLOW_DELTA_VARY
Curr_Delta = ms_Agent->Get_Curr_Delta(peerID);
#endif
cout << "\n**********"<<Curr_Delta<<"\n";
sprintf(data, "*%d#%d#%d#%d#", peerID, msgID, Curr_Delta, ECN_TRANSPORT);
client.send_data(data);
ms_Agent->Event_Msg_Sent(peerID, msgID);
msgID++;
PT += Curr_Delta;
}
if(ms_Agent->Get_Current_Tick() >= PhaseTime)
{
cout << "\n\nPhaseTime Over\n\n";
threadbreak = true;
break;
}
if(PT_Done)
{
cout << "\n\nAll Power Transfered\n\n";
threadbreak = true;
break;
}
}
}
cout<<"\n\n\n\nPT:"<<PT<<" PA:"<<PA;
receiverThread->interrupt();
receiverThread->join();
ms_Agent->~MS_Agent();
cout << "\n\nloop break\n";
return 0;
}