I'm stuck with the design of my first C++ application. I hope someone could take the time to give me some pointers. (I'll try to dereference them )
I want to write a console application that does the following:
-Do a query in a database depending on the incoming requests
-Send results back
I won't bother you with the implementation details; I already have the libraries for networking and querying the database. I'd rather talk about the structure of my program.
I'll explain the code I have in mind so it will be easyer to understand the problem:
-There is a class Request that has attributes 'ID' (this has to be included in the packet that is returned) and a method 'execute()'.
-There are several subclasses of Request, which add special variables to it to store specific data that this request needs. For example the class CheckPasswordRequest will add an int accountNumber and a char* Password.
-main() goes into an infinite loop that calls the function recieve(), which will return a packet if one was recieved.
-If a packet is returned from recieve(), I check what kind of request it is.
-I initialise the correct class, set it's variables (like the accountNumber and password that was read from the packet).
-I call it's execute() method.
-The execute() method is a pure virtual function and for the class CheckPasswordRequest it will create an SQL query to retrieve the password for the given accountNumber. If it matches the given password, 1 is returned, 0 otherwise.
That's as far as I got (with the design, that is). The problem I'm having is that the function that queries the database can take arbitrarily long to return. In the mean time, no packets can be processed (they'll just get queued). However, the database server is written to handle multiple queries at the same time which is especially beneficial on a multi-cpu system.
So my code must somehow be able to send a second query to the databse, while the first has not returned yet. How would you do this in C++? Do I need multithreading?
Also, how should I return the data from the query? Should the execute() function instantiate a specialised class to store the results for that specific query (some queries will return alot of data)? Would it be a good idea to store these return classes in a linked list so I can process them in the main loop (create a packet to be returned for each)?
If there is anything wrong with my design so far, please also point that out.
I understand it might be difficult to follow this post, if anything is unclear please don't hesitate to ask.