XBMCMove 1.0
Controlar o XBMC com gestos

Socket.cpp

Go to the documentation of this file.
00001 #include "Socket.h"
00002 
00003 Socket::Socket() {
00004     msg = 99;
00005     linha = NULL;
00006     tipo = n = 0;
00007 }
00008 
00009 Socket::~Socket() {
00010 
00011 }
00012 
00013 int Socket::conectar(int porta) {
00014 
00015     this->fechar = false;
00016 
00017     memset((char*) &sad, 0, sizeof (sad)); /* limpa estrutura sockaddr */
00018     sad.sin_family = AF_INET; /* configura famíjlia para Internet */
00019 
00020     /* Verifica argumento de linha de comando para porta de protocolo e extrai  */
00021     /* número de porta se é especificado. Caso contrário, usa o valor de        */
00022     /* porta default dado pela constante PROTOPORT                              */
00023 
00024 
00025     port = porta; /* usa o número de porta default */
00026     if (port > 0) /* teste se o valor é legal      */
00027         sad.sin_port = htons((u_short) port);
00028 
00029     //strcpy(host,"127.0.0.1");
00030 
00031     /* Converte nome de host para endereço IP equivalente e copia para sad   */
00032 
00033     if (inet_pton(AF_INET, "127.0.0.1", &sad.sin_addr) <= 0) {
00034         fprintf(stderr, "endereço de host inválido \n");
00035         msg = FALHOU;
00036     }
00037 
00038     /* Cria um socket */
00039     sd = socket(PF_INET, SOCK_STREAM, 0);
00040 
00041     if (sd < 0) {
00042         fprintf(stderr, "a criação de socket falhou \n");
00043         msg = FALHOU;
00044     }
00045 
00046     /* Conecta o socket ao servidor especificado */
00047     if (connect(sd, (struct sockaddr *) &sad, (socklen_t) sizeof (sad)) < 0) {
00048         //printf("connect falhou\n");
00049         msg = FALHOU;
00050     } else {
00051         //printf("Conectado!\n");
00052         msg = CONECTADO;
00053     }
00054 
00055     return msg;
00056 }
00057 
00058 int Socket::getMsg() {
00059     return msg;
00060 }
00061 
00062 int Socket::getTipo() {
00063     return tipo;
00064 }
00065 
00066 void Socket::setFechar(bool fechar) {
00067     this->fechar = fechar;
00068 }
00069 
00070 bool Socket::getFechar() {
00071     return this->fechar;
00072 }
00073 
00074 void Socket::run() {
00075 
00076     linha = NULL;
00077     n = recv(sd, buf, sizeof (buf), 0);
00078 
00079     if (n <= 0) {
00080         this->fechar = true;
00081     }
00082 
00083     while (!this->fechar) {
00084 
00085         n = recv(sd, buf, sizeof (buf), 0);
00086 
00087         if (n <= 0) {
00088             this->fechar = true;
00089         }
00090         
00091         //printf("###############################\n%s\n###########################\n",buf);
00092         linha = strstr(buf,"rid\":1,\"speed\":1},\"title\":\"\"},\"sender\":\"xbmc\"}}ms\":{\"data\":{\"item\":{\"type\":\"movie\"},\"player\":{\"playe");
00093         
00094         if(linha != NULL){
00095             msg = PLAY;
00096         }
00097         // Pegar tipo de conteudo que esta sendo reproduzido:
00098 
00099         linha = strstr(buf, "OnSpeedChanged");
00100         if (linha != NULL) { //Se velocidade da midia foi alterada
00101             linha = strstr(buf, "\"speed\":1");
00102             if (linha != NULL) {
00103                 linha = strstr(buf, "movie");
00104                 if (linha == NULL) { // Se não for Video
00105                     linha = strstr(buf, "song");
00106                     if (linha != NULL) { //Se for Musica
00107                         tipo = MUSICA;
00108                         //printf("Reproduzindo Musicas!\n");
00109                     }
00110                 } else {
00111                     tipo = FILME;
00112                     //printf("Reproduzindo Videos normalmente!\n");
00113                 }
00114             } else {
00115                 tipo = SPEED;
00116             }
00117         } else {
00118             linha = strstr(buf, "movie");
00119             if (linha == NULL) { // Se não for Video
00120                 linha = strstr(buf, "song");
00121                 if (linha != NULL) { //Se for Musica
00122                     tipo = MUSICA;
00123                     //printf("Reproduzindo Musicas!\n");
00124                 }
00125             } else {
00126                 tipo = FILME;
00127                 //printf("Reproduzindo Videos!\n");
00128             }
00129         }
00130 
00131         // Pegar estado do XBMC ( Se esta executando, pausado, parado...):
00132         bool backg = true;
00133 
00134         linha = strstr(buf, "OnPlay");
00135 
00136         if (linha != NULL) {
00137             msg = PLAY;
00138             backg = false;
00139         } else {
00140             linha = strstr(buf, "OnPause");
00141             if (linha != NULL) {
00142                 msg = PAUSE;
00143                 backg = false;
00144             } else {
00145                 linha = strstr(buf, "OnStop");
00146                 if (linha != NULL) {
00147                     msg = STOP;
00148                     tipo = MENU;
00149                     backg = false;
00150                 } else {
00151                     linha = strstr(buf, "OnQuit");
00152                     if (linha != NULL) {
00153                         msg = CLOSE;
00154                         backg = false;
00155                         this->fechar = true;
00156                     }
00157                 }
00158             }
00159         }
00160 
00161         // Compatibilidade com versão anterior do XBMC:
00162 
00163         if (backg) {
00164 
00165             linha = strstr(buf, "PlaybackPau");
00166 
00167             if (linha == NULL) {
00168 
00169                 linha = strstr(buf, "PlaybackRes");
00170 
00171                 if (linha != NULL) {
00172 
00173                     msg = RESUME;
00174 
00175                 } else {
00176 
00177                     linha = strstr(buf, "PlaybackSta");
00178 
00179                     if (linha != NULL) {
00180 
00181                         msg = PLAY;
00182 
00183                     } else {
00184 
00185                         linha = strstr(buf, "PlaybackSto");
00186 
00187                         if (linha != NULL) {
00188 
00189                             msg = STOP;
00190 
00191                         } else {
00192 
00193                             linha = strstr(buf, "ApplicationSt");
00194 
00195                             if (linha != NULL) {
00196 
00197                                 msg = CLOSE;
00198                                 this->fechar = true;
00199 
00200                             } else {
00201 
00202                                 linha = strstr(buf, "PlaybackEnd");
00203 
00204                                 if (linha != NULL) {
00205 
00206                                     msg = END;
00207 
00208                                 }
00209                             }
00210                         }
00211                     }
00212                 }
00213             } else {
00214                 msg = PAUSE;
00215             }
00216 
00217         } //FIM if (backg)
00218 
00219         linha = NULL;
00220     }
00221 
00222     msg = CLOSE;
00223     /* Feche o socket */
00224     close(sd);
00225 
00226     /* Termine o programa de maneira adequada */
00227     //return 0;
00228 }
 All Classes Files Functions Variables Defines