client_legacy.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  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. //this legacy stuff can not be build with amalgamation options, since it need internal APIs
  13. #include "ua_util.h"
  14. #include "ua_transport_generated_encoding_binary.h"
  15. #include "ua_types_generated_encoding_binary.h"
  16. typedef struct ConnectionInfo {
  17. UA_Int32 socket;
  18. UA_UInt32 channelId;
  19. UA_SequenceHeader sequenceHdr;
  20. UA_NodeId authenticationToken;
  21. UA_UInt32 tokenId;
  22. UA_UInt32 reqestHandle;
  23. } ConnectionInfo;
  24. static UA_Int32 sendHello(UA_Int32 sock, UA_String *endpointURL) {
  25. UA_TcpMessageHeader messageHeader;
  26. messageHeader.messageTypeAndFinal = UA_MESSAGETYPEANDFINAL_HELF;
  27. UA_TcpHelloMessage hello;
  28. UA_String_copy(endpointURL, &hello.endpointUrl);
  29. hello.maxChunkCount = 1;
  30. hello.maxMessageSize = 16777216;
  31. hello.protocolVersion = 0;
  32. hello.receiveBufferSize = 65536;
  33. hello.sendBufferSize = 65536;
  34. UA_ByteString message;
  35. UA_ByteString_newMembers(&message, 1024);
  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. messageHeader.messageSize = offset;
  40. 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. //workaround to get length calculated
  131. offset = 0;
  132. UA_CreateSessionRequest_encodeBinary(&rq, &message, &offset);
  133. UA_TcpMessageHeader_encodeBinary(&msghdr, &message, &offset);
  134. UA_NodeId_encodeBinary(&type, &message, &offset);
  135. msghdr.messageSize = 16 + offset;
  136. offset = 0;
  137. UA_TcpMessageHeader_encodeBinary(&msghdr, &message, &offset);
  138. UA_UInt32_encodeBinary(&tmpChannelId, &message, &offset);
  139. UA_UInt32_encodeBinary(&tokenId, &message, &offset);
  140. UA_UInt32_encodeBinary(&sequenceNumber, &message, &offset);
  141. UA_UInt32_encodeBinary(&requestId, &message, &offset);
  142. UA_NodeId_encodeBinary(&type, &message, &offset);
  143. UA_CreateSessionRequest_encodeBinary(&rq, &message, &offset);
  144. UA_Int32 sendret = send(sock, message.data, offset, 0);
  145. UA_ByteString_deleteMembers(&message);
  146. //fixme: potential leak
  147. //UA_CreateSessionRequest_deleteMembers(&rq);
  148. if (sendret < 0) {
  149. printf("send opensecurechannel failed");
  150. return 1;
  151. }
  152. return 0;
  153. }
  154. static UA_Int32 closeSession(ConnectionInfo *connectionInfo) {
  155. size_t offset = 0;
  156. UA_ByteString message;
  157. UA_ByteString_newMembers(&message, 65536);
  158. UA_CloseSessionRequest rq;
  159. UA_CloseSessionRequest_init(&rq);
  160. rq.requestHeader.requestHandle = connectionInfo->reqestHandle++;
  161. rq.requestHeader.timestamp = UA_DateTime_now();
  162. rq.requestHeader.timeoutHint = 10000;
  163. rq.requestHeader.authenticationToken = connectionInfo->authenticationToken;
  164. rq.deleteSubscriptions = UA_TRUE;
  165. UA_TcpMessageHeader msghdr;
  166. msghdr.messageTypeAndFinal = UA_MESSAGETYPEANDFINAL_MSGF;
  167. UA_NodeId type;
  168. type.identifier.numeric = 473;
  169. type.identifierType = UA_NODEIDTYPE_NUMERIC;
  170. type.namespaceIndex = 0;
  171. //workaround to get length calculated
  172. offset = 0;
  173. UA_CloseSessionRequest_encodeBinary(&rq, &message, &offset);
  174. UA_TcpMessageHeader_encodeBinary(&msghdr, &message, &offset);
  175. UA_NodeId_encodeBinary(&type, &message, &offset);
  176. msghdr.messageSize = 16 + offset;
  177. offset = 0;
  178. UA_TcpMessageHeader_encodeBinary(&msghdr, &message, &offset);
  179. UA_UInt32_encodeBinary(&connectionInfo->channelId, &message, &offset);
  180. UA_UInt32_encodeBinary(&connectionInfo->tokenId, &message, &offset);
  181. connectionInfo->sequenceHdr.sequenceNumber++;
  182. UA_UInt32_encodeBinary(&connectionInfo->sequenceHdr.sequenceNumber, &message, &offset);
  183. connectionInfo->sequenceHdr.requestId++;
  184. UA_UInt32_encodeBinary(&connectionInfo->sequenceHdr.requestId, &message, &offset);
  185. UA_NodeId_encodeBinary(&type, &message, &offset);
  186. UA_CloseSessionRequest_encodeBinary(&rq, &message, &offset);
  187. UA_Int32 sendret = send(connectionInfo->socket, message.data, offset, 0);
  188. UA_ByteString_deleteMembers(&message);
  189. UA_CloseSessionRequest_deleteMembers(&rq);
  190. if(sendret < 0) {
  191. printf("send closesessionrequest failed");
  192. return 1;
  193. }
  194. return 0;
  195. }
  196. static UA_Int32 closeSecureChannel(ConnectionInfo *connectionInfo) {
  197. size_t offset = 0;
  198. UA_ByteString message;
  199. UA_ByteString_newMembers(&message, 65536);
  200. UA_CloseSecureChannelRequest rq;
  201. UA_CloseSecureChannelRequest_init(&rq);
  202. rq.requestHeader.requestHandle = 1;
  203. rq.requestHeader.timestamp = UA_DateTime_now();
  204. rq.requestHeader.timeoutHint = 10000;
  205. rq.requestHeader.authenticationToken.identifier.numeric = 10;
  206. rq.requestHeader.authenticationToken.identifierType = UA_NODEIDTYPE_NUMERIC;
  207. rq.requestHeader.authenticationToken.namespaceIndex = 10;
  208. UA_TcpMessageHeader msghdr;
  209. msghdr.messageTypeAndFinal = UA_MESSAGETYPEANDFINAL_CLOF;
  210. //workaround to get length calculated
  211. offset = 0;
  212. UA_CloseSecureChannelRequest_encodeBinary(&rq, &message, &offset);
  213. UA_TcpMessageHeader_encodeBinary(&msghdr, &message, &offset);
  214. msghdr.messageSize = 4 + offset;
  215. offset = 0;
  216. UA_TcpMessageHeader_encodeBinary(&msghdr, &message, &offset);
  217. UA_UInt32_encodeBinary(&connectionInfo->channelId, &message, &offset);
  218. UA_CloseSecureChannelRequest_encodeBinary(&rq, &message, &offset);
  219. UA_Int32 sendret = send(connectionInfo->socket, message.data, offset, 0);
  220. UA_ByteString_deleteMembers(&message);
  221. UA_CloseSecureChannelRequest_deleteMembers(&rq);
  222. if(sendret < 0) {
  223. printf("send CloseSecureChannelRequest failed");
  224. return 1;
  225. }
  226. return 0;
  227. }
  228. static UA_Int32 sendActivateSession(UA_Int32 sock, UA_UInt32 channelId, UA_UInt32 tokenId, UA_UInt32 sequenceNumber,
  229. UA_UInt32 requestId, UA_NodeId authenticationToken) {
  230. UA_ByteString *message = UA_ByteString_new();
  231. UA_ByteString_newMembers(message, 65536);
  232. UA_UInt32 tmpChannelId = channelId;
  233. size_t offset = 0;
  234. UA_TcpMessageHeader msghdr;
  235. msghdr.messageTypeAndFinal = UA_MESSAGETYPEANDFINAL_MSGF;
  236. msghdr.messageSize = 86;
  237. UA_NodeId type;
  238. type.identifier.numeric = 467;
  239. type.identifierType = UA_NODEIDTYPE_NUMERIC;
  240. type.namespaceIndex = 0;
  241. UA_ActivateSessionRequest rq;
  242. UA_ActivateSessionRequest_init(&rq);
  243. rq.requestHeader.requestHandle = 2;
  244. rq.requestHeader.authenticationToken = authenticationToken;
  245. rq.requestHeader.timestamp = UA_DateTime_now();
  246. rq.requestHeader.timeoutHint = 10000;
  247. //workaround to get length calculated
  248. offset = 0;
  249. UA_ActivateSessionRequest_encodeBinary(&rq, message, &offset);
  250. UA_TcpMessageHeader_encodeBinary(&msghdr, message, &offset);
  251. UA_NodeId_encodeBinary(&type, message, &offset);
  252. msghdr.messageSize = 16 + offset;
  253. offset = 0;
  254. UA_TcpMessageHeader_encodeBinary(&msghdr, message, &offset);
  255. UA_UInt32_encodeBinary(&tmpChannelId, message, &offset);
  256. UA_UInt32_encodeBinary(&tokenId, message, &offset);
  257. UA_UInt32_encodeBinary(&sequenceNumber, message, &offset);
  258. UA_UInt32_encodeBinary(&requestId, message, &offset);
  259. UA_NodeId_encodeBinary(&type, message, &offset);
  260. UA_ActivateSessionRequest_encodeBinary(&rq, message, &offset);
  261. UA_Int32 sendret = send(sock, message->data, offset, 0);
  262. UA_ByteString_delete(message);
  263. if (sendret < 0) {
  264. printf("send opensecurechannel failed");
  265. return 1;
  266. }
  267. return 0;
  268. }
  269. static UA_Int64 sendReadRequest(ConnectionInfo *connectionInfo, UA_Int32 nodeIds_size,UA_NodeId* nodeIds){
  270. /*UA_Int32 sock, UA_UInt32 channelId, UA_UInt32 tokenId, UA_UInt32 sequenceNumber, UA_UInt32 requestId,
  271. UA_NodeId authenticationToken, UA_Int32 nodeIds_size,UA_NodeId* nodeIds) {
  272. */
  273. UA_ByteString *message = UA_ByteString_new();
  274. UA_ByteString_newMembers(message, 65536);
  275. UA_UInt32 tmpChannelId = connectionInfo->channelId;
  276. size_t offset = 0;
  277. UA_TcpMessageHeader msghdr;
  278. msghdr.messageTypeAndFinal = UA_MESSAGETYPEANDFINAL_MSGF;
  279. UA_NodeId type;
  280. type.identifier.numeric = 631;
  281. type.identifierType = UA_NODEIDTYPE_NUMERIC;
  282. type.namespaceIndex = 0;
  283. UA_ReadRequest rq;
  284. UA_ReadRequest_init(&rq);
  285. rq.maxAge = 0;
  286. rq.nodesToRead = UA_Array_new(&UA_TYPES[UA_TYPES_READVALUEID], nodeIds_size);
  287. rq.nodesToReadSize = 1;
  288. for(UA_Int32 i=0;i<nodeIds_size;i++) {
  289. UA_ReadValueId_init(&(rq.nodesToRead[i]));
  290. rq.nodesToRead[i].attributeId = 6; //WriteMask
  291. UA_NodeId_init(&(rq.nodesToRead[i].nodeId));
  292. rq.nodesToRead[i].nodeId = nodeIds[i];
  293. UA_QualifiedName_init(&(rq.nodesToRead[0].dataEncoding));
  294. }
  295. rq.requestHeader.timeoutHint = 10000;
  296. rq.requestHeader.timestamp = UA_DateTime_now();
  297. rq.requestHeader.authenticationToken = connectionInfo->authenticationToken;
  298. rq.timestampsToReturn = 0x03;
  299. rq.requestHeader.requestHandle = connectionInfo->reqestHandle++;
  300. //workaround to get length calculated
  301. offset = 0;
  302. UA_ReadRequest_encodeBinary(&rq, message, &offset);
  303. UA_TcpMessageHeader_encodeBinary(&msghdr, message, &offset);
  304. UA_NodeId_encodeBinary(&type, message, &offset);
  305. msghdr.messageSize = 16 + offset;
  306. offset = 0;
  307. UA_TcpMessageHeader_encodeBinary(&msghdr,message,&offset);
  308. UA_UInt32_encodeBinary(&tmpChannelId, message, &offset);
  309. UA_UInt32_encodeBinary(&connectionInfo->tokenId, message, &offset);
  310. connectionInfo->sequenceHdr.sequenceNumber++;
  311. UA_UInt32_encodeBinary(&connectionInfo->sequenceHdr.sequenceNumber, message, &offset);
  312. connectionInfo->sequenceHdr.requestId++;
  313. UA_UInt32_encodeBinary(&connectionInfo->sequenceHdr.requestId, message, &offset);
  314. UA_NodeId_encodeBinary(&type,message,&offset);
  315. UA_ReadRequest_encodeBinary(&rq, message, &offset);
  316. UA_DateTime tic = UA_DateTime_now();
  317. UA_Int32 sendret = send(connectionInfo->socket, message->data, offset, 0);
  318. UA_Array_delete(rq.nodesToRead, &UA_TYPES[UA_TYPES_READVALUEID], nodeIds_size);
  319. UA_ByteString_delete(message);
  320. if (sendret < 0) {
  321. printf("send readrequest failed");
  322. return 1;
  323. }
  324. return tic;
  325. }
  326. static int ua_client_connectUA(char* ipaddress,int port, UA_String *endpointUrl, ConnectionInfo *connectionInfo,
  327. UA_Boolean stateless, UA_Boolean udp) {
  328. UA_ByteString reply;
  329. UA_ByteString_newMembers(&reply, 65536);
  330. int sock;
  331. struct sockaddr_in server;
  332. //Create socket
  333. if(udp==UA_TRUE){
  334. sock = socket(AF_INET, SOCK_DGRAM, 0);
  335. }else{
  336. sock = socket(AF_INET, SOCK_STREAM, 0);
  337. }
  338. if(sock == -1) {
  339. printf("Could not create socket");
  340. return 1;
  341. }
  342. server.sin_addr.s_addr = inet_addr(ipaddress);
  343. server.sin_family = AF_INET;
  344. server.sin_port = htons(port);
  345. if(connect(sock, (struct sockaddr *) &server, sizeof(server)) < 0) {
  346. perror("connect failed. Error");
  347. return 1;
  348. }
  349. connectionInfo->socket = sock;
  350. if(stateless){
  351. UA_NodeId_init(&connectionInfo->authenticationToken);
  352. connectionInfo->channelId=0;
  353. UA_SequenceHeader_init(&connectionInfo->sequenceHdr);
  354. connectionInfo->tokenId=0;
  355. connectionInfo->reqestHandle = 1;
  356. return 0;
  357. }else{
  358. sendHello(sock, endpointUrl);
  359. recv(sock, reply.data, reply.length, 0);
  360. sendOpenSecureChannel(sock);
  361. recv(sock, reply.data, reply.length, 0);
  362. size_t recvOffset = 0;
  363. UA_TcpMessageHeader msghdr;
  364. UA_TcpMessageHeader_decodeBinary(&reply, &recvOffset, &msghdr);
  365. UA_AsymmetricAlgorithmSecurityHeader asymHeader;
  366. UA_NodeId rspType;
  367. UA_OpenSecureChannelResponse openSecChannelRsp;
  368. UA_UInt32_decodeBinary(&reply, &recvOffset, &connectionInfo->channelId);
  369. UA_AsymmetricAlgorithmSecurityHeader_decodeBinary(&reply,&recvOffset,&asymHeader);
  370. UA_AsymmetricAlgorithmSecurityHeader_deleteMembers(&asymHeader);
  371. UA_SequenceHeader_decodeBinary(&reply,&recvOffset,&connectionInfo->sequenceHdr);
  372. UA_NodeId_decodeBinary(&reply,&recvOffset,&rspType);
  373. UA_OpenSecureChannelResponse_decodeBinary(&reply,&recvOffset,&openSecChannelRsp);
  374. connectionInfo->tokenId = openSecChannelRsp.securityToken.tokenId;
  375. sendCreateSession(sock, connectionInfo->channelId, openSecChannelRsp.securityToken.tokenId, 52, 2, endpointUrl);
  376. recv(sock, reply.data, reply.length, 0);
  377. UA_NodeId messageType;
  378. recvOffset = 24;
  379. UA_NodeId_decodeBinary(&reply,&recvOffset,&messageType);
  380. UA_CreateSessionResponse createSessionResponse;
  381. UA_CreateSessionResponse_decodeBinary(&reply,&recvOffset,&createSessionResponse);
  382. connectionInfo->authenticationToken = createSessionResponse.authenticationToken;
  383. sendActivateSession(sock, connectionInfo->channelId, connectionInfo->tokenId, 53, 3,
  384. connectionInfo->authenticationToken);
  385. recv(sock, reply.data, reply.length, 0);
  386. connectionInfo->sequenceHdr.requestId = 3;
  387. connectionInfo->sequenceHdr.sequenceNumber = 53;
  388. connectionInfo->reqestHandle = 3;
  389. UA_OpenSecureChannelResponse_deleteMembers(&openSecChannelRsp);
  390. UA_String_deleteMembers(&reply);
  391. UA_CreateSessionResponse_deleteMembers(&createSessionResponse);
  392. return 0;
  393. }
  394. }
  395. int main(int argc, char *argv[]) {
  396. int defaultParams = argc < 8;
  397. //start parameters
  398. if(defaultParams) {
  399. printf("1st parameter: number of nodes to read \n");
  400. printf("2nd parameter: number of measured read-tries \n");
  401. printf("3rd parameter: name of the file to save measurement data \n");
  402. printf("4th parameter: 1 = read same node, 0 = read different nodes \n");
  403. printf("5th parameter: ip adress \n");
  404. printf("6th parameter: port \n");
  405. printf("7th parameter: 0=stateful, 1=stateless\n");
  406. printf("8th parameter: 0=tcp, 1=udp (only with stateless calls)\n");
  407. printf("9th parameter: number of tries to run in (not measured, added to tries)\n");
  408. printf("9th parameter: number of tries to run out (not measured, added to tries)\n");
  409. printf("\nUsing default parameters. \n");
  410. }
  411. UA_UInt32 nodesToReadSize;
  412. UA_UInt32 tries;
  413. UA_Boolean alwaysSameNode;
  414. UA_ByteString reply;
  415. UA_ByteString_newMembers(&reply, 65536);
  416. UA_Boolean stateless;
  417. UA_Boolean udp;
  418. UA_UInt32 runIn;
  419. UA_UInt32 runOut;
  420. if(defaultParams)
  421. nodesToReadSize = 1;
  422. else
  423. nodesToReadSize = atoi(argv[1]);
  424. if(defaultParams)
  425. tries= 1;
  426. else
  427. tries = (UA_UInt32) atoi(argv[2]);
  428. if(defaultParams){
  429. alwaysSameNode = UA_TRUE;
  430. }else{
  431. if(atoi(argv[4]) != 0)
  432. alwaysSameNode = UA_TRUE;
  433. else
  434. alwaysSameNode = UA_FALSE;
  435. }
  436. if(defaultParams){
  437. stateless = UA_FALSE;
  438. }else{
  439. if(atoi(argv[7]) != 0)
  440. stateless = UA_TRUE;
  441. else
  442. stateless = UA_FALSE;
  443. }
  444. if(defaultParams){
  445. udp = UA_FALSE;
  446. }else{
  447. if(atoi(argv[8]) != 0)
  448. udp = UA_TRUE;
  449. else
  450. udp = UA_FALSE;
  451. }
  452. if(defaultParams){
  453. runIn = 0;
  454. }else{
  455. runIn = (UA_UInt32) atoi(argv[9]);
  456. }
  457. if(defaultParams){
  458. runOut = 0;
  459. }else{
  460. runOut = (UA_UInt32) atoi(argv[10]);
  461. }
  462. //Connect to remote server
  463. UA_String endpoint;
  464. endpoint = UA_STRING("none");
  465. ConnectionInfo connectionInfo;
  466. /* REQUEST START*/
  467. UA_NodeId *nodesToRead;
  468. nodesToRead = UA_Array_new(&UA_TYPES[UA_TYPES_NODEID], 1);
  469. for(UA_UInt32 i = 0; i<1; i++) {
  470. if(alwaysSameNode)
  471. nodesToRead[i].identifier.numeric = 2253; //ask always the same node
  472. else
  473. nodesToRead[i].identifier.numeric = 19000 +i;
  474. nodesToRead[i].identifierType = UA_NODEIDTYPE_NUMERIC;
  475. nodesToRead[i].namespaceIndex = 0;
  476. }
  477. UA_DateTime tic, toc;
  478. UA_Double *timeDiffs;
  479. UA_Int32 received = 0;
  480. timeDiffs = UA_Array_new(&UA_TYPES[UA_TYPES_DOUBLE], tries);
  481. UA_Double sum = 0;
  482. tic = UA_DateTime_now();
  483. /**
  484. UA_Double duration;
  485. UA_UInt32 count = 0;
  486. UA_Double start = 0, stop = 0;
  487. UA_UInt32 timeToRun = 30;
  488. UA_UInt32 timeToStart = 8;
  489. UA_UInt32 timeToStop = 22;
  490. do{
  491. toc = UA_DateTime_now();
  492. duration = ((UA_Double)toc-(UA_Double)tic)/(UA_Double)1e4;
  493. if(duration>=timeToStart*1000 && duration <= timeToStop*1000){
  494. if(start==0.0){
  495. start=UA_DateTime_now();
  496. }
  497. }
  498. //if(stateless || (!stateless && i==0)){
  499. if(defaultParams){
  500. if(ua_client_connectUA("127.0.0.1",atoi("16664"),&endpoint,&connectionInfo,stateless,udp) != 0){
  501. return 0;
  502. }
  503. }else{
  504. if(ua_client_connectUA(argv[5],atoi(argv[6]),&endpoint,&connectionInfo,stateless,udp) != 0){
  505. return 0;
  506. }
  507. }
  508. //}
  509. sendReadRequest(&connectionInfo,1,nodesToRead);
  510. received = recv(connectionInfo.socket, reply.data, 2000, 0);
  511. if(duration>=timeToStart*1000 && duration <= timeToStop*1000){
  512. count++;
  513. }
  514. if(!stateless){
  515. closeSession(&connectionInfo);
  516. recv(connectionInfo.socket, reply.data, 2000, 0);
  517. closeSecureChannel(&connectionInfo);
  518. }
  519. //if(stateless || (!stateless && i==tries-1)){
  520. close(connectionInfo.socket);
  521. //}
  522. if(duration >= timeToStop*1000 && stop==0){
  523. stop=UA_DateTime_now();
  524. 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));
  525. }
  526. }while(duration<timeToRun*1000);
  527. exit(0);
  528. **/
  529. for(UA_UInt32 i = 0; i < tries + runIn + runOut; i++) {
  530. if(i >= runIn && i < runIn+tries)
  531. tic = UA_DateTime_now();
  532. if(defaultParams){
  533. if(ua_client_connectUA("127.0.0.1",atoi("16664"),&endpoint,&connectionInfo,stateless,udp) != 0){
  534. return 0;
  535. }
  536. }else{
  537. if(ua_client_connectUA(argv[5],atoi(argv[6]),&endpoint,&connectionInfo,stateless,udp) != 0){
  538. return 0;
  539. }
  540. }
  541. for(UA_UInt32 i = 0; i < nodesToReadSize; i++) {
  542. sendReadRequest(&connectionInfo,1,nodesToRead);
  543. received = recv(connectionInfo.socket, reply.data, 2000, 0);
  544. }
  545. if(!stateless){
  546. closeSession(&connectionInfo);
  547. recv(connectionInfo.socket, reply.data, 2000, 0);
  548. closeSecureChannel(&connectionInfo);
  549. }
  550. close(connectionInfo.socket);
  551. if(i >= runIn && i < runIn+tries){
  552. toc = UA_DateTime_now() - tic;
  553. timeDiffs[i-runIn] = (UA_Double)toc/(UA_Double)1e4;
  554. sum = sum + timeDiffs[i-runIn];
  555. }
  556. }
  557. /* REQUEST END*/
  558. UA_Double mean = sum / tries;
  559. printf("total time for requests: %16.10f ms \n",sum);
  560. printf("mean time for handling request: %16.10f ms \n",mean);
  561. if(received>0)
  562. printf("received: %i\n",received); // dummy
  563. //save to file
  564. char data[100];
  565. const char flag = 'a';
  566. FILE* fHandle = UA_NULL;
  567. if (defaultParams) {
  568. fHandle = fopen("client.log", &flag);
  569. }else{
  570. fHandle = fopen(argv[3], &flag);
  571. }
  572. //header
  573. UA_Int32 bytesToWrite = 0;//sprintf(data, "measurement %s in ms, nodesToRead %d \n", argv[3], 1);
  574. //fwrite(data,1,bytesToWrite,fHandle);
  575. for(UA_UInt32 i=0;i<tries;i++) {
  576. bytesToWrite = sprintf(data,"%16.10f \n",timeDiffs[i]);
  577. fwrite(data,1,bytesToWrite,fHandle);
  578. }
  579. fclose(fHandle);
  580. UA_String_deleteMembers(&reply);
  581. UA_Array_delete(nodesToRead,&UA_TYPES[UA_TYPES_NODEID], 1);
  582. UA_free(timeDiffs);
  583. return 0;
  584. }