# -*- generated by 1.0.6 -*-
import da
PatternExpr_210 = da.pat.TuplePattern([da.pat.BoundPattern('_BoundPattern211_'), da.pat.FreePattern('rclock'), da.pat.FreePattern('message')])
PatternExpr_218 = da.pat.FreePattern('src')
_config_object = {}

class ClockTag():
    pass

class LamportTimestamp(da.DistProcess):

    def __init__(self, procimpl, props):
        super().__init__(procimpl, props)
        self._events.extend([da.pat.EventPattern(da.pat.ReceivedEvent, '_LamportTimestampReceivedEvent_0', PatternExpr_210, sources=[PatternExpr_218], destinations=None, timestamps=None, record_history=None, handlers=[self._LamportTimestamp_handler_209])])

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

    def run(self):
        pass

    def send(self, message, to, channel=None, noclock=False, **rest):
        if noclock:
            return super().send(message, to, channel, **rest)
        else:
            return super().send((ClockTag, self._state._logical_clock, message), to, channel, **rest)

    def logical_time(self):
        'Returns the current value of the logical clock.'
        return self._state._logical_clock

    def incr_logical_time(self):
        'Increments the logical clock.'
        self._state._logical_clock += 1

    def _LamportTimestamp_handler_209(self, rclock, message, src):
        self._state._logical_clock = (max(self._state._logical_clock, rclock) + 1)
        super().send(message, to=self._id, impersonate=src)
    _LamportTimestamp_handler_209._labels = None
    _LamportTimestamp_handler_209._notlabels = None
