00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef __COMMUNICATIONSTREAM_H__
00019 #define __COMMUNICATIONSTREAM_H__
00020
00021 #define COMMPORT 7998
00022
00023 #define RESULT_COMMUNICATION_SEARCH_NULL_REFERENCE 0x01
00024 #define RESULT_COMMUNICATION_SEARCH_EMPTY_LIST 0x02
00025 #define RESULT_COMMUNICATION_SEARCH_NOT_FOUND 0x03
00026 #define RESULT_COMMUNICATION_SEARCH_FOUND 0x00
00027
00028 #define RESULT_COMMUNICATION_INSERT_ALREADY_EXIST 0x01
00029 #define RESULT_COMMUNICATION_INSERT_ERROR 0x02
00030 #define RESULT_COMMUNICATION_INSERT_OK 0x00
00031
00032 #define RESULT_COMMUNICATION_REMOVE_ERROR 0x02
00033 #define RESULT_COMMUNICATION_REMOVE_NOT_FOUND 0x03
00034 #define RESULT_COMMUNICATION_REMOVE_OK 0x00
00035
00036 #define RESULT_COMMUNICATION_CHANGE_ERROR 0x02
00037 #define RESULT_COMMUNICATION_CHANGE_NOT_FOUND 0x03
00038 #define RESULT_COMMUNICATION_CHANGE_OK 0x00
00039
00040 #define MSGCODE_IP 0x00
00041 #define MSGCODE_BLOCK 0x01
00042 #define MSGCODE_PID 0x02
00043 #define MSGCODE_MOVE 0x03
00044 #define MSGCODE_MODE 0x04
00045 #define MSGCODE_FLUXO 0x05
00046
00047 #define MAXLEN_MSGSTRUCT 1024
00048
00049 #include <sys/time.h>
00050 #include <sys/types.h>
00051 #include <sys/socket.h>
00052 #include <unistd.h>
00053 #include <stdio.h>
00054 #include <stdlib.h>
00055 #include <string.h>
00056 #include <string>
00057 using namespace std;
00058
00059 #include <arpa/inet.h>
00060 #include <netinet/in.h>
00061
00062 #include "RioInterfaceTypes.h"
00063 #include "RioInterface.h"
00064 #include "BufferStream.h"
00065
00066 class CRioMMVideo;
00067
00068 class PLSessionManager;
00069
00070 typedef enum
00071 {
00072 ACTION_PLAY,
00073 ACTION_STOP,
00074 ACTION_PAUSE,
00075 ACTION_FORWARD,
00076 ACTION_REWIND,
00077 ACTION_READBUFFER,
00078 ACTION_REACHLEADER
00079 } MoveAction;
00080
00081 typedef enum
00082 {
00083 JOIN_THIS,
00084 LEAVE_THIS,
00085 LEAVE_ALL,
00086 KEEP_THIS
00087 } IPAction;
00088
00089 class AddressIP
00090 {
00091 private:
00092 unsigned short port;
00093 in_addr_t address;
00094
00095 public:
00096 AddressIP( char *address = 0, unsigned short port = 0 );
00097 AddressIP( in_addr_t address = 0, unsigned short port = 0 );
00098
00099 void setPort( unsigned short port );
00100 void setAddress( char * address);
00101 void setAddress( in_addr_t address);
00102 unsigned short getPort();
00103 in_addr_t getAddress();
00104 };
00105
00106 class CommunicationItemData
00107 {
00108 private:
00109 int pid;
00110
00111
00112 int socket;
00113 ClientMode mode;
00114
00115 public:
00116
00117
00118
00119
00120 CommunicationItemData( int pid = 0, int socket = 0,
00121 ClientMode mode = PASSIVE );
00122
00123 void setPid( int pid );
00124 int getPid();
00125 void setSocket( int socket );
00126 int getSocket();
00127 void setMode( ClientMode mode );
00128 ClientMode getMode();
00129 };
00130
00131 class CommunicationItem : public CommunicationItemData
00132 {
00133 private:
00134 CommunicationItem *next;
00135
00136 public:
00137 CommunicationItem( int pid = 0, int socket = 0 );
00138 CommunicationItem( CommunicationItem *next = NULL, int pid = 0,
00139 int socket = 0 );
00140 CommunicationItem( CommunicationItem *previous = NULL,
00141 CommunicationItem *next = NULL, int pid = 0,
00142 int socket = 0 );
00143
00144 void setNext( CommunicationItem *next );
00145 CommunicationItem *getNext();
00146 };
00147
00148 class CommunicationTable
00149 {
00150 protected:
00151 CommunicationItem *firstItem;
00152 CommunicationItem *lastItem;
00153
00154 void setFirstItem( CommunicationItem *firstItem );
00155 void setLastItem( CommunicationItem *lastItem );
00156
00157 public:
00158 CommunicationTable();
00159
00160 CommunicationItem *getFirstItem();
00161 CommunicationItem *getLastItem();
00162 int insert( CommunicationItem *item );
00163 int insertNullPid( int socket );
00164 int changeSocketPid( int socket, int pid );
00165
00166
00167
00168 int removeSocket( int socket );
00169 int searchPid( int defaultsocket,
00170 CommunicationItem **current,
00171 CommunicationItem **previous );
00172 int searchSocket( int defaultsocket,
00173 CommunicationItem **current,
00174 CommunicationItem **previous );
00175
00176
00177
00178
00179
00180
00181 ClientMode searchPidForMode( int pid );
00182 };
00183
00184 class CommunicationStream
00185 {
00186 protected:
00187 CommunicationTable *table;
00188 CRioObject *object;
00189 PLSessionManager *PLSession;
00190 void *callback;
00191 BufferStream *bufferStream;
00192 CRioMMVideo *video;
00193 int init;
00194 pthread_mutex_t MessageMutex;
00195 pthread_mutex_t TableMutex;
00196
00197
00198
00199
00200
00201
00202
00203 void MessageMutexLock( string msg = "" );
00204
00205
00206
00207
00208
00209
00210 void MessageMutexUnlock( string msg = "" );
00211
00212
00213
00214
00215
00216
00217 void TableMutexLock( string msg = "" );
00218
00219
00220
00221
00222
00223
00224 void TableMutexUnlock( string msg = "" );
00225
00226 public:
00227 fd_set *readfds;
00228
00229 #if defined(RIO_DEBUG2) || defined(RIO_EMUL)
00230 int patching_number;
00231 int multicast_number;
00232 char FLUXOSLOG[ MaxPathSize ];
00233 ofstream m_log_fluxo;
00234 unsigned int calculeted_time;
00235 struct timeval time_list;
00236 struct timeval initial_time;
00237 struct timeval final_time;
00238 bool begin;
00239 pthread_mutex_t log_mutex;
00240 #endif
00241
00242 CommunicationStream( CRioObject *object = 0,
00243 PLSessionManager *PLSession = 0,
00244 void *callback = 0,
00245 BufferStream *bufferStream = 0,
00246 CRioMMVideo *video = 0,
00247 char *LogsDirectory = NULL );
00248
00249 int createSocket( AddressIP *address );
00250 int createListenSocket( unsigned short port );
00251
00252 void socketReceiveHandler();
00253 void socketReceiveMessageHandler( int sd );
00254
00255 void sendIPMsgToSocket( int sd, char *ip, unsigned short port,
00256 ClientMode mode, int block = -1 );
00257
00258 void sendPIDMsgToSocket( int sd, int myPid );
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285 void sendIPMsg( int pid, char *ip, unsigned short port,
00286 ClientMode mode, int block = -1,
00287 IPAction ipAction = KEEP_THIS, int target = -1 );
00288
00289
00290 void sendMoveMsg( int pid, MoveAction action, int block = -1 );
00291 void sendPIDMsg( int myPid );
00292
00293
00294
00295
00296
00297
00298
00299
00300 void sendModeMsg( int pid, ClientMode mode, int block = -1,
00301 int target = -1 );
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319 void burstRequest( int block );
00320
00321 #ifdef RIO_EMUL
00322 void sendFluxoMsg( int pid, int patching, int multicast );
00323 #endif
00324
00325 #if defined(RIO_DEBUG2) || defined(RIO_EMUL)
00326 int GetNPatchingStream();
00327 #endif
00328
00329 string MoveAction2String( MoveAction action );
00330 string ClientMode2String( ClientMode mode );
00331 string IPAction2String( IPAction action );
00332
00333 CommunicationTable *getTable();
00334 void setTable( CommunicationTable *table );
00335
00336 static void *startListenThread( void *param );
00337 static void *startReceiveThread( void *param );
00338
00339 CRioObject *getObject();
00340 PLSessionManager *getPLSession();
00341 };
00342
00343 class CommunicationStreamThreadParam
00344 {
00345 public:
00346 unsigned short port;
00347 CommunicationStream *stream;
00348
00349 CommunicationStreamThreadParam()
00350 {
00351 port = 0;
00352 stream = NULL;
00353 }
00354 };
00355
00356 typedef struct
00357 {
00358 unsigned char code;
00359 char address[17];
00360 unsigned short port;
00361 ClientMode mode;
00362 int block;
00363 int target;
00364 IPAction ipAction;
00365 } msgIP;
00366
00367 typedef struct
00368 {
00369 unsigned char code;
00370 unsigned int block;
00371 } msgBlock;
00372
00373 typedef struct
00374 {
00375 unsigned char code;
00376 int pid;
00377 } msgPID;
00378
00379 typedef struct
00380 {
00381 unsigned char code;
00382 int block;
00383 MoveAction action;
00384 } msgMove;
00385
00386 typedef struct
00387 {
00388 unsigned char code;
00389 ClientMode mode;
00390 int block;
00391 int target;
00392 } msgMode;
00393
00394 #if defined(RIO_DEBUG2) || defined(RIO_EMUL)
00395 typedef struct
00396 {
00397 unsigned char code;
00398 int patching;
00399 int multicast;
00400 unsigned long long int pid;
00401 } msgFluxo;
00402 #endif
00403
00404 #endif