author | Edouard Tisserant <edouard.tisserant@gmail.com> |
Sun, 17 Jul 2022 18:07:46 +0200 | |
changeset 3548 | da7b461f54b7 |
parent 3311 | d73af2eeea05 |
child 3750 | f62625418bff |
permissions | -rw-r--r-- |
2270
d9175daf6522
Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff
changeset
|
1 |
#!/usr/bin/env python |
d9175daf6522
Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff
changeset
|
2 |
# -*- coding: utf-8 -*- |
d9175daf6522
Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff
changeset
|
3 |
|
d9175daf6522
Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff
changeset
|
4 |
# This file is part of Beremiz runtime. |
d9175daf6522
Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff
changeset
|
5 |
|
d9175daf6522
Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff
changeset
|
6 |
# Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD |
d9175daf6522
Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff
changeset
|
7 |
# Copyright (C) 2017: Andrey Skvortsov |
d9175daf6522
Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff
changeset
|
8 |
# Copyright (C) 2018: Edouard TISSERANT |
d9175daf6522
Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff
changeset
|
9 |
|
d9175daf6522
Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff
changeset
|
10 |
# See COPYING file for copyrights details. |
d9175daf6522
Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff
changeset
|
11 |
|
2309
d8fb90a2e11f
Please pylint and pep8
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
2270
diff
changeset
|
12 |
from __future__ import absolute_import |
d8fb90a2e11f
Please pylint and pep8
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
2270
diff
changeset
|
13 |
from __future__ import print_function |
2270
d9175daf6522
Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff
changeset
|
14 |
import sys |
2603
1ffdc62784cf
Add some "pipe to self" trick in Pyro server to accelerate runtime shutdown instead of waiting for arbitrary pyro timeout.
Edouard Tisserant
parents:
2492
diff
changeset
|
15 |
import os |
2270
d9175daf6522
Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff
changeset
|
16 |
|
d9175daf6522
Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff
changeset
|
17 |
import Pyro |
d9175daf6522
Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff
changeset
|
18 |
import Pyro.core as pyro |
d9175daf6522
Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff
changeset
|
19 |
import runtime |
d9175daf6522
Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff
changeset
|
20 |
from runtime.ServicePublisher import ServicePublisher |
d9175daf6522
Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff
changeset
|
21 |
|
2309
d8fb90a2e11f
Please pylint and pep8
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
2270
diff
changeset
|
22 |
|
2476
1881d0ff5ae2
PyroServer: fixed naming of class, and reorganized methods in order to ease subclassing, in case of customization of service publication on the local network (i.e. zeroconf)
Edouard Tisserant
parents:
2315
diff
changeset
|
23 |
class PyroServer(object): |
2270
d9175daf6522
Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff
changeset
|
24 |
def __init__(self, servicename, ip_addr, port): |
d9175daf6522
Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff
changeset
|
25 |
self.continueloop = True |
d9175daf6522
Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff
changeset
|
26 |
self.daemon = None |
d9175daf6522
Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff
changeset
|
27 |
self.servicename = servicename |
d9175daf6522
Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff
changeset
|
28 |
self.ip_addr = ip_addr |
d9175daf6522
Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff
changeset
|
29 |
self.port = port |
d9175daf6522
Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff
changeset
|
30 |
self.servicepublisher = None |
2603
1ffdc62784cf
Add some "pipe to self" trick in Pyro server to accelerate runtime shutdown instead of waiting for arbitrary pyro timeout.
Edouard Tisserant
parents:
2492
diff
changeset
|
31 |
self.piper, self.pipew = None, None |
2270
d9175daf6522
Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff
changeset
|
32 |
|
d9175daf6522
Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff
changeset
|
33 |
def _to_be_published(self): |
d9175daf6522
Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff
changeset
|
34 |
return self.servicename is not None and \ |
2311
bef2b4b87370
Beremiz_service now binds both pyro and nevow ports to interface given with '-i' argument.
Edouard Tisserant
parents:
2270
diff
changeset
|
35 |
self.ip_addr not in ["", "localhost", "127.0.0.1"] |
2270
d9175daf6522
Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff
changeset
|
36 |
|
d9175daf6522
Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff
changeset
|
37 |
def PrintServerInfo(self): |
d9175daf6522
Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff
changeset
|
38 |
print(_("Pyro port :"), self.port) |
d9175daf6522
Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff
changeset
|
39 |
|
d9175daf6522
Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff
changeset
|
40 |
if self._to_be_published(): |
d9175daf6522
Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff
changeset
|
41 |
print(_("Publishing service on local network")) |
d9175daf6522
Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff
changeset
|
42 |
|
d9175daf6522
Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff
changeset
|
43 |
sys.stdout.flush() |
d9175daf6522
Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff
changeset
|
44 |
|
d9175daf6522
Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff
changeset
|
45 |
def PyroLoop(self, when_ready): |
2492
7dd551ac2fa0
check_sources.sh makes me become even less productive
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
2476
diff
changeset
|
46 |
if self._to_be_published(): |
7dd551ac2fa0
check_sources.sh makes me become even less productive
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
2476
diff
changeset
|
47 |
self.Publish() |
7dd551ac2fa0
check_sources.sh makes me become even less productive
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
2476
diff
changeset
|
48 |
|
2270
d9175daf6522
Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff
changeset
|
49 |
while self.continueloop: |
d9175daf6522
Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff
changeset
|
50 |
Pyro.config.PYRO_MULTITHREADED = 0 |
d9175daf6522
Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff
changeset
|
51 |
pyro.initServer() |
d9175daf6522
Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff
changeset
|
52 |
self.daemon = pyro.Daemon(host=self.ip_addr, port=self.port) |
d9175daf6522
Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff
changeset
|
53 |
|
d9175daf6522
Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff
changeset
|
54 |
# pyro never frees memory after connection close if no timeout set |
d9175daf6522
Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff
changeset
|
55 |
# taking too small timeout value may cause |
d9175daf6522
Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff
changeset
|
56 |
# unwanted diconnection when IDE is kept busy for long periods |
d9175daf6522
Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff
changeset
|
57 |
self.daemon.setTimeout(60) |
2309
d8fb90a2e11f
Please pylint and pep8
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
2270
diff
changeset
|
58 |
|
d8fb90a2e11f
Please pylint and pep8
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
2270
diff
changeset
|
59 |
pyro_obj = Pyro.core.ObjBase() |
2270
d9175daf6522
Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff
changeset
|
60 |
pyro_obj.delegateTo(runtime.GetPLCObjectSingleton()) |
d9175daf6522
Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff
changeset
|
61 |
|
d9175daf6522
Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff
changeset
|
62 |
self.daemon.connect(pyro_obj, "PLCObject") |
d9175daf6522
Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff
changeset
|
63 |
|
d9175daf6522
Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff
changeset
|
64 |
when_ready() |
3308
358ccd42e052
Runtime: avoids using "pipe to self" bailout unblocking trick on windows, since select() only takes sockets.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
2625
diff
changeset
|
65 |
|
358ccd42e052
Runtime: avoids using "pipe to self" bailout unblocking trick on windows, since select() only takes sockets.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
2625
diff
changeset
|
66 |
# "pipe to self" trick to to accelerate runtime shutdown |
358ccd42e052
Runtime: avoids using "pipe to self" bailout unblocking trick on windows, since select() only takes sockets.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
2625
diff
changeset
|
67 |
# instead of waiting for arbitrary pyro timeout. |
358ccd42e052
Runtime: avoids using "pipe to self" bailout unblocking trick on windows, since select() only takes sockets.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
2625
diff
changeset
|
68 |
others = [] |
358ccd42e052
Runtime: avoids using "pipe to self" bailout unblocking trick on windows, since select() only takes sockets.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
2625
diff
changeset
|
69 |
if not sys.platform.startswith('win'): |
358ccd42e052
Runtime: avoids using "pipe to self" bailout unblocking trick on windows, since select() only takes sockets.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
2625
diff
changeset
|
70 |
self.piper, self.pipew = os.pipe() |
3311
d73af2eeea05
Runtime/unix: Fix typo preventing start of Pyro server
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3308
diff
changeset
|
71 |
others.append(self.piper) |
3308
358ccd42e052
Runtime: avoids using "pipe to self" bailout unblocking trick on windows, since select() only takes sockets.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
2625
diff
changeset
|
72 |
|
358ccd42e052
Runtime: avoids using "pipe to self" bailout unblocking trick on windows, since select() only takes sockets.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
2625
diff
changeset
|
73 |
self.daemon.requestLoop(others=others, callback=lambda x: None) |
2603
1ffdc62784cf
Add some "pipe to self" trick in Pyro server to accelerate runtime shutdown instead of waiting for arbitrary pyro timeout.
Edouard Tisserant
parents:
2492
diff
changeset
|
74 |
self.piper, self.pipew = None, None |
2625
e5ce6c4a8672
Fixed code quality according to pep8 and pylint.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
2603
diff
changeset
|
75 |
if hasattr(self, 'sock'): |
2603
1ffdc62784cf
Add some "pipe to self" trick in Pyro server to accelerate runtime shutdown instead of waiting for arbitrary pyro timeout.
Edouard Tisserant
parents:
2492
diff
changeset
|
76 |
self.daemon.sock.close() |
2476
1881d0ff5ae2
PyroServer: fixed naming of class, and reorganized methods in order to ease subclassing, in case of customization of service publication on the local network (i.e. zeroconf)
Edouard Tisserant
parents:
2315
diff
changeset
|
77 |
self.Unpublish() |
2270
d9175daf6522
Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff
changeset
|
78 |
|
d9175daf6522
Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff
changeset
|
79 |
def Restart(self): |
2476
1881d0ff5ae2
PyroServer: fixed naming of class, and reorganized methods in order to ease subclassing, in case of customization of service publication on the local network (i.e. zeroconf)
Edouard Tisserant
parents:
2315
diff
changeset
|
80 |
self.daemon.shutdown(True) |
2270
d9175daf6522
Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff
changeset
|
81 |
|
d9175daf6522
Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff
changeset
|
82 |
def Quit(self): |
d9175daf6522
Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff
changeset
|
83 |
self.continueloop = False |
2476
1881d0ff5ae2
PyroServer: fixed naming of class, and reorganized methods in order to ease subclassing, in case of customization of service publication on the local network (i.e. zeroconf)
Edouard Tisserant
parents:
2315
diff
changeset
|
84 |
self.daemon.shutdown(True) |
2603
1ffdc62784cf
Add some "pipe to self" trick in Pyro server to accelerate runtime shutdown instead of waiting for arbitrary pyro timeout.
Edouard Tisserant
parents:
2492
diff
changeset
|
85 |
self.daemon.closedown() |
3308
358ccd42e052
Runtime: avoids using "pipe to self" bailout unblocking trick on windows, since select() only takes sockets.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
2625
diff
changeset
|
86 |
if not sys.platform.startswith('win'): |
358ccd42e052
Runtime: avoids using "pipe to self" bailout unblocking trick on windows, since select() only takes sockets.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
2625
diff
changeset
|
87 |
if self.pipew is not None: |
358ccd42e052
Runtime: avoids using "pipe to self" bailout unblocking trick on windows, since select() only takes sockets.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
2625
diff
changeset
|
88 |
os.write(self.pipew, "goodbye") |
2270
d9175daf6522
Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff
changeset
|
89 |
|
2476
1881d0ff5ae2
PyroServer: fixed naming of class, and reorganized methods in order to ease subclassing, in case of customization of service publication on the local network (i.e. zeroconf)
Edouard Tisserant
parents:
2315
diff
changeset
|
90 |
def Publish(self): |
1881d0ff5ae2
PyroServer: fixed naming of class, and reorganized methods in order to ease subclassing, in case of customization of service publication on the local network (i.e. zeroconf)
Edouard Tisserant
parents:
2315
diff
changeset
|
91 |
self.servicepublisher = ServicePublisher("PYRO") |
1881d0ff5ae2
PyroServer: fixed naming of class, and reorganized methods in order to ease subclassing, in case of customization of service publication on the local network (i.e. zeroconf)
Edouard Tisserant
parents:
2315
diff
changeset
|
92 |
self.servicepublisher.RegisterService(self.servicename, |
1881d0ff5ae2
PyroServer: fixed naming of class, and reorganized methods in order to ease subclassing, in case of customization of service publication on the local network (i.e. zeroconf)
Edouard Tisserant
parents:
2315
diff
changeset
|
93 |
self.ip_addr, self.port) |
1881d0ff5ae2
PyroServer: fixed naming of class, and reorganized methods in order to ease subclassing, in case of customization of service publication on the local network (i.e. zeroconf)
Edouard Tisserant
parents:
2315
diff
changeset
|
94 |
|
1881d0ff5ae2
PyroServer: fixed naming of class, and reorganized methods in order to ease subclassing, in case of customization of service publication on the local network (i.e. zeroconf)
Edouard Tisserant
parents:
2315
diff
changeset
|
95 |
def Unpublish(self): |
2270
d9175daf6522
Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff
changeset
|
96 |
if self.servicepublisher is not None: |
d9175daf6522
Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff
changeset
|
97 |
self.servicepublisher.UnRegisterService() |
d9175daf6522
Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff
changeset
|
98 |
self.servicepublisher = None |