runtime/eRPCServer.py
author Edouard Tisserant <edouard@beremiz.fr>
Tue, 09 Jul 2024 11:46:19 +0200
changeset 3984 883a85b9ebcc
parent 3905 d47fe28d799a
permissions -rw-r--r--
MQTT: still WIP, generated C code builds and link.
3884
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     1
#!/usr/bin/env python
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     2
# -*- coding: utf-8 -*-
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     3
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     4
# Written by Edouard TISSERANT (C) 2024
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     5
# This file is part of Beremiz runtime
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     6
# See COPYING.Runtime file for copyrights details.
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     7
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     8
import sys
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     9
import traceback
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    10
from inspect import getmembers, isfunction
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    11
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    12
import erpc
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    13
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    14
# eRPC service code
3887
2df45e4bd500 Fix variable forcing. Now works with eRPC. PLCObject API changed.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3885
diff changeset
    15
from erpc_interface.erpc_PLCObject.common import PSKID, PLCstatus, TraceVariables, trace_sample, PLCstatus_enum, log_message
3884
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    16
from erpc_interface.erpc_PLCObject.interface import IBeremizPLCObjectService
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    17
from erpc_interface.erpc_PLCObject.server import BeremizPLCObjectServiceService
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    18
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    19
from runtime import GetPLCObjectSingleton as PLC
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    20
from runtime.loglevels import LogLevelsDict
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    21
from runtime.ServicePublisher import ServicePublisher
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    22
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    23
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    24
def ReturnAsLastOutput(method, args_wrapper, *args):
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    25
    args[-1].value = method(*args_wrapper(*args[:-1]))
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    26
    return 0
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    27
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    28
def TranslatedReturnAsLastOutput(translator):
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    29
    def wrapper(method, args_wrapper, *args):
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    30
        args[-1].value = translator(method(*args_wrapper(*args[:-1])))
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    31
        return 0
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    32
    return wrapper
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    33
    
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    34
    
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    35
ReturnWrappers = {
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    36
    "AppendChunkToBlob":ReturnAsLastOutput,
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    37
    "GetLogMessage":TranslatedReturnAsLastOutput(
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    38
        lambda res:log_message(*res)),
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    39
    "GetPLCID":TranslatedReturnAsLastOutput(
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    40
        lambda res:PSKID(*res)),
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    41
    "GetPLCstatus":TranslatedReturnAsLastOutput(
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    42
        lambda res:PLCstatus(getattr(PLCstatus_enum, res[0]),res[1])),
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    43
    "GetTraceVariables":TranslatedReturnAsLastOutput(
3885
22a009561502 Fix variable tracing in new eRPC interface.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3884
diff changeset
    44
        lambda res:TraceVariables(getattr(PLCstatus_enum, res[0]),[trace_sample(*sample) for sample in res[1]])),
3884
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    45
    "MatchMD5":ReturnAsLastOutput,
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    46
    "NewPLC":ReturnAsLastOutput,
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    47
    "SeedBlob":ReturnAsLastOutput,
3885
22a009561502 Fix variable tracing in new eRPC interface.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3884
diff changeset
    48
    "SetTraceVariablesList": ReturnAsLastOutput,
22a009561502 Fix variable tracing in new eRPC interface.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3884
diff changeset
    49
    "StopPLC":ReturnAsLastOutput,
3884
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    50
}
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    51
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    52
ArgsWrappers = {
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    53
    "AppendChunkToBlob":
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    54
        lambda data, blobID:(data, bytes(blobID)),
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    55
    "NewPLC":
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    56
        lambda md5sum, plcObjectBlobID, extrafiles: (
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    57
            md5sum, bytes(plcObjectBlobID), [(f.fname, bytes(f.blobID)) for f in extrafiles]),
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    58
    "SetTraceVariablesList": 
3887
2df45e4bd500 Fix variable forcing. Now works with eRPC. PLCObject API changed.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3885
diff changeset
    59
        lambda orders : ([(order.idx, None if len(order.force)==0 else bytes(order.force)) for order in orders],)
3884
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    60
}
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    61
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    62
def rpc_wrapper(method_name):
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    63
    PLCobj = PLC()
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    64
    method=getattr(PLCobj, method_name)
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    65
    args_wrapper = ArgsWrappers.get(method_name, lambda *x:x)
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    66
    return_wrapper = ReturnWrappers.get(method_name,
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    67
        lambda method, args_wrapper, *args: method(*args_wrapper(*args)))
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    68
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    69
    def exception_wrapper(self, *args):
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    70
        try:
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    71
            return_wrapper(method, args_wrapper, *args)
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    72
            return 0
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    73
        except Exception as e:
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    74
            print(traceback.format_exc())
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    75
            PLCobj.LogMessage(CRITICAL_LOG_LEVEL, f'eRPC call {method_name} Exception "{str(e)}"')
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    76
            raise
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    77
        
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    78
    return exception_wrapper
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    79
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    80
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    81
class eRPCServer(object):
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    82
    def __init__(self, servicename, ip_addr, port):
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    83
        self.continueloop = True
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    84
        self.server = None
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    85
        self.transport = None
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    86
        self.servicename = servicename
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    87
        self.ip_addr = ip_addr
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    88
        self.port = port
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    89
        self.servicepublisher = None
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    90
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    91
    def _to_be_published(self):
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    92
        return self.servicename is not None and \
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    93
               self.ip_addr not in ["", "localhost", "127.0.0.1"]
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    94
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    95
    def PrintServerInfo(self):
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    96
        print(_("eRPC port :"), self.port)
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    97
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    98
        if self._to_be_published():
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    99
            print(_("Publishing service on local network"))
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   100
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   101
        if sys.stdout:
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   102
            sys.stdout.flush()
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   103
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   104
    def Loop(self, when_ready):
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   105
        if self._to_be_published():
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   106
            self.Publish()
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   107
3905
d47fe28d799a eRPC Server: fix exception at disconnect and reconnecting
Edouard Tisserant <edouard@beremiz.fr>
parents: 3898
diff changeset
   108
        # service handler calls PLC object though erpc_stubs's wrappers
