miniupnp.tuxfamily.org Forum Index miniupnp.tuxfamily.org
The forum about miniupnp and libnatpmp
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Simple Source Files Package for Qt Creator

 
Post new topic   Reply to topic    miniupnp.tuxfamily.org Forum Index -> miniupnpc Compilation/Installation
View previous topic :: View next topic  
Author Message
gyll



Joined: 29 Oct 2014
Posts: 2

PostPosted: Wed Oct 29, 2014 11:58 am    Post subject: Simple Source Files Package for Qt Creator Reply with quote

Can somebody create a simple source files package containing all the miniupnpc {cpp/h} files which i can just add to a Qt Creator project, PLUS a wrapper function over the whole thing with EXACTLY the interface which is provided by the command line executable? This would be of an IMMENSE HELP for people (like me) who don't have the time/knowledge to figure out how to compile miniupnpc and use all the detailed interfaces that miniupnpc provides.

Just to make it as clear as i can: i'd like to just grab the miniupnpc files, add them to my project in Qt Creator, and then have a unique function that i can use e.g. "int miniupnpc_cmdline(char *command_string, char *stdout_buffer, char* stderr_buffer)" which will provide me with the miniupnpc command line set of commands (or, why not, with an extended set), and which would return the command's result.

PS
I tried to make such a function myself but didn't make it (apparently i'm not as smart as i thought i was), so i eventually wound up creating an object which simply invokes the windows executable, but the problem with this approach, apart from its lack in implementation elegance, is that it also lacks elegance at execution, i.e. it pops up that console window at every miniupnpc operation, which obviously looks pretty nasty and totally unprofessional...
Back to top
View user's profile Send private message
miniupnp
Site Admin


Joined: 14 Apr 2007
Posts: 1445

PostPosted: Thu Oct 30, 2014 8:46 am    Post subject: Reply with quote

you should have a look at functions in upnpcommands.h and miniupnpc.h
Code:

int error = 0;
struct UPNPDev * devlist = 0;
struct UPNPDev * device;
struct UPNPUrls urls;
struct IGDdatas data;
int r;

devlist = upnpDiscover(2000, NULL/*multicastif*/, NULL/*minissdpdpath*/,
                                0/*sameport*/, 0/*ipv6*/, &error)
if(devlist) {
    r = UPNP_GetValidIGD(devlist, &urls, &data, NULL, 0);
    if(r == 1) {
       // valid IGD !
       // run commands from upnpcommands.h
       //example:
       r = UPNP_AddPortMapping(urls.controlURL, data.first.servicetype, 12345, 12345, "192.168.1.123", "my port mapping", "TCP", 0, 0);
       if(r != UPNPCOMMAND_SUCCESS) {
          // error calling AddPortMapping
       }
    } else {
       // not valid IGD
    }
    FreeUPNPUrls(&urls);
    freeUPNPDevlist(devlist);
} else {
// no device found
}

_________________
Main miniUPnP author.
http://miniupnp.tuxfamily.org/
Back to top
View user's profile Send private message Visit poster's website
gyll



Joined: 29 Oct 2014
Posts: 2

PostPosted: Thu Oct 30, 2014 2:25 pm    Post subject: Reply with quote

First of all, thanks a lot for the reply, you have been very kind to take your time and actually show me a piece of sample code that i can use.

However, when i untarred the tarball, added all the c and h files to a Qt Creator project on windows, and hit "build", it did not build, it came up with various errors.

In any case, I now realize that i will most likely never use miniupnpc (except for testing purposes) because it is obviously not intended to be easily portable with proper encapsulation of the low-level functions so that i can re-implement them for multiple platforms (e.g. i was expecting to find in the source code a "miniupnpc::TcpClientSocket" object with a minimalistic API that i can re-implement easily on any platform that supports TCP connections, etc), so i will probably have to start writing my own upnp client.

