-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmulticast_api.py
More file actions
executable file
·143 lines (120 loc) · 3.81 KB
/
multicast_api.py
File metadata and controls
executable file
·143 lines (120 loc) · 3.81 KB
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
# -*- coding: utf-8 -*-
"""
***************************************************************************
* Copyright (C) 2023, Lanka Hsu, <lankahsu@gmail.com>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
***************************************************************************
"""
#import os, sys, errno, getopt, signal, time, io
#from time import sleep
from pythonX9 import *
from threadx_api import *
import select, socket
import struct
class multicast_ctx(pythonX9, threadx_ctx):
def openx(self):
self.sockfd = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
self.sockfd.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
#sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 32)
def closex(self):
if not self.sockfd is None:
self.sockfd.close()
self.sockfd = None
DBG_DB_LN(self, "{}".format(DBG_TXT_DONE) )
def serverx(self):
mreq = struct.pack("4sl", socket.inet_aton(self.addr), socket.INADDR_ANY)
self.sockfd.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
DBG_IF_LN(self, "bind ... ({}:{})".format(self.addr, self.port) )
self.sockfd.bind((self.addr, self.port))
def writex(self, buf):
DBG_DB_LN(self, "send {}:{} - {}".format(self.addr, self.port, buf) )
if ( self.is_quit == 0 ):
try:
self.sockfd.sendto(buf, (self.addr, self.port))
except Exception:
pass
def readx(self):
self.serverx()
rlist = [self.sockfd]
wlist, xlist = [], []
DBG_WN_LN(self, "{}".format( DBG_TXT_RUN_LOOP ) )
while ( self.is_quit == 0 ):
readable, writeable, exceptional = select.select(rlist, wlist, xlist, 1)
for sock in readable:
try:
if self.sockfd is not None:
buffer = self.sockfd.recv(self.max_size)
#print len(buf)
#print repr(buf)
if not self.readcb is None:
self.readcb(buffer)
else:
DBG_DB_LN(self, "buffer[{}] - {}".format( len(buffer), repr(buffer)) )
except KeyboardInterrupt:
self.is_quit = 1
except (IOError, OSError) as exc:
err = None
if hasattr(exc, 'errno'):
err = exc.errno
elif exc.args:
err = exc.args[0]
if err == errno.EINTR:
continue
raise
finally:
pass
self.closex()
def threadx_handler(self):
#DBG_IF_LN(self, "enter")
self.threadx_set_inloop(1)
self.readx()
self.threadx_set_inloop(0)
DBG_WN_LN(self, "{}".format(DBG_TXT_BYE_BYE))
def release(self):
if ( self.is_quit == 0 ):
self.is_quit = 1
if ( self.threadx_inloop() == 1 ):
self.threadx_wakeup()
self.threadx_join()
self.closex()
DBG_DB_LN(self, "{}".format(DBG_TXT_DONE))
def ctx_init(self, url, port, readcb):
DBG_DB_LN(self, "{}".format(DBG_TXT_ENTER))
self.sockfd = None
self.addr = url
self.port = port
self.readcb = readcb
self.max_listen = 1
self.max_size = 2048
self.closex()
self.openx()
self.count = 0
def __init__(self, url, port, readcb, **kwargs):
if ( isPYTHON(PYTHON_V3) ):
super().__init__(**kwargs)
else:
super(multicast_ctx, self).__init__(**kwargs)
DBG_TR_LN(self, "{}".format(DBG_TXT_ENTER))
self._kwargs = kwargs
self.ctx_init(url, port, readcb)
def parse_args(self, args):
DBG_TR_LN(self, "{}".format(DBG_TXT_ENTER))
self._args = args
def start(self, args={}):
DBG_TR_LN(self, "{}".format(DBG_TXT_START))
self.parse_args(args)
self.threadx_init()
#ip="239.255.255.250"
#port=3618
#multicast = multicast_ctx(ip, port, notify_cb)
#multicast.release()