File: normFileRecv.cpp

package info (click to toggle)
norm 1.5.9%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 9,680 kB
  • sloc: cpp: 123,494; xml: 7,536; tcl: 5,460; makefile: 3,442; python: 1,898; java: 1,750; ansic: 642; sh: 21; csh: 8
file content (173 lines) | stat: -rw-r--r-- 6,819 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/******************************************************************************
 Simple NORM file receiver example app using the NORM API

 USAGE: 
 
 normRecvFile <rxCacheDirectory>

 BUILD (Unix): 
 
g++ -o normFileRecv normFileRecv.cpp -D_FILE_OFFSET_BITS=64 -I../common/ \
     -I../protolib/include ../lib/libnorm.a ../protolib/lib/libProtokit.a \
     -lpthread
         
     (for MacOS/BSD, add "-lresolv")
     (for Solaris, add "-lnsl -lsocket -lresolv")

******************************************************************************/            


// Notes:
//  1) The program exits once a single file has been received ...
//  2) The received file is written to the <rxCacheDirectory>
//  3) The program also will exit on <CTRL-C> from user
//  4) "normFileRecv" should be started before "normFileSend"

#include "normApi.h"     // for NORM API

#include "protoDefs.h"   // for ProtoSystemTime        
#include "protoDebug.h"  // for SetDebugLevel(), etc   

#include <stdio.h>       // for printf(), etc
#include <stdlib.h>      // for srand()
#include <string.h>      // for strrchr()

#ifdef WIN32
const char DIR_DELIMITER = '\\';
#else
const char DIR_DELIMITER = '/';
#endif // if/else WIN32/UNIX

int main(int argc, char* argv[])
{
    // 0) The rxCachePath should be argv[1] ...
    if (argc != 2)
    {
        fprintf(stderr, "normFileRecv error: invalid argument(s)\n");
        fprintf(stderr, "Usage: normRecvFile <rxCachePath>\n");
        return -1;   
    }    
    const char* rxCachePath = argv[1];
    
    // 1) Create a NORM API "NormInstance"
    NormInstanceHandle instance = NormCreateInstance();
    
    // 2) Create a NormSession using default "automatic" local node id
    NormSessionHandle session = NormCreateSession(instance,
                                                  "224.1.2.3", 
                                                   6003,
                                                   NORM_NODE_ANY);
    
    NormSetRxPortReuse(session, true);
    NormSetMulticastLoopback(session, true);
    
    // NOTE: These are debugging routines available 
    //       (not necessary for normal app use)
    // (Need to include "common/protoDebug.h" for this
    //SetDebugLevel(2);
    // Uncomment to turn on debug NORM message tracing
    //NormSetMessageTrace(session, true);
    // Uncomment to turn on some random packet loss for testing
    NormSetRxLoss(session, 10.0);  // 10% packet loss
    struct timeval currentTime;
    ProtoSystemTime(currentTime);
    // Uncomment to get different packet loss patterns from run to run
    srand(currentTime.tv_sec);  // seed random number generator
    
    // 3) Set receiver file cache path (where received files are mstored)
    if (!NormSetCacheDirectory(instance, rxCachePath))
    {
        fprintf(stderr, "normFileReceive: error setting cache directory\n");
        return -1;   
    }
    
    // 4) Start the receiver with 1 Mbyte buffer per sender
    NormStartReceiver(session, 1024*1024);
  
    // 5) Enter NORM event loop
    bool keepGoing = true;
    while (keepGoing)
    {
        NormEvent theEvent;
        if (!NormGetNextEvent(instance, &theEvent)) continue;
        switch (theEvent.type)
        {
           case NORM_RX_OBJECT_NEW:
                fprintf(stderr, "normFileRecv: NORM_RX_OBJECT_NEW event ...\n");
                break;

            case NORM_RX_OBJECT_INFO:
                // Assume info contains '/' delimited <path/fileName> string
                fprintf(stderr, "normFileRecv: NORM_RX_OBJECT_INFO event ...\n");
                if (NORM_OBJECT_FILE == NormObjectGetType(theEvent.object))
                {
                    char fileName[PATH_MAX];
                    strcpy(fileName, rxCachePath);
                    int pathLen = strlen(fileName);
                    if (DIR_DELIMITER != fileName[pathLen-1])
                    {
                        fileName[pathLen++] = DIR_DELIMITER;
                        fileName[pathLen] = '\0';
                    }
                    unsigned short nameLen = PATH_MAX - pathLen;
                    nameLen = NormObjectGetInfo(theEvent.object, fileName+pathLen, nameLen);
                    fileName[nameLen + pathLen] = '\0';
                    char* ptr = fileName + 5;
                    while ('\0' != *ptr)
                    {
                        if ('/' == *ptr) *ptr = DIR_DELIMITER;
                        ptr++;   
                    }
                    if (!NormFileRename(theEvent.object, fileName))
                        fprintf(stderr, "normFileRecv: NormSetFileName(%s) error\n", fileName);
                }
                break;

            case NORM_RX_OBJECT_UPDATED:
            {
                //fprintf(stderr, "normFileRecv: NORM_RX_OBJECT_UPDATE event ...\n");
                // Uncomment this stuff to monitor file receive progress
                // (At high packet rates, you may want to be careful here and
                //  only calculate/post updates occasionally rather than for
                //  each and every RX_OBJECT_UPDATE event)
                NormSize objectSize = NormObjectGetSize(theEvent.object);
                fprintf(stderr, "sizeof(NormSize) = %d\n", (int)sizeof(NormSize));
                NormSize completed = objectSize - NormObjectGetBytesPending(theEvent.object);
                double percentComplete = 100.0 * ((double)completed/(double)objectSize);
                fprintf(stderr, "normFileRecv: completion status %lu/%lu (%3.0lf%%)\n",
                                (unsigned long)completed, (unsigned long)objectSize, percentComplete);
                break;                 
            }

            case NORM_RX_OBJECT_COMPLETED:
                fprintf(stderr, "normFileRecv: NORM_RX_OBJECT_COMPLETED event ...\n");
                keepGoing = false;
                break;

            case NORM_RX_OBJECT_ABORTED:
                fprintf(stderr, "normFileRecv: NORM_RX_OBJECT_ABORTED event ...\n");
                break;

            case NORM_REMOTE_SENDER_NEW:
                fprintf(stderr, "normFileRecv: NORM_REMOTE_SENDER_NEW event ...\n");
                break;

            case NORM_REMOTE_SENDER_ACTIVE:
                fprintf(stderr, "normFileRecv: NORM_REMOTE_SENDER_ACTIVE event ...\n");
                break;

            case NORM_REMOTE_SENDER_INACTIVE:
                fprintf(stderr, "normFileRecv: NORM_REMOTE_SENDER_INACTIVE event ...\n");
                break;

            default:
                fprintf(stderr, "normFileRecv: Got event type: %d\n", theEvent.type); 
        }  // end switch(theEvent.type)
    }
    NormStopReceiver(session);
    NormDestroySession(session);
    NormDestroyInstance(instance);
    
    fprintf(stderr, "normFileRecv: Done.\n");
    return 0;
}  // end main()