Tuesday, 10 February 2009

Sometimes a function can became a class...

...and it happens after the function became too big (or complicated). Like account-management functions for GaduProtocol (registerAccount, unregisterAccount, remindPassword, changePassword). Each of these functions used getToken function and a Mode field of GaduProtocol object, so the getToken could choose right function to call after its work was done (it works asynchronically). And each of these has own slot!

Why I don't like this solution? Without true reason it adds a new state field to GaduProtocol class - Mode (Register, Unregister, RemindPassword, ChangePassword). It makes GaduProtocol do things that are not really involved with normal IM communication - account management. It uses copy-and-pasted code, becouse these methods are nearly identical.

So I've decided to promote each of this funtion to a class of its own. With, of course, one base class: GaduServerConnector (ok, the name suck, it will be changed when I found a better one). This class does most of the work: it connects to server, fetches token (captcha) image, asks user for token value (using external object for that, so method of asking can be changed) and calls abstract method performAction(tokenId, tokenValue). Classes like GaduServerRegisterAccount (this name sucks too) only implement performAction method (and should call finished(bool), so GaduServerConnector can emit a signal for higher level code).

For example (there is only one right now) GaduServerRegisterAccount implements performAction using gg_register3 libgadu function. It wraps the connection in GaduPubdirSocketNotifiers and connects to its signal: done(bool, gg_http *). So after registration if over, it receives that signal and sets its Uin field. The caller can then read GaduServerConnector::Result field (so it knows if it was successfully) and the GaduPubdirSocketNotifiers::Uin (so it knows id of the newly registered account). Voila.

It makes GaduProtocol smaller and the flow of Register/Unregister/RemindPassword/ChangePassword a lot simples (I hope so...).

BTW: it still don't work, it segfaults every time. But it is just a technical problem, not design one.

UPDATE: I've fixed the crash. Registration does not work because (as I think) we are no longer compatible with GG protocol. I'll fix that later.

Is there somebody who wants to promote last 3 functions to objects?

UPDATE 2: I've found error with registration - it was just sending 2x password instead of password and e-mail ;)

No comments:

Post a Comment