opcuaClient.c 19 KB

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