Main Page | Class Hierarchy | Class List | Directories | File List | Class Members | File Members

Connessione.cpp

Go to the documentation of this file.
00001 #include "Connessione.h"
00002 #include "ChannelGui.h"
00003 #include "QueryGui.h"
00004 #include "StatusGui.h"
00005 #include "ClientGui.h"
00006 #include "wxStringIRC.h"
00007 
00008 // Event Table - Inizio
00009 BEGIN_EVENT_TABLE(Connessione, wxEvtHandler)
00010     EVT_SOCKET(SOCKET_ID, Connessione::OnSocketEvent)
00011 END_EVENT_TABLE()
00012 // Event Table - Fine
00013 
00014 Connessione::Connessione(wxString addr, int port, int timeout, bool rfcCompliant)
00015 :serverAddr(addr), serverPort(port), serverTimeout(timeout), rfc_compliant(rfcCompliant){
00016     serverSocket = new wxSocketClient();  // Crea un nuovo Socket
00017     serverSocket->SetEventHandler(*this, SOCKET_ID);  // Imposta l'event handler
00018     serverSocket->SetNotify(wxSOCKET_INPUT_FLAG);  // Imposta gli eventi che devono
00019                                                    // essere catturati
00020     CreateStatus(_("~Status"));  // Crea una finestra di Status
00021     status = static_cast<StatusGui*>(GetTerminal(_("~Status")));
00022     wxString title = _("Status");
00023     title.Append(_(": ")).Append(addr);
00024     status->SetTitle(title);
00025     parserIn = new ParserIn(this);  // Crea il ParserIn
00026     parserOut = new ParserOut(this); // Crea il ParserIn
00027 }
00028 
00029 Connessione::~Connessione(){
00030     Disconnect();  // Chiude la connessione corrente
00031     serverSocket->Notify(FALSE);  // Disabilita la notifica degli eventi
00032     serverSocket->Discard();  // Elimina gli eventi nel buffer
00033     serverSocket->Destroy();  // Elimina il Socket
00034     wxTerminalsMap::iterator it;
00035     for(it = terminalsMap.begin(); it != terminalsMap.end(); ++it)
00036         it->second->Destroy();  // Elimina tutti i terminali associati alla connessione
00037     delete parserIn;  // Cancella il ParserIn
00038     delete parserOut;  // Cancella il ParserOut
00039 }
00040 
00041 bool Connessione::Connect(){
00042     wxIPV4address ipAddr;
00043     ipAddr.Hostname(serverAddr);
00044     ipAddr.Service(serverPort);
00045     serverSocket->Notify(TRUE);  // Imposta la notifica degli eventi
00046     serverSocket->SetTimeout(serverTimeout);  // Imposta il timeout
00047     serverSocket->Connect(ipAddr, TRUE);  // Tenta di connettersi
00048     if (!serverSocket->IsConnected())
00049     {
00050         serverSocket->Close();
00051         wxMessageBox(_("Impossibile connettersi all'host specificato!"), _("Errore!"));
00052         return false;
00053     }
00054     else{
00055         status->receiveMsg(_("Connessione effettuata! Attendere prego...\n"));
00056         status->receiveMsg(_("La connessione ad alcuni server può richiedere piu' di un minuto!\n"));
00057         return true;
00058     }
00059 }
00060 
00061 bool Connessione::Disconnect(){
00062     serverSocket->Close();  // Chiude la connessione
00063     if(serverSocket->IsDisconnected())
00064         return true;
00065     else
00066         return false;
00067 }
00068 
00069 bool Connessione::sendCommand(wxString command){
00070     wxChar* buf;
00071     size_t len;
00072     len  = (wxStrlen(command) + 1) * sizeof(wxChar);
00073     buf = new wxChar[wxStrlen(command) + 1];
00074     wxStrcpy(buf, command);
00075     serverSocket->Write(buf, len);  // Scrive sul socket il buffer
00076     serverSocket->Write(_("\n"), 1);  // Aggiunge il ritorno a capo al termine
00077                                       // del comando
00078     delete buf;  // Elimina il buffer
00079     
00080     if(!serverSocket->Error()){
00081         return true;
00082     }
00083     else {
00084         return false;
00085     }        
00086 }
00087 
00088 void Connessione::readSocket(){
00089     wxChar c;
00090     serverSocket->Read(&c, sizeof(wxChar));
00091     cmdBuffer << c;
00092     if(c == 0x0A){  // Attende un LF
00093         wxString buf = cmdBuffer;  // Inserisce in un buffer tampone
00094         cmdBuffer.clear();  // Ripulisce il buffer di ricezione
00095         buf = buf.Mid(0, (buf.Len() - 2));  // Elimina i caratteri speciali
00096         parserIn->getMessage(buf);
00097     }  
00098 }
00099 
00100 void Connessione::OnSocketEvent(wxSocketEvent& event)
00101 {
00102   switch(event.GetSocketEvent())
00103   {
00104     case wxSOCKET_INPUT      : readSocket(); break;
00105     default                  : break;
00106   }
00107 }
00108 
00109 void Connessione::CreateChannel(wxString nome)
00110 {
00111     wxString title = nome;
00112     if(rfc_compliant){  // Gestione dei caratteri speciali secondo l'RFC2812
00113         wxStringIRC nome_lower = nome;
00114         nome = nome_lower.Lower();
00115     }    
00116     else
00117         nome.MakeLower();  // Rende il nome del terminale lower case
00118     if(terminalsMap.count(nome)){  // Cerca il terminale nella hash map
00119         wxMessageBox(_("Gia' esiste un Terminale con questo nome!"), _("Errore!"));
00120         exit(1);
00121     }
00122     ClientGui* client = ClientGui::Instance(NULL);  // Ottiene il riferimento
00123                                                     // all'istanza di ClientGui
00124     terminalsMap[nome] = new ChannelGui(this, client);  // Crea un nuovo ChannelGui
00125     terminalsMap[nome]->SetName(nome);  // Imposta il nome della finestra
00126     terminalsMap[nome]->SetTitle(title);  // Imposta il titolo
00127     terminalsMap[nome]->Show(TRUE);  // Mostra il frame
00128 }
00129 
00130 void Connessione::CreateQuery(wxString nome)
00131 {
00132     wxString title = nome;
00133     if(rfc_compliant){
00134         wxStringIRC nome_lower = nome;
00135         nome = nome_lower.Lower();
00136     }    
00137     else
00138         nome.MakeLower();  // Rende il nome del terminale lower case
00139     if(terminalsMap.count(nome)){
00140         wxMessageBox(_("Gia' esiste un Terminale con questo nome!"), _("Errore!"));
00141         exit(1);
00142     }
00143     ClientGui* client = ClientGui::Instance(NULL);
00144     terminalsMap[nome] = new QueryGui(this, client);
00145     terminalsMap[nome]->SetName(nome);
00146     terminalsMap[nome]->SetTitle(title);
00147     terminalsMap[nome]->Show(TRUE);
00148 }
00149 
00150 void Connessione::CreateStatus(wxString nome)
00151 {
00152     wxString title = nome;
00153     if(rfc_compliant){
00154         wxStringIRC nome_lower = nome;
00155         nome = nome_lower.Lower();
00156     }    
00157     else
00158         nome.MakeLower();  // Rende il nome del terminale lower case
00159     if(terminalsMap.count(nome)){
00160         wxMessageBox(_("Gia' esiste un Terminale con questo nome!"), _("Errore!"));
00161         exit(1);
00162     }
00163     ClientGui* client = ClientGui::Instance(NULL);
00164     terminalsMap[nome] = new StatusGui(this, client);
00165     terminalsMap[nome]->SetName(nome);
00166     terminalsMap[nome]->SetTitle(title);
00167     terminalsMap[nome]->Show(TRUE);
00168 }
00169 
00170 bool Connessione::TerminalExists(wxString nome)
00171 {
00172     if(rfc_compliant){
00173         wxStringIRC nome_lower = nome;
00174         nome = nome_lower.Lower();
00175     }    
00176     else
00177         nome.MakeLower();  // Rende il nome del terminale lower case
00178     return terminalsMap.count(nome);  // Ritorna true se trova il terminale
00179                                       // nella hash map
00180 }
00181 
00182 void Connessione::DeleteTerminal(wxString nome)
00183 {
00184     if(rfc_compliant){
00185         wxStringIRC nome_lower = nome;
00186         nome = nome_lower.Lower();
00187     }    
00188     else
00189         nome.MakeLower();  // Rende il nome del terminale lower case
00190     if(!terminalsMap.count(nome)){  // Se il terminale non esiste mostra un errore
00191         wxMessageBox(_(nome), _("Errore!"));
00192         exit(1);
00193     }
00194     terminalsMap[nome]->Destroy();  // Involca la distruzione del terminale
00195     terminalsMap.erase(nome);  // Elimina il contenuto della mappa
00196     
00197     #ifdef TESTING
00198     if(terminalsMap.empty())
00199         wxMessageBox(_("Non ci sono più Terminali!"), _("Informazione!"));
00200     #endif
00201 }
00202 
00203 TerminalGui* Connessione::GetTerminal(wxString nome)
00204 {
00205     if(rfc_compliant){
00206         wxStringIRC nome_lower = nome;
00207         nome = nome_lower.Lower();
00208     }    
00209     else
00210         nome.MakeLower();  // Rende il nome del terminale lower case
00211     if(terminalsMap[nome] == NULL){  // Se il terminale non esiste mostra un errore
00212         wxString msg = _("Il Terminale a cui cerchi di accedere non esiste! Nome: ");
00213         msg.Append(nome);
00214         wxMessageBox(msg, _("Errore!"));
00215         exit(1);
00216     }
00217     
00218     return terminalsMap[nome];  // Ritorna un riferimento al terminale richiesto
00219 }
00220 
00221 ParserOut* Connessione::getParserOut(){
00222     return parserOut;  // Ritorna un riferimento al ParserOut
00223 }
00224 
00225 wxString Connessione::GetMyNick()
00226 {
00227     return parserIn->mynick;  // Ritorna il nick letto dal ParserIn al momento
00228                               // della connessione
00229 }
00230 
00231 StatusGui* Connessione::GetStatus()
00232 {
00233     return status;  // Ritorna il riferimento alla StatusGui
00234 }
00235 
00236 void Connessione::UserQuit(wxString name, wxString msg)
00237 {
00238     wxTerminalsMap::iterator it;
00239     // Viene usato un iteratore per scorrere la mappa di terminali
00240     for(it = terminalsMap.begin(); it != terminalsMap.end(); ++it){
00241        if(it->first.StartsWith(_("#")) || it->first.StartsWith(_("+")) ||
00242           it->first.StartsWith(_("&")) || it->first.StartsWith(_("!"))){
00243            ChannelGui* canale = static_cast<ChannelGui*>(GetTerminal(it->first));
00244            // Viene notificato al canale il QUIT dell'utente
00245            canale->receiveMsg(name+" è uscito dal server: "+msg, STYLE_BLUE);
00246            // Viene rimosso il nickname
00247            canale->removeUser(name);
00248        }   
00249     }   
00250 }
00251 
00252 void Connessione::UserChNick(wxString oldname, wxString newname)
00253 {
00254     wxTerminalsMap::iterator it;
00255     // Viene usato un iteratore per scorrere la mappa di terminali
00256     for(it = terminalsMap.begin(); it != terminalsMap.end(); ++it){
00257        if(it->first.StartsWith(_("#")) || it->first.StartsWith(_("+")) ||
00258           it->first.StartsWith(_("&")) || it->first.StartsWith(_("!"))){
00259            ChannelGui* canale = static_cast<ChannelGui*>(GetTerminal(it->first));
00260            // Viene effettuato il cambio del nickname se il nick è presente nel canale
00261            if(canale->changeNick(oldname, newname))
00262            // Viene notificato al canale il cambio di nick
00263            canale->receiveMsg(oldname+" ha cambiato nick in "+newname+"\n", STYLE_BLUE);
00264        }   
00265     }   
00266 }

Generated on Thu Dec 2 16:51:42 2004 for Quirck by  doxygen 1.3.9.1