# -*- generated by 1.0.2 -*-
import da
PatternExpr_256 = da.pat.TuplePattern([da.pat.ConstantPattern('done')])
PatternExpr_261 = da.pat.BoundPattern('_BoundPattern262_')
PatternExpr_276 = da.pat.TuplePattern([da.pat.ConstantPattern('Value'), da.pat.FreePattern('v')])
PatternExpr_263 = da.pat.TuplePattern([da.pat.FreePattern(None), da.pat.TuplePattern([da.pat.FreePattern(None), da.pat.FreePattern(None), da.pat.BoundPattern('_BoundPattern269_')]), da.pat.TuplePattern([da.pat.ConstantPattern('done')])])
_config_object = {'channel': 'Reliable', 'handling': 'one'}
import sys

class P(da.DistProcess):

    def __init__(self, procimpl, props):
        super().__init__(procimpl, props)
        self._PReceivedEvent_0 = []
        self._events.extend([da.pat.EventPattern(da.pat.ReceivedEvent, '_PReceivedEvent_0', PatternExpr_256, sources=[PatternExpr_261], destinations=None, timestamps=None, record_history=True, handlers=[]), da.pat.EventPattern(da.pat.ReceivedEvent, '_PReceivedEvent_1', PatternExpr_276, sources=None, destinations=None, timestamps=None, record_history=None, handlers=[self._P_handler_275])])

    def setup(self, ps, v, maxfail, **rest_361):
        super().setup(ps=ps, v=v, maxfail=maxfail, **rest_361)
        self._state.ps = ps
        self._state.v = v
        self._state.maxfail = maxfail
        self._state.x = (- 1)
        self._state.V = {self._state.v: False}
        self._state.receiveflag = False

    def run(self):
        super()._label('start', block=False)
        for i in range(self._state.maxfail):
            super()._label('one_round', block=False)
            for k in self._state.V:
                if (not self._state.V[k]):
                    self.send(('Value', k), self._state.ps)
                    self._state.V[k] = True
            super()._label('_st_label_221', block=False)
            _st_label_221 = 0
            while (_st_label_221 == 0):
                _st_label_221 += 1
                if self._state.receiveflag:
                    _st_label_221 += 1
                else:
                    super()._label('_st_label_221', block=True)
                    _st_label_221 -= 1
            else:
                if (_st_label_221 != 2):
                    continue
            if (_st_label_221 != 2):
                break
            self._state.receiveflag = False
        super()._label('end', block=False)
        self.send(('done',), self._state.ps)
        self._state.x = max([self._state.v for self._state.v in self._state.V if self._state.V[self._state.v]])
        self.output(('x = %r' % self._state.x))
        super()._label('_st_label_247', block=False)
        p = None

        def UniversalOpExpr_248():
            nonlocal p
            for p in self._state.ps:
                if (not PatternExpr_263.match_iter(self._PReceivedEvent_0, _BoundPattern269_=p, SELF_ID=self._id)):
                    return False
            return True
        _st_label_247 = 0
        while (_st_label_247 == 0):
            _st_label_247 += 1
            if UniversalOpExpr_248():
                _st_label_247 += 1
            else:
                super()._label('_st_label_247', block=True)
                _st_label_247 -= 1

    def _P_handler_275(self, v):
        self._state.receiveflag = True
        if (not (v in self._state.V)):
            self._state.V[v] = False
    _P_handler_275._labels = None
    _P_handler_275._notlabels = None

class Node_(da.NodeProcess):

    def __init__(self, procimpl, props):
        super().__init__(procimpl, props)
        self._events.extend([])

    def run(self):
        n = (int(sys.argv[1]) if (len(sys.argv) > 1) else 10)
        f = (int(sys.argv[2]) if (len(sys.argv) > 2) else 50)
        ps = self.new(P, num=n)
        for (i, p) in enumerate(list(ps)):
            self._setup({p}, (ps, i, f))
        self._start(ps)
