NetworkDesign: Difference between revisions

Jump to navigation Jump to search
Content deleted Content added
No edit summary
No edit summary
Line 36: Line 36:


Receiving the message is a bit more complex. First we need to determine that we are running a compatible version of the protocol. Once we have agreed that the protocol is compatible we read the type of message and we ask a Message Factory ( "Java Design Patterns" ) to build an object of that type with the coresponding data. Once the message is build we simply store it in a queue of incoming messages waiting to be processed.
Receiving the message is a bit more complex. First we need to determine that we are running a compatible version of the protocol. Once we have agreed that the protocol is compatible we read the type of message and we ask a Message Factory ( "Java Design Patterns" ) to build an object of that type with the coresponding data. Once the message is build we simply store it in a queue of incoming messages waiting to be processed.

=Network Manager=
The Network Manager is our router that sends and receives messages to and from the network. The manager exposes the interfaces that allow:
*Reading a message from the network
*Sending a message to the network
*Finalizing the manager

The read operation is a blocking type operation so we have two options, either Polling and Blocking. We choose Blocking because we don't want to waste CPU time Polling the network for messages, we just want to sleep until messages are available. Hence we create a Thread to read from the Network, let's call it Network Manager Read.

To write messages to the network so we can safely code it simply as a method of Network Manager as write is an operation that is non blocking by nature.

The Network Manager opens a Socket from which it will receive all the messages from the network, and to where it will write all the messages to the network. Both write and read use the same Socket.

To encapsulate all this we create both the Read and Write methods as inner classes of Network Manager.

<pre>
NetworkManager
{
socket
messages
pendingToSendMessages

NetworkManagerRead isa Thread
{
read socket
build message
store in messages
}

NetworkManagerWrite isa Thread
{
get from pendingToSendMessages
build UDP from message
send socket
}
}
</pre>

As you can see, messages are stored in a list when they are received, hence access to the list is synchronized.

Now lets get back to the interface as exposed to other objects. The Write method is immediate, just call it with the message to send, making sure that you have correctly filled SourceAddress and ClientID, and it will be sent to the Client.

It is important to notice that as the transport is based on UDP there are no guarantees that the message is correctly sent. This situation is in the protocol itself.

The Read method is blocking, when you call the Read method it either returns a message from the queue or if the queue is empty the thread blocks until one arrives.

That is the basic idea of the Network Manager; however, the manager gets a bit more complex as a result of the need to support Server to Client messages that are bigger than one UDP package, the manager just sends the stream of packets once, and doesn't confirm if any of the messages are received. Please refer to UDP Packet Format for more info about it.



=Message Types=
=Message Types=
Line 183: Line 231:


The message is composed of: An array of TransferContent objects containing all the name of each resource, its timestamp, a flag indicating if the resource is cacheable or not and a byte array with the content itself.
The message is composed of: An array of TransferContent objects containing all the name of each resource, its timestamp, a flag indicating if the resource is cacheable or not and a byte array with the content itself.



=Network Manager=
The Network Manager is our router that sends and receives messages to and from the network. The manager exposes the interfaces that allow:
*Reading a message from the network
*Sending a message to the network
*Finalizing the manager

The read operation is a blocking type operation so we have two options, either Polling and Blocking. We choose Blocking because we don't want to waste CPU time Polling the network for messages, we just want to sleep until messages are available. Hence we create a Thread to read from the Network, let's call it Network Manager Read.

To write messages to the network so we can safely code it simply as a method of Network Manager as write is an operation that is non blocking by nature.

The Network Manager opens a Socket from which it will receive all the messages from the network, and to where it will write all the messages to the network. Both write and read use the same Socket.

To encapsulate all this we create both the Read and Write methods as inner classes of Network Manager.

<pre>
NetworkManager
{
socket
messages
pendingToSendMessages

NetworkManagerRead isa Thread
{
read socket
build message
store in messages
}

NetworkManagerWrite isa Thread
{
get from pendingToSendMessages
build UDP from message
send socket
}
}
</pre>

As you can see, messages are stored in a list when they are received, hence access to the list is synchronized.

Now lets get back to the interface as exposed to other objects. The Write method is immediate, just call it with the message to send, making sure that you have correctly filled SourceAddress and ClientID, and it will be sent to the Client.

It is important to notice that as the transport is based on UDP there are no guarantees that the message is correctly sent. This situation is in the protocol itself.

The Read method is blocking, when you call the Read method it either returns a message from the queue or if the queue is empty the thread blocks until one arrives.

That is the basic idea of the Network Manager; however, the manager gets a bit more complex as a result of the need to support Server to Client messages that are bigger than one UDP package, the manager just sends the stream of packets once, and doesn't confirm if any of the messages are received. Please refer to UDP Packet Format for more info about it.



=Versioning and port numbering=
=Versioning and port numbering=