# -*- generated by 1.0.2 -*-
import da
PatternExpr_208 = da.pat.TuplePattern([da.pat.BoundPattern('_BoundPattern209_'), da.pat.FreePattern('rclock'), da.pat.FreePattern('message')])
_config_object = {}

class TagClock():
    pass

class LamportClock(da.DistProcess):

    def __init__(self, procimpl, props):
        super().__init__(procimpl, props)
        self._events.extend([da.pat.EventPattern(da.pat.ReceivedEvent, '_LamportClockReceivedEvent_0', PatternExpr_208, sources=None, destinations=None, timestamps=None, record_history=None, handlers=[self._LamportClock_handler_207])])

    def setup(self, **rest_235):
        super().setup(**rest_235)
        self._state._logical_clock = 0

    def run(self):
        pass

    def send(self, message, to, channel=None):
        if (not (to == self._id)):
            return super().send((TagClock, self._state._logical_clock, message), to, channel)
        else:
            return super().send(message, to, channel)

    def logical_clock(self):
        return self._state._logical_clock

    def _LamportClock_handler_207(self, rclock, message):
        self._state._logical_clock = (max(self._state._logical_clock, rclock) + 1)
        self.send(message, self._id)
    _LamportClock_handler_207._labels = None
    _LamportClock_handler_207._notlabels = None
