This commit was generated by cvs2svn to compensate for changes in r12269,
[u/mrichter/AliRoot.git] / SHUTTLE / test / TestServer.cxx
1 #include "TestServer.h"
2
3 #include "AliDCSMessage.h"
4 #include "AliLog.h"
5
6 #include <TTimeStamp.h>
7
8 ClassImp(TestServer)
9
10 const Int_t TestServer::kBadState;
11
12 const Int_t TestServer::kTimeout;
13
14 const Int_t TestServer::kBadMessage;
15
16 const Int_t TestServer::kCommError;
17
18 const Int_t TestServer::kServerError;
19
20 TestServer::TestServer(Int_t port, Long_t timeout, Int_t retries):
21         fServerSocket(port), fTimeout(timeout), fRetries(retries)
22 {
23
24 }
25
26 Int_t TestServer::SendBuffer(TSocket* socket, const char* buffer, Int_t size) {
27
28         Int_t sentSize = 0;
29         Int_t tries = 0;
30
31         while (sentSize < size && tries < fRetries) {
32
33                 Int_t sResult = socket->Select(TSocket::kWrite, fTimeout);
34
35                 if (sResult == 0) {
36                         AliDebug(1, Form("Timeout! tries <%d> ...", tries));
37                         tries ++;
38                         continue;
39
40                 } else if (sResult < 0) {
41                         AliDebug(1, Form("Communication error <%d>!",
42                                         socket->GetErrorCode()));
43                         return TestServer::kCommError;
44                 }
45
46                 sResult = socket->SendRaw(buffer + sentSize, size - sentSize,
47                                         kDontBlock);
48
49                 if (sResult > 0) {
50                         sentSize += sResult;
51                 } else {
52                         AliDebug(1, Form("Communication error <%d>!",
53                                         socket->GetErrorCode()));
54                         return TestServer::kCommError;
55                 } 
56         }
57
58         if (tries == fRetries) {
59                 return TestServer::kTimeout;
60         }
61
62         return sentSize;
63 }
64
65 Int_t TestServer::ReceiveBuffer(TSocket* socket, char* buffer, Int_t size) {
66
67         Int_t readSize = 0;
68         Int_t tries = 0;
69
70         while (readSize < size && tries < fRetries) {
71
72                 Int_t sResult = socket->Select(TSocket::kRead, fTimeout);
73
74                 if (sResult == 0) {
75                         AliDebug(1, Form("Timeout! tries <%d> ...", tries));
76                         tries ++;
77                         continue;
78
79                 } else if (sResult < 0) {
80                         AliDebug(1, Form("Communication error <%d>!",
81                                         socket->GetErrorCode()));
82                         return TestServer::kCommError;
83                 }
84
85                 sResult = socket->RecvRaw(buffer + readSize, size - readSize,
86                                          kDontBlock);
87
88                 if (sResult > 0) {
89                         readSize += sResult;
90                 } else {
91                         AliDebug(1, Form("Communication error <%d>!",
92                                         socket->GetErrorCode()));
93                         return TestServer::kCommError;
94                 }
95         }
96
97         if (tries == fRetries) {
98                 return TestServer::kTimeout;
99         }
100
101         return readSize;
102 }
103
104 Int_t TestServer::SendMessage(TSocket* socket, AliDCSMessage& message) {
105         
106         message.StoreToBuffer();
107         
108         return SendBuffer(socket, message.GetMessage(), 
109                                 message.GetMessageSize());
110 }
111
112 Int_t TestServer::ReceiveMessage(TSocket* socket, AliDCSMessage& message) {
113
114         char header[HEADER_SIZE];
115
116         Int_t sResult;
117
118         if ((sResult = ReceiveBuffer(socket, header, HEADER_SIZE)) < 0) {
119                 return sResult;
120         }
121
122         if (!message.SetRawHeader(header)) {
123                 return TestServer::kBadMessage;
124         }
125
126         if ((sResult = ReceiveBuffer(socket, message.GetBody(),
127                         message.GetBodySize())) < 0) {
128
129                 return sResult;
130         }
131
132         message.LoadFromBuffer();       
133
134         return HEADER_SIZE + sResult;
135 }
136
137
138 void TestServer::Run(Int_t count, Int_t rsSize) {
139
140         if (!fServerSocket.IsValid()) {
141                 AliError("Invalid server socket!");
142                 return;
143         }
144
145
146         TSocket* connSocket;
147         while (1) {
148                 
149                 AliInfo("Waiting for connection");
150
151                 connSocket = fServerSocket.Accept();
152
153                 AliInfo("Client accepted");
154
155                 if (!connSocket) {
156                         AliError("Can't accept connection!");
157                         continue;
158                 }
159
160                 connSocket->SetOption(kNoBlock, 1);
161
162                 if (!connSocket->IsValid()) {
163                         AliError("Invalid connection socket!");
164                         delete connSocket;
165                         continue;
166                 }
167
168                 Int_t result;
169
170                 AliDCSMessage message;
171                 if ((result = ReceiveMessage(connSocket, message)) < 0) {
172                         AliError(Form("Communication failure: ", result));
173                         delete connSocket;
174                         continue;
175                 }
176                 
177                 //message.Print();
178
179                 if (message.GetType() != AliDCSMessage::kRequest) {
180                         AliError("Bad message type!");
181                         delete connSocket;
182                         continue;
183                 }
184
185                 AliDCSMessage countMessage;
186                 countMessage.CreateCountMessage(count);
187                 if ((result = SendMessage(connSocket, countMessage)) < 0) {
188                         AliError(Form("Communication failure: %d", result));
189                         continue;
190                 }
191
192                         
193                 Int_t sentValues = 0;
194                 
195                 AliInfo(Form("Count: %d", count));
196
197                 while (sentValues < count) {
198                         
199                         Int_t pSize = rsSize < count - sentValues ? 
200                                         rsSize : count - sentValues;
201
202
203                         TTimeStamp currentTime;
204                         AliDCSMessage rsMessage;
205                         rsMessage.CreateResultSetMessage(AliSimpleValue::kInt);
206                                 
207                         for (Int_t k = 0; k < pSize; k ++) {
208                                 AliDCSValue aValue(k, currentTime.GetSec() + k);
209                                 rsMessage.AddValue(aValue);
210                         }
211
212                         if ((result = SendMessage(connSocket, rsMessage)) < 0) {
213                                 AliError(Form("Communication failure: %d", 
214                                                 result));
215                                 break;
216                         }
217
218                         sentValues += pSize;
219                 }
220                 
221                 AliInfo(Form("Sent values: %d", sentValues));           
222                 delete connSocket; 
223
224                 //break;
225         }
226 }