File: rawRecv.py

package info (click to toggle)
norm 1.5.9%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 9,664 kB
  • sloc: cpp: 123,494; xml: 7,536; tcl: 5,460; makefile: 3,441; python: 1,898; java: 1,750; ansic: 642; sh: 21; csh: 8
file content (102 lines) | stat: -rwxr-xr-x 3,509 bytes parent folder | download | duplicates (4)
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
#!/usr/bin/env python
'''
Example of using the NORM library directly.
You really shouldn't need to do this.  Use the pretty API instead.
But its here if you need it.
'''

import sys
import os.path
import ctypes
from optparse import OptionParser

from pynorm.core import libnorm, NormEventStruct
import pynorm.constants as c

USAGE = 'usage: %s [options] <cacheDir>' % sys.argv[0]
DEFAULT_ADDR = '224.1.2.3'
DEFAULT_PORT = 6003

def get_option_parser():
    parser = OptionParser(usage=USAGE)
    parser.set_defaults(address=DEFAULT_ADDR, port=DEFAULT_PORT)

    parser.add_option('-a', '--address'
            help='The IP address to bind to (default %s)' % DEFAULT_ADDR)
    parser.add_option('-p', '--port', type=int,
            help='The port number to listen on (default %i)' % DEFAULT_PORT)
    parser.add_option('-i', '--iface',
            help='The inteface to transmit multicast on.')
    return parser

def main(argv):
    (opts, args) = get_option_parser().parse_args(argv)

    if len(args) != 2:
        print get_option_parser().get_usage()
        return 1

    path = os.path.abspath(args[1])

    instance = libnorm.NormCreateInstance(False)
    session = libnorm.NormCreateSession(instance, opts.address, opts.port,
            c.NORM_NODE_ANY)

    if opts.iface:
        libnorm.NormSetMulticastInterface(session, opts.iface)

    libnorm.NormSetCacheDirectory(instance, path)
    libnorm.NormStartReceiver(session, 1024*1024)
    theEvent = NormEventStruct()

    try:
        while libnorm.NormGetNextEvent(instance, ctypes.byref(theEvent)):
            if theEvent.type == c.NORM_RX_OBJECT_NEW:
                print 'rawRecv.py: NORM_RX_OBJECT_NEW event ...'

            elif theEvent.type == c.NORM_RX_OBJECT_INFO:
                print 'rafRecv.py: NORM_RX_OBJECT_INFO event ...'

                if c.NORM_OBJECT_FILE == libnorm.NormObjectGetType(theEvent.object):
                    length = libnorm.NormObjectGetInfoLength(theEvent.object)
                    buffer = ctypes.create_string_buffer(length)
                    recv = libnorm.NormObjectGetInfo(theEvent.object, buffer, length)
                    if recv == 0:
                        print 'Error'
                    filename = os.path.join(path, buffer.value)
                    print 'Filename - %s' % filename
                    libnorm.NormFileRename(theEvent.object, filename)

            elif theEvent.type == c.NORM_RX_OBJECT_UPDATED:
                size = libnorm.NormObjectGetSize(theEvent.object)
                completed = size - libnorm.NormObjectGetBytesPending(theEvent.object)
                percent = 100.0 * (float(completed) / float(size))
                print '%.1f completed' % percent

            elif theEvent.type == c.NORM_RX_OBJECT_COMPLETED:
                print 'Complete'
                return 0

            elif theEvent.type == c.NORM_RX_OBJECT_ABORTED:
                print 'Aborted'
                return 0

            elif theEvent.type == c.NORM_REMOTE_SENDER_NEW:
                print 'New sender'

            elif theEvent.type == c.NORM_REMOTE_SENDER_ACTIVE:
                print 'Sender active'

            elif theEvent.type == c.NORM_REMOTE_SENDER_INACTIVE:
                print 'Sender inactive'
    except KeyboardInterrupt:
        pass

    libnorm.NormStopReceiver(session)
    libnorm.NormDestroySession(session)
    libnorm.NormDestroyInstance(instance)
    print 'Exiting.'
    return 0

if __name__ == '__main__':
    sys.exit(main(sys.argv))