client.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  1. /*
  2. C ECHO client example using sockets
  3. This is an example client for internal benchmarks. It works, but is not ready
  4. for serious use. We do not really check any of the returns from the server.
  5. */
  6. #include <stdio.h> //printf
  7. #include <string.h> //strlen
  8. #include <sys/socket.h> //socket
  9. #include <arpa/inet.h> //inet_addr
  10. #include <unistd.h> // for close
  11. #include <stdlib.h> // pulls in declaration of malloc, free
  12. #include "ua_transport_generated.h"
  13. #include "ua_types_encoding_binary.h"
  14. #include "ua_util.h"
  15. typedef struct ConnectionInfo {
  16. UA_Int32 socket;
  17. UA_UInt32 channelId;
  18. UA_SequenceHeader sequenceHdr;
  19. UA_NodeId authenticationToken;
  20. UA_UInt32 tokenId;
  21. } ConnectionInfo;
  22. static UA_Int32 sendHello(UA_Int32 sock, UA_String *endpointURL) {
  23. UA_TcpMessageHeader messageHeader;
  24. messageHeader.messageTypeAndFinal = UA_MESSAGETYPEANDFINAL_HELF;
  25. UA_TcpHelloMessage hello;
  26. UA_String_copy(endpointURL, &hello.endpointUrl);
  27. hello.maxChunkCount = 1;
  28. hello.maxMessageSize = 16777216;
  29. hello.protocolVersion = 0;
  30. hello.receiveBufferSize = 65536;
  31. hello.sendBufferSize = 65536;
  32. messageHeader.messageSize = UA_TcpHelloMessage_calcSizeBinary((UA_TcpHelloMessage const*) &hello) +
  33. UA_TcpMessageHeader_calcSizeBinary((UA_TcpMessageHeader const*) &messageHeader);
  34. UA_ByteString message;
  35. UA_ByteString_newMembers(&message, messageHeader.messageSize);
  36. size_t offset = 0;
  37. UA_TcpMessageHeader_encodeBinary((UA_TcpMessageHeader const*) &messageHeader, &message, &offset);
  38. UA_TcpHelloMessage_encodeBinary((UA_TcpHelloMessage const*) &hello, &message, &offset);
  39. UA_Int32 sendret = send(sock, message.data, offset, 0);
  40. UA_ByteString_deleteMembers(&message);
  41. free(hello.endpointUrl.data);
  42. if (sendret < 0)
  43. return 1;
  44. return 0;
  45. }
  46. static int sendOpenSecureChannel(UA_Int32 sock) {
  47. UA_TcpMessageHeader msghdr;
  48. msghdr.messageTypeAndFinal = UA_MESSAGETYPEANDFINAL_OPNF;
  49. UA_UInt32 secureChannelId = 0;
  50. UA_String securityPolicy;
  51. UA_String_copycstring("http://opcfoundation.org/UA/SecurityPolicy#None", &securityPolicy);
  52. UA_String senderCert;
  53. senderCert.data = UA_NULL;
  54. senderCert.length = -1;
  55. UA_String receiverCertThumb;
  56. receiverCertThumb.data = UA_NULL;
  57. receiverCertThumb.length = -1;
  58. UA_UInt32 sequenceNumber = 51;
  59. UA_UInt32 requestId = 1;
  60. UA_NodeId type;
  61. type.identifier.numeric = 446; // id of opensecurechannelrequest
  62. type.identifierType = UA_NODEIDTYPE_NUMERIC;
  63. type.namespaceIndex = 0;
  64. UA_OpenSecureChannelRequest opnSecRq;
  65. UA_OpenSecureChannelRequest_init(&opnSecRq);
  66. opnSecRq.requestHeader.timestamp = UA_DateTime_now();
  67. UA_ByteString_newMembers(&opnSecRq.clientNonce, 1);
  68. opnSecRq.clientNonce.data[0] = 0;
  69. opnSecRq.clientProtocolVersion = 0;
  70. opnSecRq.requestedLifetime = 30000;
  71. opnSecRq.securityMode = UA_MESSAGESECURITYMODE_NONE;
  72. opnSecRq.requestType = UA_SECURITYTOKENREQUESTTYPE_ISSUE;
  73. opnSecRq.requestHeader.authenticationToken.identifier.numeric = 10;
  74. opnSecRq.requestHeader.authenticationToken.identifierType = UA_NODEIDTYPE_NUMERIC;
  75. opnSecRq.requestHeader.authenticationToken.namespaceIndex = 10;
  76. msghdr.messageSize = 135; // todo: compute the message size from the actual content
  77. UA_ByteString message;
  78. UA_ByteString_newMembers(&message, 1000);
  79. size_t offset = 0;
  80. UA_TcpMessageHeader_encodeBinary(&msghdr, &message, &offset);
  81. UA_UInt32_encodeBinary(&secureChannelId, &message, &offset);
  82. UA_String_encodeBinary(&securityPolicy, &message, &offset);
  83. UA_String_encodeBinary(&senderCert, &message, &offset);
  84. UA_String_encodeBinary(&receiverCertThumb, &message, &offset);
  85. UA_UInt32_encodeBinary(&sequenceNumber, &message, &offset);
  86. UA_UInt32_encodeBinary(&requestId, &message, &offset);
  87. UA_NodeId_encodeBinary(&type, &message, &offset);
  88. UA_OpenSecureChannelRequest_encodeBinary(&opnSecRq, &message, &offset);
  89. UA_OpenSecureChannelRequest_deleteMembers(&opnSecRq);
  90. UA_String_deleteMembers(&securityPolicy);
  91. UA_Int32 sendret = send(sock, message.data, offset, 0);
  92. UA_ByteString_deleteMembers(&message);
  93. if (sendret < 0) {
  94. printf("send opensecurechannel failed");
  95. return 1;
  96. }
  97. return 0;
  98. }
  99. static UA_Int32 sendCreateSession(UA_Int32 sock, UA_UInt32 channelId, UA_UInt32 tokenId, UA_UInt32 sequenceNumber,
  100. UA_UInt32 requestId, UA_String *endpointUrl) {
  101. UA_ByteString message;
  102. UA_ByteString_newMembers(&message, 65536);
  103. UA_UInt32 tmpChannelId = channelId;
  104. size_t offset = 0;
  105. UA_TcpMessageHeader msghdr;
  106. msghdr.messageTypeAndFinal = UA_MESSAGETYPEANDFINAL_MSGF;
  107. UA_NodeId type;
  108. type.identifier.numeric = 461;
  109. type.identifierType = UA_NODEIDTYPE_NUMERIC;
  110. type.namespaceIndex = 0;
  111. UA_CreateSessionRequest rq;
  112. UA_CreateSessionRequest_init(&rq);
  113. rq.requestHeader.requestHandle = 1;
  114. rq.requestHeader.timestamp = UA_DateTime_now();
  115. rq.requestHeader.timeoutHint = 10000;
  116. rq.requestHeader.authenticationToken.identifier.numeric = 10;
  117. rq.requestHeader.authenticationToken.identifierType = UA_NODEIDTYPE_NUMERIC;
  118. rq.requestHeader.authenticationToken.namespaceIndex = 10;
  119. UA_String_copy(endpointUrl, &rq.endpointUrl);
  120. UA_String_copycstring("mysession", &rq.sessionName);
  121. UA_String_copycstring("abcd", &rq.clientCertificate);
  122. UA_ByteString_newMembers(&rq.clientNonce, 1);
  123. rq.clientNonce.data[0] = 0;
  124. rq.requestedSessionTimeout = 1200000;
  125. rq.maxResponseMessageSize = UA_INT32_MAX;
  126. msghdr.messageSize = 16 + UA_TcpMessageHeader_calcSizeBinary(&msghdr) + UA_NodeId_calcSizeBinary(&type) +
  127. UA_CreateSessionRequest_calcSizeBinary(&rq);
  128. UA_TcpMessageHeader_encodeBinary(&msghdr, &message, &offset);
  129. UA_UInt32_encodeBinary(&tmpChannelId, &message, &offset);
  130. UA_UInt32_encodeBinary(&tokenId, &message, &offset);
  131. UA_UInt32_encodeBinary(&sequenceNumber, &message, &offset);
  132. UA_UInt32_encodeBinary(&requestId, &message, &offset);
  133. UA_NodeId_encodeBinary(&type, &message, &offset);
  134. UA_CreateSessionRequest_encodeBinary(&rq, &message, &offset);
  135. UA_Int32 sendret = send(sock, message.data, offset, 0);
  136. UA_ByteString_deleteMembers(&message);
  137. UA_CreateSessionRequest_deleteMembers(&rq);
  138. if (sendret < 0) {
  139. printf("send opensecurechannel failed");
  140. return 1;
  141. }
  142. return 0;
  143. }
  144. static UA_Int32 closeSession(ConnectionInfo *connectionInfo) {
  145. size_t offset = 0;
  146. UA_ByteString message;
  147. UA_ByteString_newMembers(&message, 65536);
  148. UA_CloseSessionRequest rq;
  149. UA_CloseSessionRequest_init(&rq);
  150. rq.requestHeader.requestHandle = 1;
  151. rq.requestHeader.timestamp = UA_DateTime_now();
  152. rq.requestHeader.timeoutHint = 10000;
  153. rq.requestHeader.authenticationToken.identifier.numeric = 10;
  154. rq.requestHeader.authenticationToken.identifierType = UA_NODEIDTYPE_NUMERIC;
  155. rq.requestHeader.authenticationToken.namespaceIndex = 10;
  156. rq.deleteSubscriptions = UA_TRUE;
  157. UA_TcpMessageHeader msghdr;
  158. msghdr.messageTypeAndFinal = UA_MESSAGETYPEANDFINAL_MSGF;
  159. UA_NodeId type;
  160. type.identifier.numeric = 473;
  161. type.identifierType = UA_NODEIDTYPE_NUMERIC;
  162. type.namespaceIndex = 0;
  163. msghdr.messageSize = 16 + UA_TcpMessageHeader_calcSizeBinary(&msghdr) + UA_NodeId_calcSizeBinary(&type) +
  164. UA_CloseSessionRequest_calcSizeBinary(&rq);
  165. UA_TcpMessageHeader_encodeBinary(&msghdr, &message, &offset);
  166. UA_UInt32_encodeBinary(&connectionInfo->channelId, &message, &offset);
  167. UA_UInt32_encodeBinary(&connectionInfo->tokenId, &message, &offset);
  168. UA_UInt32_encodeBinary(&connectionInfo->sequenceHdr.sequenceNumber, &message, &offset);
  169. UA_UInt32_encodeBinary(&connectionInfo->sequenceHdr.requestId, &message, &offset);
  170. UA_NodeId_encodeBinary(&type, &message, &offset);
  171. UA_CloseSessionRequest_encodeBinary(&rq, &message, &offset);
  172. UA_Int32 sendret = send(connectionInfo->socket, message.data, offset, 0);
  173. UA_ByteString_deleteMembers(&message);
  174. UA_CloseSessionRequest_deleteMembers(&rq);
  175. if(sendret < 0) {
  176. printf("send closesessionrequest failed");
  177. return 1;
  178. }
  179. return 0;
  180. }
  181. static UA_Int32 closeSecureChannel(ConnectionInfo *connectionInfo) {
  182. size_t offset = 0;
  183. UA_ByteString message;
  184. UA_ByteString_newMembers(&message, 65536);
  185. UA_CloseSecureChannelRequest rq;
  186. UA_CloseSecureChannelRequest_init(&rq);
  187. rq.requestHeader.requestHandle = 1;
  188. rq.requestHeader.timestamp = UA_DateTime_now();
  189. rq.requestHeader.timeoutHint = 10000;
  190. rq.requestHeader.authenticationToken.identifier.numeric = 10;
  191. rq.requestHeader.authenticationToken.identifierType = UA_NODEIDTYPE_NUMERIC;
  192. rq.requestHeader.authenticationToken.namespaceIndex = 10;
  193. UA_TcpMessageHeader msghdr;
  194. msghdr.messageTypeAndFinal = UA_MESSAGETYPEANDFINAL_CLOF;
  195. msghdr.messageSize = 4 + UA_TcpMessageHeader_calcSizeBinary(&msghdr) +
  196. UA_CloseSecureChannelRequest_calcSizeBinary(&rq);
  197. UA_TcpMessageHeader_encodeBinary(&msghdr, &message, &offset);
  198. UA_UInt32_encodeBinary(&connectionInfo->channelId, &message, &offset);
  199. UA_CloseSecureChannelRequest_encodeBinary(&rq, &message, &offset);
  200. UA_Int32 sendret = send(connectionInfo->socket, message.data, offset, 0);
  201. UA_ByteString_deleteMembers(&message);
  202. UA_CloseSecureChannelRequest_deleteMembers(&rq);
  203. if(sendret < 0) {
  204. printf("send CloseSecureChannelRequest failed");
  205. return 1;
  206. }
  207. return 0;
  208. }
  209. static UA_Int32 sendActivateSession(UA_Int32 sock, UA_UInt32 channelId, UA_UInt32 tokenId, UA_UInt32 sequenceNumber,
  210. UA_UInt32 requestId, UA_NodeId authenticationToken) {
  211. UA_ByteString *message = UA_ByteString_new();
  212. UA_ByteString_newMembers(message, 65536);
  213. UA_UInt32 tmpChannelId = channelId;
  214. size_t offset = 0;
  215. UA_TcpMessageHeader msghdr;
  216. msghdr.messageTypeAndFinal = UA_MESSAGETYPEANDFINAL_MSGF;
  217. msghdr.messageSize = 86;
  218. UA_NodeId type;
  219. type.identifier.numeric = 467;
  220. type.identifierType = UA_NODEIDTYPE_NUMERIC;
  221. type.namespaceIndex = 0;
  222. UA_ActivateSessionRequest rq;
  223. UA_ActivateSessionRequest_init(&rq);
  224. rq.requestHeader.requestHandle = 2;
  225. rq.requestHeader.authenticationToken = authenticationToken;
  226. rq.requestHeader.timestamp = UA_DateTime_now();
  227. rq.requestHeader.timeoutHint = 10000;
  228. msghdr.messageSize = 16 + UA_TcpMessageHeader_calcSizeBinary(&msghdr) + UA_NodeId_calcSizeBinary(&type) +
  229. UA_ActivateSessionRequest_calcSizeBinary(&rq);
  230. UA_TcpMessageHeader_encodeBinary(&msghdr, message, &offset);
  231. UA_UInt32_encodeBinary(&tmpChannelId, message, &offset);
  232. UA_UInt32_encodeBinary(&tokenId, message, &offset);
  233. UA_UInt32_encodeBinary(&sequenceNumber, message, &offset);
  234. UA_UInt32_encodeBinary(&requestId, message, &offset);
  235. UA_NodeId_encodeBinary(&type, message, &offset);
  236. UA_ActivateSessionRequest_encodeBinary(&rq, message, &offset);
  237. UA_Int32 sendret = send(sock, message->data, offset, 0);
  238. UA_ByteString_delete(message);
  239. if (sendret < 0) {
  240. printf("send opensecurechannel failed");
  241. return 1;
  242. }
  243. return 0;
  244. }
  245. static UA_Int64 sendReadRequest(ConnectionInfo *connectionInfo, UA_Int32 nodeIds_size,UA_NodeId* nodeIds){
  246. /*UA_Int32 sock, UA_UInt32 channelId, UA_UInt32 tokenId, UA_UInt32 sequenceNumber, UA_UInt32 requestId,
  247. UA_NodeId authenticationToken, UA_Int32 nodeIds_size,UA_NodeId* nodeIds) {
  248. */
  249. UA_ByteString *message = UA_ByteString_new();
  250. UA_ByteString_newMembers(message, 65536);
  251. UA_UInt32 tmpChannelId = connectionInfo->channelId;
  252. size_t offset = 0;
  253. UA_TcpMessageHeader msghdr;
  254. msghdr.messageTypeAndFinal = UA_MESSAGETYPEANDFINAL_MSGF;
  255. UA_NodeId type;
  256. type.identifier.numeric = 631;
  257. type.identifierType = UA_NODEIDTYPE_NUMERIC;
  258. type.namespaceIndex = 0;
  259. UA_ReadRequest rq;
  260. UA_ReadRequest_init(&rq);
  261. rq.maxAge = 0;
  262. rq.nodesToRead = UA_Array_new(&UA_TYPES[UA_TYPES_READVALUEID], nodeIds_size);
  263. rq.nodesToReadSize = nodeIds_size;
  264. for(UA_Int32 i=0;i<nodeIds_size;i++) {
  265. UA_ReadValueId_init(&(rq.nodesToRead[i]));
  266. rq.nodesToRead[i].attributeId = 6; //WriteMask
  267. UA_NodeId_init(&(rq.nodesToRead[i].nodeId));
  268. rq.nodesToRead[i].nodeId = nodeIds[i];
  269. UA_QualifiedName_init(&(rq.nodesToRead[0].dataEncoding));
  270. }
  271. rq.requestHeader.timeoutHint = 10000;
  272. rq.requestHeader.timestamp = UA_DateTime_now();
  273. rq.requestHeader.authenticationToken = connectionInfo->authenticationToken;
  274. rq.timestampsToReturn = 0x03;
  275. rq.requestHeader.requestHandle = 1 + connectionInfo->sequenceHdr.requestId;
  276. msghdr.messageSize = 16 + UA_TcpMessageHeader_calcSizeBinary(&msghdr) + UA_NodeId_calcSizeBinary(&type) +
  277. UA_ReadRequest_calcSizeBinary(&rq);
  278. UA_TcpMessageHeader_encodeBinary(&msghdr,message,&offset);
  279. UA_UInt32_encodeBinary(&tmpChannelId, message, &offset);
  280. UA_UInt32_encodeBinary(&connectionInfo->tokenId, message, &offset);
  281. UA_UInt32_encodeBinary(&connectionInfo->sequenceHdr.sequenceNumber, message, &offset);
  282. UA_UInt32_encodeBinary(&connectionInfo->sequenceHdr.requestId, message, &offset);
  283. UA_NodeId_encodeBinary(&type,message,&offset);
  284. UA_ReadRequest_encodeBinary(&rq, message, &offset);
  285. UA_DateTime tic = UA_DateTime_now();
  286. UA_Int32 sendret = send(connectionInfo->socket, message->data, offset, 0);
  287. UA_Array_delete(rq.nodesToRead, &UA_TYPES[UA_TYPES_READVALUEID], nodeIds_size);
  288. UA_ByteString_delete(message);
  289. if (sendret < 0) {
  290. printf("send readrequest failed");
  291. return 1;
  292. }
  293. return tic;
  294. }
  295. static int ua_client_connectUA(char* ipaddress,int port, UA_String *endpointUrl, ConnectionInfo *connectionInfo,
  296. UA_Boolean stateless, UA_Boolean udp) {
  297. UA_ByteString reply;
  298. UA_ByteString_newMembers(&reply, 65536);
  299. int sock;
  300. struct sockaddr_in server;
  301. //Create socket
  302. if(udp==UA_TRUE){
  303. sock = socket(AF_INET, SOCK_DGRAM, 0);
  304. }else{
  305. sock = socket(AF_INET, SOCK_STREAM, 0);
  306. }
  307. if(sock == -1) {
  308. printf("Could not create socket");
  309. return 1;
  310. }
  311. server.sin_addr.s_addr = inet_addr(ipaddress);
  312. server.sin_family = AF_INET;
  313. server.sin_port = htons(port);
  314. if(connect(sock, (struct sockaddr *) &server, sizeof(server)) < 0) {
  315. perror("connect failed. Error");
  316. return 1;
  317. }
  318. connectionInfo->socket = sock;
  319. if(stateless){
  320. UA_NodeId_init(&connectionInfo->authenticationToken);
  321. connectionInfo->channelId=0;
  322. UA_SequenceHeader_init(&connectionInfo->sequenceHdr);
  323. connectionInfo->tokenId=0;
  324. return 0;
  325. }else{
  326. sendHello(sock, endpointUrl);
  327. recv(sock, reply.data, reply.length, 0);
  328. sendOpenSecureChannel(sock);
  329. recv(sock, reply.data, reply.length, 0);
  330. size_t recvOffset = 0;
  331. UA_TcpMessageHeader msghdr;
  332. UA_TcpMessageHeader_decodeBinary(&reply, &recvOffset, &msghdr);
  333. UA_AsymmetricAlgorithmSecurityHeader asymHeader;
  334. UA_NodeId rspType;
  335. UA_OpenSecureChannelResponse openSecChannelRsp;
  336. UA_UInt32_decodeBinary(&reply, &recvOffset, &connectionInfo->channelId);
  337. UA_AsymmetricAlgorithmSecurityHeader_decodeBinary(&reply,&recvOffset,&asymHeader);
  338. UA_AsymmetricAlgorithmSecurityHeader_deleteMembers(&asymHeader);
  339. UA_SequenceHeader_decodeBinary(&reply,&recvOffset,&connectionInfo->sequenceHdr);
  340. UA_NodeId_decodeBinary(&reply,&recvOffset,&rspType);
  341. UA_OpenSecureChannelResponse_decodeBinary(&reply,&recvOffset,&openSecChannelRsp);
  342. connectionInfo->tokenId = openSecChannelRsp.securityToken.tokenId;
  343. sendCreateSession(sock, connectionInfo->channelId, openSecChannelRsp.securityToken.tokenId, 52, 2, endpointUrl);
  344. recv(sock, reply.data, reply.length, 0);
  345. UA_NodeId messageType;
  346. recvOffset = 24;
  347. UA_NodeId_decodeBinary(&reply,&recvOffset,&messageType);
  348. UA_CreateSessionResponse createSessionResponse;
  349. UA_CreateSessionResponse_decodeBinary(&reply,&recvOffset,&createSessionResponse);
  350. connectionInfo->authenticationToken = createSessionResponse.authenticationToken;
  351. sendActivateSession(sock, connectionInfo->channelId, connectionInfo->tokenId, 53, 3,
  352. connectionInfo->authenticationToken);
  353. recv(sock, reply.data, reply.length, 0);
  354. UA_OpenSecureChannelResponse_deleteMembers(&openSecChannelRsp);
  355. UA_String_deleteMembers(&reply);
  356. UA_CreateSessionResponse_deleteMembers(&createSessionResponse);
  357. return 0;
  358. }
  359. }
  360. int main(int argc, char *argv[]) {
  361. int defaultParams = argc < 8;
  362. //start parameters
  363. if(defaultParams) {
  364. printf("1st parameter: number of nodes to read \n");
  365. printf("2nd parameter: number of read-tries \n");
  366. printf("3rd parameter: name of the file to save measurement data \n");
  367. printf("4th parameter: 1 = read same node, 0 = read different nodes \n");
  368. printf("5th parameter: ip adress \n");
  369. printf("6th parameter: port \n");
  370. printf("7th parameter: 0=stateful, 1=stateless\n");
  371. printf("8th parameter: 0=tcp, 1=udp (only with stateless calls)\n");
  372. printf("\nUsing default parameters. \n");
  373. }
  374. UA_UInt32 nodesToReadSize;
  375. UA_UInt32 tries;
  376. UA_Boolean alwaysSameNode;
  377. UA_ByteString reply;
  378. UA_ByteString_newMembers(&reply, 65536);
  379. UA_Boolean stateless;
  380. UA_Boolean udp;
  381. if(defaultParams)
  382. nodesToReadSize = 1;
  383. else
  384. nodesToReadSize = atoi(argv[1]);
  385. if(defaultParams)
  386. tries= 2;
  387. else
  388. tries = (UA_UInt32) atoi(argv[2]);
  389. if(defaultParams){
  390. alwaysSameNode = UA_TRUE;
  391. }else{
  392. if(atoi(argv[4]) != 0)
  393. alwaysSameNode = UA_TRUE;
  394. else
  395. alwaysSameNode = UA_FALSE;
  396. }
  397. if(defaultParams){
  398. stateless = UA_FALSE;
  399. }else{
  400. if(atoi(argv[7]) != 0)
  401. stateless = UA_TRUE;
  402. else
  403. stateless = UA_FALSE;
  404. }
  405. if(defaultParams){
  406. udp = UA_FALSE;
  407. }else{
  408. if(atoi(argv[8]) != 0)
  409. udp = UA_TRUE;
  410. else
  411. udp = UA_FALSE;
  412. }
  413. //Connect to remote server
  414. UA_String endpoint;
  415. UA_String_copycstring("none",&endpoint);
  416. ConnectionInfo connectionInfo;
  417. /* REQUEST START*/
  418. UA_NodeId *nodesToRead;
  419. nodesToRead = UA_Array_new(&UA_TYPES[UA_TYPES_NODEID], nodesToReadSize);
  420. for(UA_UInt32 i = 0; i<nodesToReadSize; i++) {
  421. if(alwaysSameNode)
  422. nodesToRead[i].identifier.numeric = 2253; //ask always the same node
  423. else
  424. nodesToRead[i].identifier.numeric = 19000 +i;
  425. nodesToRead[i].identifierType = UA_NODEIDTYPE_NUMERIC;
  426. nodesToRead[i].namespaceIndex = 0;
  427. }
  428. UA_DateTime tic, toc;
  429. UA_Double *timeDiffs;
  430. UA_Int32 received;
  431. timeDiffs = UA_Array_new(&UA_TYPES[UA_TYPES_DOUBLE], tries);
  432. UA_Double sum = 0;
  433. for(UA_UInt32 i = 0; i < tries; i++) {
  434. //if(stateless || (!stateless && i==0)){
  435. tic = UA_DateTime_now();
  436. if(defaultParams){
  437. if(ua_client_connectUA("127.0.0.1",atoi("16664"),&endpoint,&connectionInfo,stateless,udp) != 0){
  438. return 0;
  439. }
  440. }else{
  441. if(ua_client_connectUA(argv[5],atoi(argv[6]),&endpoint,&connectionInfo,stateless,udp) != 0){
  442. return 0;
  443. }
  444. }
  445. //}
  446. sendReadRequest(&connectionInfo,nodesToReadSize,nodesToRead);
  447. received = recv(connectionInfo.socket, reply.data, 2000, 0);
  448. toc = UA_DateTime_now() - tic;
  449. timeDiffs[i] = (UA_Double)toc/(UA_Double)1e4;
  450. sum = sum + timeDiffs[i];
  451. closeSession(&connectionInfo);
  452. recv(connectionInfo.socket, reply.data, 2000, 0);
  453. closeSecureChannel(&connectionInfo);
  454. //if(stateless || (!stateless && i==tries-1)){
  455. close(connectionInfo.socket);
  456. //}
  457. }
  458. /* REQUEST END*/
  459. UA_Double mean = sum / tries;
  460. printf("mean time for handling request: %16.10f ms \n",mean);
  461. if(received>0)
  462. printf("received: %i\n",received); // dummy
  463. //save to file
  464. char data[100];
  465. const char flag = 'a';
  466. FILE* fHandle = UA_NULL;
  467. if (defaultParams) {
  468. fHandle = fopen("client.log", &flag);
  469. }else{
  470. fHandle = fopen(argv[3], &flag);
  471. }
  472. //header
  473. UA_Int32 bytesToWrite = sprintf(data, "measurement %s in ms, nodesToRead %d \n", argv[3], nodesToReadSize);
  474. fwrite(data,1,bytesToWrite,fHandle);
  475. for(UA_UInt32 i=0;i<tries;i++) {
  476. bytesToWrite = sprintf(data,"%16.10f \n",timeDiffs[i]);
  477. fwrite(data,1,bytesToWrite,fHandle);
  478. }
  479. fclose(fHandle);
  480. UA_String_deleteMembers(&reply);
  481. UA_Array_delete(nodesToRead,&UA_TYPES[UA_TYPES_NODEID], nodesToReadSize);
  482. UA_free(timeDiffs);
  483. return 0;
  484. }