00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef __AWHANDLER_MCTXRX__
00022 #define __AWHANDLER_MCTXRX__
00023
00024 #include "bitvector.h"
00025
00026
00027
00028
00029 #define AW_MCTXRX_DEFAULT_MAX_TX_RATE 80
00030
00031
00032 #define AW_MCTXRX_DEFAULT_MCAST_GROUP "224.0.0.1"
00033 #define AW_MCTXRX_DEFAULT_MCAST_PORT 1800
00034 #define AW_MCTXRX_DEFAULT_RETX_PORT 1801
00035
00036
00037 #define AW_MCTX_DEFAULT_RETX_TIMEOUT (3*AW_TIMESTAMP_PER_SEC)
00038 #define AW_MCRX_DEFAULT_RETX_TIMEOUT (1*AW_TIMESTAMP_PER_SEC)
00039
00040
00041 #define AW_MCTXRX_MSG_VERSION 1
00042
00043 #define AW_MCTXRX_MAX_PATHLEN 256
00044
00045
00046
00047
00048 typedef struct {
00049
00050
00051 u_int16_t
00052 version;
00053
00054
00055 u_int32_t
00056 xtnid;
00057
00058
00059 int32_t
00060 blockid;
00061
00062
00063 u_int16_t
00064 payload_size;
00065
00066
00067 void
00068 *payload;
00069
00070 } aw_mctxrx_msghdr_t;
00071
00072
00073 typedef struct {
00074
00075
00076 byte_t
00077 filename[AW_MCTXRX_MAX_PATHLEN];
00078
00079
00080 u_int64_t
00081 filesize;
00082
00083
00084 u_int32_t
00085 nblocks;
00086
00087
00088 u_int16_t
00089 blocksize;
00090
00091
00092 u_int16_t
00093 retxport;
00094
00095 } aw_mctxrx_info_t;
00096
00097
00098
00099
00100 #define AW_MCTXRX_MAX_PAYLOAD_SIZE ( 65000 - sizeof(aw_mctxrx_msghdr_t) - 8 )
00101 #define AW_MCTXRX_DEFAULT_PAYLOAD_SIZE 27500
00102
00103
00104 inline static u_int32_t mctxrx_create_infomsg(u_int32_t xtnid, aw_mctxrx_info_t *info, byte_t *msg)
00105 {
00106
00107
00108 {
00109 *((u_int16_t *)msg) = htons(AW_MCTXRX_MSG_VERSION);
00110 }
00111
00112
00113 {
00114 *((u_int32_t *)(msg + 2)) = htonl(xtnid);
00115 }
00116
00117
00118 {
00119 *((u_int32_t *)(msg + 6)) = htonl(0);
00120 }
00121
00122
00123 {
00124
00125 *((u_int16_t *)(msg + 10)) = htons( AW_MCTXRX_MAX_PATHLEN + 8 + 4 + 2);
00126 }
00127
00128
00129 {
00130 strncpy(msg + 12, info->filename, AW_MCTXRX_MAX_PATHLEN);
00131 }
00132
00133
00134 {
00135 *( (u_int64_t *)(msg + 12 + AW_MCTXRX_MAX_PATHLEN) ) = htonll(info->filesize);
00136 }
00137
00138
00139 {
00140 *((u_int32_t *)(msg + 12 + AW_MCTXRX_MAX_PATHLEN + 8)) = htonl(info->nblocks);
00141 }
00142
00143
00144 {
00145
00146 *((u_int16_t *)(msg + 12 + AW_MCTXRX_MAX_PATHLEN + 8 + 4)) = htons( info->blocksize );
00147 }
00148
00149
00150 {
00151
00152 *((u_int16_t *)(msg + 12 + AW_MCTXRX_MAX_PATHLEN + 8 + 4 + 2)) = htons( info->retxport );
00153 }
00154
00155 return 12 + AW_MCTXRX_MAX_PATHLEN + 8 + 4 + 2 + 2;
00156 }
00157
00158
00159 inline static int32_t mctxrx_create_datamsg(u_int32_t xtnid, u_int32_t blockid,
00160 u_int16_t payload_size, int32_t fd,
00161 byte_t *msg)
00162 {
00163
00164 {
00165 *((u_int16_t *)msg) = htons(AW_MCTXRX_MSG_VERSION);
00166 }
00167
00168
00169 {
00170 *((u_int32_t *)(msg + 2)) = htonl(xtnid);
00171 }
00172
00173
00174 {
00175 *((u_int32_t *)(msg + 6)) = htonl(blockid);
00176 }
00177
00178
00179 {
00180 *((u_int16_t *)(msg + 10)) = htons( payload_size );
00181 }
00182
00183 if ( payload_size && read(fd, msg + 12, payload_size) != payload_size )
00184 return -1;
00185 else
00186 return 12 + payload_size ;
00187
00188 }
00189
00190
00191 inline static int32_t mctxrx_create_retxmsg(u_int32_t xtnid,
00192 aw_bitvector_t *bv,
00193 byte_t *msg)
00194 {
00195 int32_t
00196 payload_size = AW_BITVECTOR_GET_VECTOR_SIZE(bv) + sizeof(*bv);
00197
00198
00199 assert( sizeof(*bv) == sizeof(u_int32_t) );
00200
00201
00202 {
00203 *((u_int16_t *)msg) = htons(AW_MCTXRX_MSG_VERSION);
00204 }
00205
00206
00207 {
00208 *((u_int32_t *)(msg + 2)) = htonl(xtnid);
00209 }
00210
00211
00212 {
00213 *((u_int32_t *)(msg + 6)) = htonl(~0);
00214 }
00215
00216
00217 {
00218 *((u_int16_t *)(msg + 10)) = htons( payload_size );
00219 }
00220
00221
00222 {
00223 u_int32_t
00224 i,
00225 nwords = payload_size / sizeof(u_int32_t),
00226 *words = (u_int32_t *)(msg + 12);
00227
00228
00229 memcpy(words, bv, payload_size );
00230
00231
00232 for(i=0; i<nwords; i++) words[i] = htonl( words[i] );
00233 }
00234
00235 return 12 + payload_size ;
00236
00237 }
00238
00239
00240
00241 inline static int32_t mctxrx_extract_msghdr(aw_mctxrx_msghdr_t *hdr, byte_t *msg)
00242 {
00243
00244
00245 {
00246 hdr->version = ntohs( *((u_int16_t *)msg) );
00247 }
00248
00249
00250 if (hdr->version != AW_MCTXRX_MSG_VERSION ) return -1;
00251
00252
00253 {
00254 hdr->xtnid = ntohl( *((u_int32_t *)(msg + 2)) );
00255 }
00256
00257
00258 {
00259 hdr->blockid = ntohl( *((u_int32_t *)(msg + 6)) );
00260 }
00261
00262
00263 {
00264 hdr->payload_size = ntohs( *((u_int16_t *)(msg + 10)) );
00265 }
00266
00267
00268 hdr->payload = msg + 12;
00269
00270 return 0 ;
00271 }
00272
00273
00274 inline static int32_t mctxrx_extract_info(aw_mctxrx_info_t *info, aw_mctxrx_msghdr_t *hdr)
00275 {
00276
00277 if ( hdr->blockid != 0 ) return -1;
00278
00279
00280 {
00281 strncpy(info->filename, hdr->payload, AW_MCTXRX_MAX_PATHLEN);
00282 info->filename[AW_MCTXRX_MAX_PATHLEN -1] = 0;
00283 }
00284
00285
00286 {
00287 info->filesize = ntohll ( *( (u_int64_t *)(hdr->payload + AW_MCTXRX_MAX_PATHLEN) ) );
00288 }
00289
00290
00291 {
00292 info->nblocks = ntohl( *((u_int32_t *)(hdr->payload + AW_MCTXRX_MAX_PATHLEN + 8)) );
00293 }
00294
00295
00296 {
00297 info->blocksize = ntohs( *((u_int16_t *)(hdr->payload + AW_MCTXRX_MAX_PATHLEN + 8 + 4)) );
00298 }
00299
00300
00301 {
00302 info->retxport = ntohs( *((u_int16_t *)(hdr->payload + AW_MCTXRX_MAX_PATHLEN + 8 + 4 + 2)) );
00303 }
00304
00305
00306 return 0 ;
00307 }
00308
00309
00310 inline static aw_bitvector_t *mctxrx_extract_retxmsg(aw_mctxrx_msghdr_t *hdr)
00311 {
00312 aw_bitvector_t
00313 *bv = NULL;
00314
00315
00316 assert( sizeof(*bv) == sizeof(u_int32_t) );
00317
00318
00319 if ( hdr->blockid != ~0 ) goto done;
00320
00321 bv = aw_bitvector_create( 8*(hdr->payload_size - sizeof(*bv)) );
00322 if (bv == NULL) goto done;
00323
00324
00325 {
00326 u_int32_t
00327 i,
00328 nwords = hdr->payload_size / sizeof(u_int32_t),
00329 *words = (u_int32_t *)(hdr->payload);
00330
00331
00332 for(i=0; i<nwords; i++) words[i] = ntohl( words[i] );
00333
00334
00335 *bv = *( (aw_bitvector_t *)(hdr->payload) );
00336 memcpy(AW_BITVECTOR_GET_VECTOR(bv), hdr->payload + sizeof(*bv), (nwords*sizeof(u_int32_t)) - sizeof(*bv));
00337
00338 }
00339
00340 done:;
00341
00342 return bv;
00343 }
00344
00345 #endif