socket.cpp

00001 
00004 /*- A Graphic Remote Control Utility for Klaus Schidinger's vdr (www.cadsoft.de/vdr)
00005 
00006                 Copyright (C)2006 Peter Marquardt p_marquardt@users.sourceforge.net
00007  */
00008 /*
00009                 This program is free software; you can redistribute it and/or modify
00010                 it under the terms of the GNU General Public License as published by
00011                 the Free Software Foundation; either version 2 of the License, or
00012                 (at your option) any later version.
00013 
00014                 This program is distributed in the hope that it will be useful,
00015                 but WITHOUT ANY WARRANTY; without even the implied warranty of
00016                 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017                 GNU General Public License for more details.
00018 
00019                 You should have received a copy of the GNU General Public License
00020                 along with this program; if not, write to the Free Software
00021                 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022 */
00023                 
00024                 
00025 #include <wx/tokenzr.h>
00026 #include "vdr_remote.h"
00027 
00028 
00029 void vdr_remoteFrame::OnSocketEvent(wxSocketEvent& event)
00030 {
00031         
00041         switch (event.GetSocketEvent())
00042         {
00043                 case wxSOCKET_LOST:
00044                         wxMessageBox( wxT( "Lost Socket connection." ), wxT( "Socket error" ), wxOK | wxICON_INFORMATION, this );
00045                         connection_is_alive = false;
00046 
00047                 
00048                         break;
00049                 case wxSOCKET_INPUT:
00050                 {
00055                         if (socket_data_needed) 
00056                         {
00057                                 socket_data_available = false;
00058                                 if (my_socket_buffer) delete[] my_socket_buffer;
00059                                 my_socket_buffer = new char[1024];
00060                                 vdr_client->Read(my_socket_buffer, 1024);
00061                                 if (vdr_client->LastCount()) 
00062                                 {       
00063                                         socket_data_available = true;
00064                                         my_socket_buffer[vdr_client->LastCount()] = '\0';
00065                                                                                 
00066                                 }
00067                         }
00068                         else if (show_socket_viewer)
00069                         {
00070                                 char temp_buffer[1024];
00071                                 vdr_client->Read(temp_buffer, 1024);
00072                                 if (vdr_client->LastCount()) 
00073                                 {
00074                                         wxString returned_data;
00075                                         temp_buffer[vdr_client->LastCount()] = '\0';
00076                                         returned_data.Append(wxString(temp_buffer, wxConvLocal)); 
00077                                         socket_viewer->AppendText(returned_data);
00078                                 }
00079                                 socket_data_available = false;
00080                         }
00081                 }
00082                         break;
00083                 case wxSOCKET_CONNECTION:
00084                         connection_is_alive = true;
00085                         break;
00086                 default:
00087                         break;
00088         }
00089 }
00090 
00091 void vdr_remoteFrame::Connect()
00092 {
00093 
00094         
00095         if (connection_is_alive && vdr_client->IsConnected()) return;
00096         if (num_connection_failures > 5)
00097         {
00098                 wxMessageBox( wxT( "Connecting failed too often, please check the preferences." ), wxT( "Socket error" ), wxOK | wxICON_INFORMATION, this );
00099                 num_connection_failures = 0;
00100                 SetStatusText(wxT("Connection failed."));
00101                 return; 
00102         }
00103 
00104         
00105         vdr_adress.Hostname(vdr_host);
00106         vdr_adress.Service( vdr_port);
00107         SetStatusText(wxT("Connecting... timeout 5 secs "));
00108         vdr_client->Connect(vdr_adress,false);
00109         vdr_client->WaitOnConnect(5);
00110         if (vdr_client->IsConnected()) 
00111         {
00112                 connection_is_alive = true;
00113                 num_connection_failures = 0;
00114         }
00115         else
00116         {
00117                 vdr_client->Close();
00118                 SetStatusText(wxT("Connection failed."));
00119                 connection_is_alive = false;
00120                 num_connection_failures++;
00121         }
00122 }
00123 void vdr_remoteFrame::Disconnect()
00124 {
00125 
00126         if ( vdr_client->IsConnected() && vdr_client->Ok()) 
00127         {
00128                 SendMessage( "QUIT");
00129                 
00130         }
00131         vdr_client->Close();
00132         connection_is_alive = false;
00133         socket_data_available = false;
00134         socket_data_needed = false;
00135         SetStatusText(wxT("Ready."));
00136 }
00137 
00138 void vdr_remoteFrame::GetDiskStat(wxCommandEvent& event)
00139 {
00140         Connect();
00141         if (!vdr_client->IsConnected()) return; //Disconnected means serious trouble we can't fix here so we step out early
00142         const char* the_msg = "stat disk";
00143         unsigned char len = (unsigned char)((strlen(the_msg) + 1) * sizeof(char));
00144         socket_data_needed = true;
00145         vdr_client->Write(the_msg,len );
00146         SetStatusText(wxT("Gettin Disk status."));
00147         vdr_client->WaitForRead(0,disk_status_timeout);
00148         if (socket_data_available ) //checking my_socket_buffer seems superfluous (sp?)  my_socket_buffer && 
00149         {
00150                 wxString returned_message,input_string;
00151                 input_string = wxString(my_socket_buffer, wxConvLocal);
00152                 if (!input_string ) return;
00153                 wxStringTokenizer tkz(input_string, wxT("\r\n"));
00154                 while ( tkz.HasMoreTokens() )
00155                 {
00156                         wxString token = tkz.GetNextToken();
00157                         wxString temp_message;
00158                         if (token.StartsWith(wxT("250 "),&temp_message) )
00159                         {
00160                                 wxStringTokenizer mtkz(temp_message, wxT(" "));
00161                                 int i = 0;
00162                                 while (mtkz.HasMoreTokens())
00163                                 {
00164                                         wxString data_entry = mtkz.GetNextToken();
00165                                         switch (i)  
00166                                         {
00167                                                 case 0:
00168                                                 {
00169                                                         long size_in_mb;
00170                                                         data_entry.ToLong(&size_in_mb);
00171                                                         data_entry = wxT("Total size: ");
00172                                                         data_entry << (size_in_mb/1024);
00173                                                         data_entry.Append(wxT(" GB\n"));
00174                                                 }
00175                                                         break;
00176                                                 case 1:
00177                                                 {
00178                                                         long size_in_mb;
00179                                                         data_entry.ToLong(&size_in_mb);
00180                                                         data_entry = wxT("Free Space: ");
00181                                                         data_entry << (size_in_mb/1024);
00182                                                         data_entry.Append(wxT(" GB\n"));
00183                                                 }
00184                                                         break;
00185                                                 case 2:
00186                                                         data_entry.Append(wxT(" used\n"));
00187                                                         break;
00188                                                 default:
00189                                                         data_entry = wxT("");
00190                                                         break;
00191                                         }
00192                                         i++;
00193                                         returned_message.Append(data_entry);
00194                                 }                                       
00195                         }
00196                 }
00197                 wxMessageBox( returned_message, wxT( "Disk status" ), wxOK | wxICON_INFORMATION, this );
00198         }
00199         socket_data_needed = false;
00200         Disconnect();
00201 }
00202 
00203 
00204 void vdr_remoteFrame::SendMessage(const char * the_msg) 
00205 {
00206 
00207         unsigned char len;
00208         bool get_data = socket_data_needed;
00209         socket_data_needed = false;
00210         len  = (unsigned char)((strlen(the_msg) + 1) * sizeof(char));
00211         if (!vdr_client->IsConnected())
00212         {
00213                 Disconnect();
00214                 Connect();
00215         }
00216         if (vdr_client->IsConnected()) 
00217         {
00218                 socket_data_needed = get_data;
00219                 vdr_client->Write(the_msg, len);
00220                 if (vdr_client->Error()) 
00221                 {
00222                         SetStatusText(wxT("Writing to Socket failed"));
00223                 }
00224                 socket_data_needed = false;
00225         }
00226 }

Generated on Sun Nov 5 22:39:02 2006 for vdr_remote by  doxygen 1.4.6