d47fe28d799a eRPC Server: fix exception at disconnect and reconnecting
Edouard Tisserant <edouard@beremiz.fr>
parents: 3898
diff changeset
   109
        handler = type(
d47fe28d799a eRPC Server: fix exception at disconnect and reconnecting
Edouard Tisserant <edouard@beremiz.fr>
parents: 3898
diff changeset
   110
            "PLCObjectServiceHandlder", 
d47fe28d799a eRPC Server: fix exception at disconnect and reconnecting
Edouard Tisserant <edouard@beremiz.fr>
parents: 3898
diff changeset
   111
            (IBeremizPLCObjectService,),
d47fe28d799a eRPC Server: fix exception at disconnect and reconnecting
Edouard Tisserant <edouard@beremiz.fr>
parents: 3898
diff changeset
   112
            {name: rpc_wrapper(name)              
d47fe28d799a eRPC Server: fix exception at disconnect and reconnecting
Edouard Tisserant <edouard@beremiz.fr>
parents: 3898
diff changeset
   113
                    for name,_func in getmembers(IBeremizPLCObjectService, isfunction)})()
d47fe28d799a eRPC Server: fix exception at disconnect and reconnecting
Edouard Tisserant <edouard@beremiz.fr>
parents: 3898
diff changeset
   114
        
d47fe28d799a eRPC Server: fix exception at disconnect and reconnecting
Edouard Tisserant <edouard@beremiz.fr>
parents: 3898
diff changeset
   115
        service = BeremizPLCObjectServiceService(handler)
d47fe28d799a eRPC Server: fix exception at disconnect and reconnecting
Edouard Tisserant <edouard@beremiz.fr>
parents: 3898
diff changeset
   116
d47fe28d799a eRPC Server: fix exception at disconnect and reconnecting
Edouard Tisserant <edouard@beremiz.fr>
parents: 3898
diff changeset
   117
        # TODO initialize Serial transport layer if selected
d47fe28d799a eRPC Server: fix exception at disconnect and reconnecting
Edouard Tisserant <edouard@beremiz.fr>
parents: 3898
diff changeset
   118
        # transport = erpc.transport.SerialTransport(device, baudrate)
