client_legacy.c 21 KB

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