Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Design recommendation wanted.

Reply
Thread Tools

Design recommendation wanted.

 
 
Yannick Turgeon
Guest
Posts: n/a
 
      09-23-2004
Hello all,

I'm implementing a protocol to communicate with a server through serial port
(Yes Peter and Alex it's me again!). This means looking for lost packet,
checksum error, connection failure.

Whitout error, the normal course of things is:
(C = Client - the one I'm programming, S = Server)

Type #1 command:
C: Send a packet to ask something
S: Reply with the final answer
C: Acknowledge reception

Type #2 command:
C: Send a packet to ask something
S: Reply with part of the answer
C: Acknowledge reception of the partial answer
S: Reply with the following part of the answer
C: Acknowledge reception of the partial answer
....
S: Confirm the end of the answer
C: Acknowledge reception

Added to that, the server can send me an error packet due to my last sent
packet being lost or checksum error or packet wrongly construct or
impossibility to answer my "correctly constructed and received question".
It includs in this error whether or not I should resend the last packet.
Even more, there is a fixed maximum number of tentative to resent a packet.

So my question is a general design one:
Anybody has any hint how to handle in a well structured way that much
possible errors and situation? How?

Say I got 20 commands I can ask to server (which is near the real
situation).

I tried to make a base Command class that has a function that looks like:

def getReply(expected):
try:
packet = serial.readline() #raise TimeoutError()
packet_type, data = decode(packet) #raise FormatPacketError()
# ChecksumError()
# PacketNumberError()
if packet_type == ERROR:
raise ServerReplyError()
if packet_type <> expected:
raise UnexpectedPacketError()
c = PACKETS[packet_type](data) #PACKETS is a dict containing Classes
#raise DataFormatError()
expect TimeoutError:
blabla
expect FormatPacketError:
blabla
expect ChecksumError:
blabla
expect PacketNumberError:
blabla
expect UnexpectedPacketError:
blabla
expect ServerReplyError:
blabla
expect DataFormatError:
blabla


With this solution, I will get lost very soon. I feel that I would need a
kind of State-Managing system/class that would keep track of the command
processing in general. But I'm not grasping it. I'm conscious that by not
knowing the total situation, it's difficult for anybody to help but any
suggestion, recommendation would be much appreciated.

Yannick
 
Reply With Quote
 
 
 
 
Istvan Albert
Guest
Posts: n/a
 
      09-24-2004
Yannick Turgeon wrote:

> With this solution, I will get lost very soon. I feel that I would need a
> kind of State-Managing system/class that would keep track of the command


It would simplify your life if you dealt with
the problems (that can be dealt with) on the spot, as they
happen. Don't use the exceptions as a mechnism to jump out of a
block of code. This is so much easier to follow:

if packet_type == ERROR1:
...deal with it..
elif packet_type == ERROR2:
...deal with it ...
else:
try:
c = PACKETS[packet_type](data)
except ...:
...deal with this


You don't have to have to put your code in one try/except
block. This only depens on the nature of the response that
you want to invoke when an exeption occurs. Feel free to nest them
as needed. Keep the error and the response to it as one logical
entity as much as possible.

Istvan.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Need Recommendation for VGA hardware design books everkids VHDL 1 07-30-2007 02:24 PM
Web design program recommendation wanted for schools jzkilmer@yahoo.com HTML 17 05-05-2006 12:00 AM
Recommendation for good book for layout and design for web pages John Dalberg HTML 4 07-05-2004 12:53 PM
design pattern book recommendation Yamin C++ 3 09-25-2003 02:41 PM
Design book recommendation Csaba2000 HTML 4 07-17-2003 12:15 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57