libmsn 4.2
/home/salem/libmsn/tags/libmsn-4.2/msn/switchboardserver.h
00001 #ifndef __msn_switchboardserver_h__
00002 #define __msn_switchboardserver_h__
00003 
00004 /*
00005  * switchboardserver.h
00006  * libmsn
00007  *
00008  * Created by Mark Rowe on Mon Mar 22 2004.
00009  * Refactored by Tiago Salem Herrmann on 08/2007.
00010  * Copyright (c) 2004 Mark Rowe. All rights reserved.
00011  * Copyright (c) 2007 Tiago Salem Herrmann. All rights reserved
00012  *
00013  * This program is free software; you can redistribute it and/or modify
00014  * it under the terms of the GNU General Public License as published by
00015  * the Free Software Foundation; either version 2 of the License, or
00016  * (at your option) any later version.
00017  *
00018  * This program is distributed in the hope that it will be useful,
00019  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00020  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00021  * GNU General Public License for more details.
00022  *
00023  * You should have received a copy of the GNU General Public License
00024  * along with this program; if not, write to the Free Software
00025  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00026  */
00027 
00028 #include <msn/message.h>
00029 #include <msn/authdata.h>
00030 #include <msn/connection.h>
00031 #include <msn/passport.h>
00032 #include <msn/p2p.h>
00033 #include <string>
00034 #include <cassert>
00035 
00036 #include "libmsn_export.h"
00037 
00038 namespace MSN
00039 {
00040     class NotificationServerConnection;
00041     class FileTransferConnectionP2P;
00042 
00045     class LIBMSN_EXPORT SwitchboardServerConnection : public Connection
00046     {
00047 private:
00048         typedef void (SwitchboardServerConnection::*SwitchboardServerCallback)(std::vector<std::string> & args, int trid, void *);
00049         typedef void (SwitchboardServerConnection::*SwitchboardServerCallback2)(std::vector<std::string> & args, int trid, unsigned int sessionID);
00050 
00051         typedef struct
00052         {
00053             int chunks;
00054             int receivedChunks;
00055             std::string mime;
00056             std::string body;
00057         } MultiPacketSession;
00058 
00059 public:
00060         class AuthData : public ::MSN::AuthData
00061         {
00062 public:
00063             std::string sessionID;
00064             bool direct_connection;
00065             std::string cookie;
00066             const void *tag;
00067             
00068             AuthData(Passport & username_, const std::string & sessionID_, 
00069                      const std::string & cookie_, const void *tag_=NULL) : 
00070                 ::MSN::AuthData(username_), sessionID(sessionID_), cookie(cookie_), tag(tag_) {};
00071             
00072             AuthData(Passport & username_, const void *tag_=NULL) :
00073                 ::MSN::AuthData(username_), sessionID(""), cookie(""), tag(tag_) {};        
00074         };
00075         
00076         SwitchboardServerConnection::AuthData auth;
00077         
00080         std::list<Passport> users;
00081 
00082         P2P p2p;
00083 
00084         SwitchboardServerConnection(AuthData & auth_, NotificationServerConnection &);
00085         virtual ~SwitchboardServerConnection();
00086         virtual void dispatchCommand(std::vector<std::string> & args);
00087         
00096         Connection *connectionWithSocket(void *sock);
00097         
00098         std::map<std::string, MultiPacketSession> MultiPacketSessions;
00099 
00102         void addFileTransferConnectionP2P(FileTransferConnectionP2P *);
00103 
00106         void removeFileTransferConnectionP2P(FileTransferConnectionP2P *);
00107 
00110         void sendTypingNotification();
00111 
00114         void sendAction(std::string action);
00115 
00118         void sendVoiceClip(std::string msnobject);
00119 
00122         void sendWink(std::string msnobject);
00123 
00126         void sendInk(std::string image);
00127 
00130         void sendEmoticon(std::string alias, std::string file);
00131 
00134         void sendNudge();
00135         
00138         void sendKeepAlive();
00139 
00142         void sendFile(MSN::fileTransferInvite ft);
00143 
00146         void inviteUser(Passport userName);
00147 
00150         void fileTransferResponse(unsigned int sessionID, std::string filename, bool response);
00151 
00154         void cancelFileTransfer(unsigned int sessionID);
00155 
00156         virtual void connect(const std::string & hostname, unsigned int port);
00157         virtual void disconnect();
00158 
00161         virtual int sendMessage(const Message *msg);
00162 
00165         virtual int sendMessage(const std::string & s);
00166 
00170         virtual void addCallback(SwitchboardServerCallback, int trid, void *data);
00171 
00172         // callback of msg acks
00173         virtual void addP2PCallback(SwitchboardServerCallback2, int trid, unsigned int sessionID);
00174 
00177         virtual void removeCallback(int trid);
00178 
00179         virtual void removeP2PCallback(int trid);
00180         
00181         virtual void socketConnectionCompleted();
00182         
00183         enum SwitchboardServerState
00184         {
00185             SB_DISCONNECTED,
00186             SB_CONNECTING,
00187             SB_CONNECTED,            
00188             SB_WAITING_FOR_USERS,
00189             SB_READY
00190         };
00191         
00192         SwitchboardServerState connectionState() const { return this->_connectionState; };
00193         virtual NotificationServerConnection *myNotificationServer() { return &notificationServer; };
00194         void callback_continueTransfer(std::vector<std::string> & args, int trid, unsigned int sessionID);
00195 
00198         void requestEmoticon(unsigned int id, std::string filename, std::string msnobject, std::string alias);
00199 
00202         void requestVoiceClip(unsigned int id, std::string filename, std::string msnobject);
00203 
00206         void requestWink(unsigned int id, std::string filename, std::string msnobject);
00207 
00210         void requestDisplayPicture(unsigned int id, std::string filename, std::string msnobject);
00211 protected:
00212         virtual void handleIncomingData();
00213         SwitchboardServerState _connectionState;
00214 
00215         void setConnectionState(SwitchboardServerState s) { this->_connectionState = s; };
00216         void assertConnectionStateIs(SwitchboardServerState s) { assert(this->_connectionState == s); };
00217         void assertConnectionStateIsNot(SwitchboardServerState s) { assert(this->_connectionState != s); };
00218         void assertConnectionStateIsAtLeast(SwitchboardServerState s) { assert(this->_connectionState >= s); };
00219 private:
00220         NotificationServerConnection & notificationServer;
00221         std::list<FileTransferConnectionP2P *> _fileTransferConnectionsP2P;
00222 
00223         std::map<int, std::pair<SwitchboardServerCallback, void *> > callbacks;
00224         std::map<int, std::pair<SwitchboardServerCallback2, unsigned int> > callbacks2;
00225         
00226         static std::map<std::string, void (SwitchboardServerConnection::*)(std::vector<std::string> &)> commandHandlers;
00227         static std::map<std::string, void (SwitchboardServerConnection::*)(std::vector<std::string> &, std::string, std::string)> messageHandlers;
00228         void registerCommandHandlers(); 
00229         void registerMessageHandlers(); 
00230         void handle_BYE(std::vector<std::string> & args);
00231         void handle_JOI(std::vector<std::string> & args);
00232         void handle_NAK(std::vector<std::string> & args);
00233         void handle_MSG(std::vector<std::string> & args);
00234         
00235         void callback_InviteUsers(std::vector<std::string> & args, int trid, void * data);
00236         void callback_AnsweredCall(std::vector<std::string> & args, int trid, void * data);
00237         void callback_messageACK(std::vector<std::string> & args, int trid, void * data);
00238         
00239         void handleInvite(Passport from, const std::string & friendly, const std::string & mime, const std::string & body);
00240         void handleNewInvite(Passport & from, const std::string & friendly, const std::string & mime, const std::string & body);
00241         void message_plain(std::vector<std::string> & args, std::string mime, std::string body);
00242         void message_invitation(std::vector<std::string> & args, std::string mime, std::string body);
00243         void message_typing_user(std::vector<std::string> & args, std::string mime, std::string body);
00244         void message_p2p(std::vector<std::string> & args, std::string mime, std::string body);
00245         void message_datacast(std::vector<std::string> & args, std::string mime, std::string body);
00246         void message_emoticon(std::vector<std::string> & args, std::string mime, std::string body);
00247 public:
00248         void message_ink(std::vector<std::string> & args, std::string mime, std::string body);
00249 
00250         friend class Connection;
00251     };
00252 }
00253 #endif
 All Classes Namespaces Functions Variables Enumerations Enumerator