d47fe28d799a eRPC Server: fix exception at disconnect and reconnecting
Edouard Tisserant <edouard@beremiz.fr>
parents: 3898
diff changeset
   119
d47fe28d799a eRPC Server: fix exception at disconnect and reconnecting
Edouard Tisserant <edouard@beremiz.fr>
parents: 3898
diff changeset
   120
        # initialize TCP transport layer
d47fe28d799a eRPC Server: fix exception at disconnect and reconnecting
Edouard Tisserant <edouard@beremiz.fr>
parents: 3898
diff changeset
   121
        self.transport = erpc.transport.TCPTransport(self.ip_addr, int(self.port), True)
d47fe28d799a eRPC Server: fix exception at disconnect and reconnecting
Edouard Tisserant <edouard@beremiz.fr>
parents: 3898
diff changeset
   122
d47fe28d799a eRPC Server: fix exception at disconnect and reconnecting
Edouard Tisserant <edouard@beremiz.fr>
parents: 3898
diff changeset
   123
        self.server = erpc.simple_server.SimpleServer(self.transport, erpc.basic_codec.BasicCodec)
d47fe28d799a eRPC Server: fix exception at disconnect and reconnecting
Edouard Tisserant <edouard@beremiz.fr>
parents: 3898
diff changeset
   124
        self.server.add_service(service)
d47fe28d799a eRPC Server: fix exception at disconnect and reconnecting
Edouard Tisserant <edouard@beremiz.fr>
parents: 3898
diff changeset
   125
d47fe28d799a eRPC Server: fix exception at disconnect and reconnecting
Edouard Tisserant <edouard@beremiz.fr>
parents: 3898
diff changeset
   126
        when_ready()
d47fe28d799a eRPC Server: fix exception at disconnect and reconnecting
Edouard Tisserant <edouard@beremiz.fr>
parents: 3898
diff changeset
   127
3884
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   128
        while self.continueloop:
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   129
3905
d47fe28d799a eRPC Server: fix exception at disconnect and reconnecting
Edouard Tisserant <edouard@beremiz.fr>
parents: 3898
diff changeset
   130
            try:
d47fe28d799a eRPC Server: fix exception at disconnect and reconnecting
Edouard Tisserant <edouard@beremiz.fr>
parents: 3898
diff changeset
   131
                self.server.run()
d47fe28d799a eRPC Server: fix exception at disconnect and reconnecting
Edouard Tisserant <edouard@beremiz.fr>
parents: 3898
diff changeset
   132
            except erpc.transport.ConnectionClosed:
d47fe28d799a eRPC Server: fix exception at disconnect and reconnecting
Edouard Tisserant <edouard@beremiz.fr>
parents: 3898
diff changeset
   133
                PLC().LogMessage(LogLevelsDict["DEBUG"], 'eRPC client disconnected')
d47fe28d799a eRPC Server: fix exception at disconnect and reconnecting
Edouard Tisserant <edouard@beremiz.fr>
parents: 3898
diff changeset
   134
            except Exception as e:
d47fe28d799a eRPC Server: fix exception at disconnect and reconnecting
Edouard Tisserant <edouard@beremiz.fr>
parents: 3898
diff changeset
   135
                self.Unpublish()
d47fe28d799a eRPC Server: fix exception at disconnect and reconnecting
Edouard Tisserant <edouard@beremiz.fr>
parents: 3898
diff changeset
   136
                #TODO crash better
d47fe28d799a eRPC Server: fix exception at disconnect and reconnecting
Edouard Tisserant <edouard@beremiz.fr>
parents: 3898
diff changeset
   137
                raise e
3884
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   138
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   139
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   140
    def Restart(self):
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   141
        self.server.stop()
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   142
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   143
    def Quit(self):
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   144
        self.continueloop = False
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   145
        self.server.stop()
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   146
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   147
    def Publish(self):
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   148
        self.servicepublisher = ServicePublisher("ERPC")
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   149
        self.servicepublisher.RegisterService(self.servicename,
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   150
                                              self.ip_addr, self.port)
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   151
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   152
    def Unpublish(self):
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   153
        if self.servicepublisher is not None:
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   154
            self.servicepublisher.UnRegisterService()
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   155
            self.servicepublisher = None