question

Yue Y avatar image
0 Likes"
Yue Y asked Phil BoBo edited

How does applicationcommand("downloadhttprequest") work?

From this post https://answers.flexsim.com/questions/67068/how-to-make-asynchronous-call-to-web-service.html, I know this command: applicationcommand("downloadhttprequest", verb, server, object, data, silent, filePath, callbackNode) is asynchronous and the callbackNode will be executed after the request has complete. Could someone help me understand how does this function work under the hood? Since FlexSim is single threaded, I assume this function is executed in a separate thread and once it finishes, it will push the callbackNode on top of FlexSim's event queue and get scheduled next. However, in this post https://answers.flexsim.com/questions/67556/is-flexsim-thread-safe.html it says FlexSim's event loop is not thread safe, then how is the callbackNode safely added to the event queue?

Also, not sure if it's related, in the cpp code provided in this post:https://answers.flexsim.com/questions/67556/is-flexsim-thread-safe.html

There is a line:

PostMessage(window, FLEXSIM_MESSAGE_USER_NODEFUNCTION, (WPARAM)registernode, NULL)

Does this line force the registernode to be executed immediately? If so, how does this line interact with FlexSim event loop? Thank you!

FlexSim 19.1.0
thread
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

1 Answer

·
Phil BoBo avatar image
0 Likes"
Phil BoBo answered Phil BoBo edited

applicationcommand("downloadhttprequest") passes the flag WINHTTP_FLAG_ASYNC to the WinHttpOpen function. Then it passes a callback to the WinHttpSetStatusCallback function. That callback uses PostMessage() to execute the passed treenode callbackNode on FlexSim's main thread, and the http communications happen asynchronously on a separate thread.

PostMessage(window, FLEXSIM_MESSAGE_USER_NODEFUNCTION, (WPARAM)registernode, NULL) posts a message to FlexSim's main window message loop. It doesn't execute immediately (you use nodefunction() directly to execute immediately); FlexSim's processing of its Windows messages will execute that node function when it executes that Windows message. A posted window message will happen soon, but not immediately. That line doesn't interact with FlexSim's simulation event loop; it interacts with FlexSim's windows message loop.

· 6
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Yue Y avatar image Yue Y commented ·

For applicationcommand("downloadhttprequest"), how does FlexSim main thread know the action is complete and it's time to execute the callback? The way I understand FlexSim is that there is a event loop executing event in a queue in the order of time. Since it is single thread, how does the callback function gets in the middle of the loop? Does FlexSim frequently checking a flag or does the call back function pushed to FlexSim event queue?

0 Likes 0 ·
Phil BoBo avatar image Phil BoBo ♦♦ Yue Y commented ·

FlexSim doesn't have to do anything special to handle it. It is a Windows API command called from a Windows API application. Windows handles it.

If you want to know how it works, contact Microsoft. You don't need to know how the Windows API handles it behind the scenes in order to use it.

0 Likes 0 ·
Phil BoBo avatar image Phil BoBo ♦♦ Phil BoBo ♦♦ commented ·

Re-reading your question, I need to clarify:

The callback passed to WinHttpOpen is a C++ function defined in the FlexSim engine.

That C++ callback function uses PostMessage() to execute the callback treenode function on the main thread.

The execution of the treenode callback is handled by FlexSim; the execution of the C++ callback is handled by the Windows API.

1 Like 1 ·
Yue Y avatar image Yue Y commented ·

When will FlexSim processes its Windows messages?

0 Likes 0 ·
Phil BoBo avatar image Phil BoBo ♦♦ Yue Y commented ·

FlexSim is always processing its Windows messages. It is literally a while(1) loop. See the example in Windows message loop.

0 Likes 0 ·
Yue Y avatar image Yue Y commented ·

Many thanks for your help!

0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.