Hello,
I need some assistance understanding how dc++ *hangs* together. I have already read the code, read the docs, read the doxygen docs, read the faq, read the posts in here, read the protocol doco so I know what it is suppose to do but i cant quite get how it hangs together. I figure the code below starts everything up but what does it actually start up?
Code excerpt from client/DCPlusPlus.cpp
LogManager::newInstance();
TimerManager::newInstance();
ShareManager::newInstance();
CryptoManager::newInstance();
SearchManager::newInstance();
ClientManager::newInstance();
ConnectionManager::newInstance();
DownloadManager::newInstance();
UploadManager::newInstance();
HubManager::newInstance();
QueueManager::newInstance();
FinishedManager::newInstance();
ADLSearchManager::newInstance();
These start a new instance of that class/object right? What does that actually mean its doing? It loads something into memory or starts a thread? How do these communicate to each other? How do they react to events?
This information would help me understand the structure of what is going on. I am familiar with C but C++ and windows programming are new to me. I keep seeing throw() statements in the code.... what does that mean? I understand most of the code but I just cant quite fathom how it all hangs together. I am nearly there... I can almost taste it... can you give me some clues to aid my understanding?
Appreciate any help...
rtfmoz
Understanding....
Moderator: Moderators
-
GargoyleMT
- DC++ Contributor
- Posts: 3212
- Joined: 2003-01-08 02:46
- Location: .pa.us
DC++ seems to be pretty darn good about inheritance, etc. If you're unfamiliar with C++, be prepared to do some browsing or reading to get up to speed.
If you look further, you'll see Listener and Speaker classes, and some classes inheriting from other *Listener classes. These, along with the fire(...) and onAction(...) functions, are actually how the most of the pieces communicate with each other.
A mini-example of this is that when a download completes, the DownloadManager fires a message that says it completed an upload. The FinishedManager, which is a listener of the DownloadManager class, gets that, and acts on the second parameter (the details of the finished download), and adds it to its list of finished downloads. It then sends a message saying it added an entry, and the FinishedFrm, which is a listener of the FinishedManager class, sees that message and adds it to the window that shows the finished downloads.
I suggest you pick some feature out of the feature requests tracker on sourceforge, and try to implement it. You'll learn a lot about the code you come into contact with, as well as furthering DC++ as project.
That's what I'm still doing!
If you look further, you'll see Listener and Speaker classes, and some classes inheriting from other *Listener classes. These, along with the fire(...) and onAction(...) functions, are actually how the most of the pieces communicate with each other.
A mini-example of this is that when a download completes, the DownloadManager fires a message that says it completed an upload. The FinishedManager, which is a listener of the DownloadManager class, gets that, and acts on the second parameter (the details of the finished download), and adds it to its list of finished downloads. It then sends a message saying it added an entry, and the FinishedFrm, which is a listener of the FinishedManager class, sees that message and adds it to the window that shows the finished downloads.
I suggest you pick some feature out of the feature requests tracker on sourceforge, and try to implement it. You'll learn a lot about the code you come into contact with, as well as furthering DC++ as project.
-
sarf
- Posts: 382
- Joined: 2003-01-24 10:43
- Location: Sweden
Re: Understanding....
rtfmoz wrote:[snip new instances]
These start a new instance of that class/object right? What does that actually mean its doing? It loads something into memory or starts a thread? How do these communicate to each other? How do they react to events?
Each of these instantiates an object (creates it). As each of the classes instantiate are a Singleton (i.e. only one instance (object) of the class exists at any given time) some of them start running their own threads... but most just listen to events. GargoyleMT explained those.
rtfmoz wrote:This information would help me understand the structure of what is going on. I am familiar with C but C++ and windows programming are new to me. I keep seeing throw() statements in the code.... what does that mean?
try, catch (as well as finally) and throw are part of a new shiny thing in the C++ world - exceptions. An exception is a way for a method to say "OK, this stinks to high heaven and someone needs to take note of it".
For example, in many object-oriented languages, trying to access a reference to a structure/object results in a NullPointerException being throw (example is taken from Java). This gives the coder a way to say "Well, if the data becomes null I can pop up a window telling the user that something has happened and shut down the app in a good way" or "Heck, who cares if the Version data got shafted? Let's press on!".
Depending on how serious an exception is (it can range from the dread NullPointerException to the rather mild IndexOutOfBoundsException) you can, as a programmer either a) ignore/"swallow" it (bad idea), b) handle it (good idea) or c) not handle it (often a good idea, and done by throwing it to the next fellow on the try...catch ladder).
The way exceptions are handled is this (well, somewhat) :
Imagine that the whole program is surrounded by a gigantic try block. If the program throws an exception (it is not handled by a try...catch statement on a lower level of the program) the program will terminate with the dreaded "Unhandled Exception" error message.
When you create a try...catch statement you put the code you wish to "try" in the try part and the exception you wish to catch in the catch part. Neat, huh? But wait, there's more (Ginsu knives and nail polish not included)! You can catch several exceptions after one another, and an exception is a class which can inherit from other classes, and... perhaps an example is in order:
Code: Select all
bool sendStuff()
{
Socket* s = NULL;
try
{
s = new Socket();
s->connect("localhost", 31337);
s->send(buffer, bufferSize, 0);
try
{
s->close();
}
catch(...)
{
// using ... means catch *everything*.
// this is not a good way to do things, but since some languages
// (<cough> Java <cough>) forces you to catch everything but
// runtime exceptions, getting used to catching stuff around
// close() methods will be good for you :)
// here we do nothing... we don't care if the close() methods fails
// horribly since we only do it to be nice.
}
}
catch(SocketConnectionException ex)
{
// does not really exist buf if it would have, it would handle the
// exceptions that may happen when you try to connect to something
return false;
}
catch(SocketException ex)
{
// socketexception is thrown when something goes wrong with the socket
// the "SocketConnectionException" has inherited from SocketException
// so we could handle it here, but since we're being very OO in this
// example, we'll assume we've connected but something went wrong with
// the send() method
try
{
s->close();
}
catch(...)
{
// yada yada yada
}
return false;
}
catch(IOException ex)
{
// IOException is thrown when something goes wrong with the I/O.
// Duh. :)
// I can't think of anything to do here, but sockets are evil,
// mean things that like to throw exceptions faster than you
// can write "NotEnoughMemoryException".
try
{
s->close();
}
catch(...)
{
// here we do nothing... as usual
}
return false;
}
catch(Exception ex)
{
// Whoops! An Exception has been thrown (and we've handled all of
// its family that we care too... Ouch.
// This means that something is seriously out of whack...
// mayhap a NullPointerException or something equally bad.
// We'll not handle this; that's for methods with more cunning
// than us
throw ex;
}
return true;
}For the inquiring mind, this code should do the same, and with less lines of code (but it is only guaranteed to work on Java 1.3) :
Code: Select all
bool sendStuff()
{
Socket* s = NULL;
try
{
s = new Socket();
s->connect("localhost", 31337);
s->send(buffer, bufferSize, 0);
}
catch(SocketConnectionException ex)
{
// yada yada... see above code
return false;
}
catch(SocketException ex)
{
// *yawn*
return false;
}
catch(IOException ex)
{
// "zzzzzzzz"
return false;
}
catch(Exception ex)
{
// "<BONG> Your computer has experienced a serious error, and will now reboot you."
throw ex;
}
finally
{
try
{
s->close();
}
catch(...)
{
// here we do nothing... as usual
}
}
return true;
}rtfmoz wrote:I understand most of the code but I just cant quite fathom how it all hangs together. I am nearly there... I can almost taste it... can you give me some clues to aid my understanding?
Hope this helps.
Sarf
---
A man's a man all his life; a woman's sexy until she's your wife!
-
sarf
- Posts: 382
- Joined: 2003-01-24 10:43
- Location: Sweden
Not a problem. I never mix topics (well, only sometimes) and I sometimes let my temper get the best of me, too.
If you have any further questions about (D)C++, just post it here - I check all topics on the board that are updated about twice daily, so I'll get back to you ASAP.
Sarf
---
Despite the high cost of living, it remains a popular item.
If you have any further questions about (D)C++, just post it here - I check all topics on the board that are updated about twice daily, so I'll get back to you ASAP.
Sarf
---
Despite the high cost of living, it remains a popular item.
-
GargoyleMT
- DC++ Contributor
- Posts: 3212
- Joined: 2003-01-08 02:46
- Location: .pa.us
rtfmoz wrote:Thank you for the advice. It is really appreciated and I mean it. I am a bit of a hot head so if I go off from time to time please excuse me. (in other forums)
Not a problem here either. I've noticed that I tend to hop on new ideas and figure out what's wrong with them instead of what's right; this is how I handle my own feature ideas too. You have some good ideas, and I'd like to see them in code, even if they aren't the way I'd like them done - it doesn't matter, since they'd be your creations.
Besides, expanding someone's understanding (including my own) about the code is something that has an immediate payoff.
Who is online
Users browsing this forum: Google [Bot] and 0 guests