Skip to content

Commit f686422

Browse files
committed
Clean up machine.run loops for a bit better efficiency
1 parent 5312c98 commit f686422

File tree

4 files changed

+26
-21
lines changed

4 files changed

+26
-21
lines changed

server/enip/device.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1897,8 +1897,8 @@ def closure():
18971897
log.detail( "%s Parsing: %3d-%3d of %r", target, beg, end, reqdata )
18981898
req = dotdict()
18991899
req.input = reqdata[beg:end]
1900+
source = peekable( req.input )
19001901
with target.parser as machine:
1901-
source = peekable( req.input )
19021902
with contextlib.closing( machine.run( source=source, data=req )) as engine:
19031903
for m,s in engine:
19041904
pass
@@ -2259,8 +2259,12 @@ def request( self, data, addr=None ):
22592259
source = rememberable( data.request.input )
22602260
with self.parser_service_path as machine:
22612261
with contextlib.closing( machine.run( source=source, data=targetpath )) as engine:
2262-
for i,(m,s) in enumerate( engine ):
2262+
for m,s in engine:
22632263
pass
2264+
# for i,(m,s) in enumerate( engine ):
2265+
# log.detail( "%s #%3d -> %10.10s; next byte %3d: %-10.10r: %s",
2266+
# machine.name_centered(), i, s, source.sent, source.peek(),
2267+
# repr( data ) if log.getEffectiveLevel() < logging.DETAIL else misc.reprlib.repr( data ))
22642268
if log.isEnabledFor( logging.DETAIL ):
22652269
log.detail( "%s Routing request to target Object at address %s", self, enip_format( targetpath ))
22662270
# We have the service and path. Find the target Object (see state_multiple_service.closure)
@@ -2275,12 +2279,13 @@ def request( self, data, addr=None ):
22752279
assert target, "Unknown CIP Object in request: %s" % ( enip_format( targetpath ))
22762280
source = rememberable( data.request.input )
22772281
with target.parser as machine:
2278-
with contextlib.closing( machine.run( path='request', source=source, data=data )) as engine:
2279-
for i,(m,s) in enumerate( engine ):
2282+
with contextlib.closing( machine.run( source=source, data=data.request )) as engine:
2283+
for m,s in engine:
22802284
pass
2281-
#log.detail( "%s #%3d -> %10.10s; next byte %3d: %-10.10r: %s",
2282-
# machine.name_centered(), i, s, source.sent, source.peek(),
2283-
# repr( data ) if log.getEffectiveLevel() < logging.DETAIL else misc.reprlib.repr( data ))
2285+
# for i,(m,s) in enumerate( engine ):
2286+
# log.detail( "%s #%3d -> %10.10s; next byte %3d: %-10.10r: %s",
2287+
# machine.name_centered(), i, s, source.sent, source.peek(),
2288+
# repr( data ) if log.getEffectiveLevel() < logging.DETAIL else misc.reprlib.repr( data ))
22842289

22852290
target.request( data.request, addr=addr )
22862291
except:

server/enip/logix.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
3232
"""
3333

34+
import contextlib
3435
import json
3536
import logging
3637
import sys
@@ -958,11 +959,13 @@ def process( addr, data, **kwds ):
958959
if 'input' in data.request.enip:
959960
source.chain( data.request.enip.input )
960961
with ucmm.parser as machine:
961-
for i,(m,s) in enumerate( machine.run( path='request.enip', source=source, data=data )):
962-
#log.detail( "%s #%3d -> %10.10s; next byte %3d: %-10.10r: %s",
963-
# machine.name_centered(), i, s, source.sent, source.peek(),
964-
# repr( data ) if log.getEffectiveLevel() < logging.DETAIL else misc.reprlib.repr( data ))
965-
pass
962+
with contextlib.closing( machine.run( source=source, data=data.request.enip )) as engine:
963+
for m,s in engine:
964+
pass
965+
# for i,(m,s) in enumerate( engine ):
966+
# log.detail( "%s #%3d -> %10.10s; next byte %3d: %-10.10r: %s",
967+
# machine.name_centered(), i, s, source.sent, source.peek(),
968+
# repr( data ) if log.getEffectiveLevel() < logging.DETAIL else misc.reprlib.repr( data ))
966969
if log.isEnabledFor( logging.DETAIL ):
967970
log.detail( "EtherNet/IP CIP Request (Client %16s): %s", addr, enip_format( data.request ))
968971

server/enip/main.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -635,9 +635,7 @@ def enip_srv_udp( conn, name, enip_process, **kwds ):
635635
# Exception (dfa exits in non-terminal state). Build data.request.enip:
636636
begun = misc.timer() # waiting for next transaction
637637
addr,stats = None,None
638-
with contextlib.closing( machine.run(
639-
path='request', source=source, data=data )) as engine:
640-
# PyPy compatibility; avoid deferred destruction of generators
638+
with contextlib.closing( machine.run( source=source, data=data.request )) as engine:
641639
for mch,sta in engine:
642640
if sta is not None:
643641
# No more transitions available. Wait for input.
@@ -738,9 +736,7 @@ def enip_srv_tcp( conn, addr, name, enip_process, delay=None, **kwds ):
738736
# If no/partial EtherNet/IP header received, parsing will fail with a NonTerminal
739737
# Exception (dfa exits in non-terminal state). Build data.request.enip:
740738
begun = misc.timer()
741-
with contextlib.closing( machine.run(
742-
path='request', source=source, data=data )) as engine:
743-
# PyPy compatibility; avoid deferred destruction of generators
739+
with contextlib.closing( machine.run( source=source, data=data.request )) as engine:
744740
for mch,sta in engine:
745741
if sta is not None:
746742
continue

server/enip/parser.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1891,6 +1891,7 @@ def __init__( self, name=None, **kwds ):
18911891
slct = octets_noop( 'sel_CIP' )
18921892
for cmd,cls in self.COMMAND_PARSERS.items():
18931893

1894+
'''
18941895
def recog_CIP( path=None, data=None, cmd=cmd, **kwds ):
18951896
found = data[path+'..command'] in cmd
18961897
log.isEnabledFor( logging.INFO ) and log.info(
@@ -1902,12 +1903,12 @@ def recog_CIP( path=None, data=None, cmd=cmd, **kwds ):
19021903
enip_format( data ))
19031904
)
19041905
return data[path+'..command'] in cmd
1905-
1906+
'''
19061907
slct[None] = decide(
19071908
cls.__name__,
19081909
state = cls( limit='...length', terminal=True ),
1909-
predicate = recog_CIP,
1910-
#predicate = lambda path=None, data=None, cmd=cmd, **kwds: data[path+'..command'] in cmd
1910+
#predicate = recog_CIP,
1911+
predicate = lambda path=None, data=None, cmd=cmd, **kwds: data[path+'..command'] in cmd,
19111912
)
19121913

19131914
def unrec_CIP( path=None, data=None, **kwds ):

0 commit comments

Comments
 (0)