Thanx for your quick replies Saluk and Blender_owl.
Regarding the check() function, in C++ a 2-dimensional array is like a table with columns and rows (or a matrix in 3d maths).
this is a table 10x10 which is 2-dimensional:
this is a one-dimensional list of 10 elements:
Is it the same in python?
Saluk said this:
Yeah, I think object oriented might be the right way to go. In fact I’m not sure if we really need the dualsocket code at all, rather a udpsocket and a tcpsocket module and use them both in the server and class. But I’m not really sure how to go about it.
I would say that the dualsocket code is fine except it is not very extendable or re-usable. What if you wanted to add another protocol other than UDP or TCP? You would have to go through the code and change every function. The question is - would it EVER be feasible to have more protocols than UDP or TCP, or maybe a different implementation of UDP, TCP at some stage in the future? If not, then there’s not much point in complicating the code with OOP. If the answer is yes then we need to modify the code which brings me nicely to blender_owls question!
I’m not sure about the patterns STRATEGY and DECORATOR. Perhaps I am not as familiar with OOP as i though i was. But go ahead. Write a demonstration. I think this project should be as object orientated as possible. Perhaps you can explain to me why we need 3 classes, I am not sure why you would split the dualsocket into 3.
Forget the DECORATOR, sorry about that! But using the Strategy design pattern we can provide a class that supports UDP and TCP and it will be easy to add new protocols if ever we want to!
For more info about the pattern type “Strategy Design Pattern” into Goggle and you will get loads of info. Design Patterns are just a way of tackling well-known problems in an object-oriented way and for our problem the Strategy is perfect.
Put simply we can have a Connection or Link class (or something) that provides the interface. link.send() link.receive(), etc. The link class contains a reference to a protocol which is initialised when the object is created. All methods, such as send() and receive() delegate to the protocol:
def __init__(self, protocol)
self.protocol = protocol
protocol is a reference to the Protocol class which is the interface for the different protocols:
#send a tcp socket here
#send a udp socket here
All the client would do is this:
udpSocket = UDPSocket
link = Link(udpSocket)
or to get the dualsocket behaviour:
sockets = [ UDPSocket, TCPSocket ]
sockets.send(data) #send udp
sockets.send(data) #send tcp
Sorry about my python - its crap. Hope you can understand it!
Although it may sound quite over the top it is actually quite simple to use in practise because all coding is done in logical black boxes. To receive a UDP message, add code to UDPSocket.receive(). To connect to a TCP socket, add code to TCPSocket.connect().
Also, it is extendable. We could add a SerialComms protocol and write the send, recevie, connect, close methods and it would work without chaning any existing code.
I will get my python book out and start learning it properly (I am reading lots of books and learning it well, but not wrote any scripts yet as you can probably tell!). Over the weekend I will try to get version working and let you guys know how I go on. In the meantime, any thoughts?