For example, i am now writing a cross-platform library with networking, multi-threading, and file system modules, which provides objects for each task, and it allows users to easily re-implement the interfaces for their target platforms. I am using the low-level functionality provided by the Qt framework in my implementation (which by itself makes it portable on win/osx/lin/android/ios/winphone), but users can write their own implementations if they wish to.
E.g. my filesystem module declares a "Sandbox" class with a minimalistic API like this:

Code:

namespace POSIF_Lib_Implementation {class Sandbox;}

namespace POSIF_Lib {

std::string getUserDataDirPath();
std::string getUserTempDirPath();
std::string getWorkingDirPath();

class Sandbox { // operations are allowed only inside the Sanbox' path
   POSIF_Lib_Implementation::Sandbox* sandbox;
public:
   Sandbox() = delete;
   Sandbox(std::string absolutePath);
   ~Sandbox();
   std::string path();
   bool MakeDir(std::string dirName);
   bool isDir(std::string dirName);
   bool RenameDir(std::string oldFileName, std::string newFileName);
   bool DeleteDir(std::string dirName, int forceDelete=0); // must pass DELNONEMPTYDIR in order to [recursively] delete a non-empty directory
   bool ReadDir(std::string dirName, std::set<std::string>& files);
   bool isFile(std::string fileName);
   bool ReadFile(std::string fileName, std::string& data, int* errc=nullptr);
   bool ReadFileRecord(std::string fileName, std::string& data, int recordOffset, int recordSize, int* errc=nullptr);
   bool WriteFile(std::string fileName, const std::string& data, int* errc=nullptr);
   bool WriteFileRecord(std::string fileName, const std::string& data, int recordOffset, int recordSize, int* errc=nullptr);
   bool WriteFileAppend(std::string fileName, const std::string& data, int* errc=nullptr);
   bool RenameFile(std::string oldFileName, std::string newFileName);
   bool DeleteFile(std::string fileName);
};
}


Again, thank you very much for your prompt reply!

PS
A wild idea: since you have become an expert in upnp, maybe you can write a upnp client on top of Qt (i.e. using Qt's sockets etc, such that is is natively cross-platform on all platforms supported by Qt)? This would be really something.
Back to top
View user's profile Send private message
miniupnp
Site Admin


Joined: 14 Apr 2007
Posts: 1445

PostPosted: Thu Oct 30, 2014 7:07 pm    Post subject: Reply with quote

Quote:
However, when i untarred the tarball, added all the c and h files to a Qt Creator project on windows, and hit "build", it did not build, it came up with various errors.

It should build under linux. Maybe you included too much .c files, like the ones for test executables or Python module.
Files for miniupnpc library are :
LIBOBJS = miniwget.o minixml.o igd_desc_parse.o minisoap.o \
miniupnpc.o upnpreplyparse.o upnpcommands.o upnperrors.o \
connecthostport.o portlistingparse.o receivedata.o

However it may be easier to use the compiled library. see http://mayaposch.wordpress.com/2011/09/18/setting-up-miniupnpc-with-qt/

Quote:
In any case, I now realize that i will most likely never use miniupnpc (except for testing purposes) because it is obviously not intended to be easily portable with proper encapsulation of the low-level functions so that i can re-implement them for multiple platforms (e.g. i was expecting to find in the source code a "miniupnpc::TcpClientSocket" object with a minimalistic API that i can re-implement easily on any platform that supports TCP connections, etc), so i will probably have to start writing my own upnp client.

miniupnpc is ANSI C, not C++ !
However, it is always possible to call C function from C++
You can do a C++ object to wrap call to the C functions.

I have no time to reimplement miniupnpc for Qt (using Qt sockets, etc.)
I had the project to do an "asynchronous" version using libevent, but never had time to complete this...
_________________
Main miniUPnP author.
http://miniupnp.tuxfamily.org/
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    miniupnp.tuxfamily.org Forum Index -> miniupnpc Compilation/Installation All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP
© 2007 Thomas Bernard, author of MiniUPNP.