|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectmcast.MCastSocket
public class MCastSocket
MCastSocket: implements basic multicast transport. Is responsible for (a) the join protocol (via the lobby), and (b) effecting multicast (as repeated unicast).
The MCastSocket is multithreaded. When created, a thread is started that sits in a loop trying to read packets off the network, and putting them into a receive buffer. The client thread takes them out of that queue, by calling receive(). The client thread may also send packets.
Nested Class Summary | |
---|---|
private class |
MCastSocket.ReceiveHelper
Helper class to implement receiving thread. |
Field Summary | |
---|---|
private static int |
BUFFER_SIZE
Size of inComingPktBuf. |
static int |
CLOSED
Socket state All clients have left the group. |
static int |
CONNECTED
Socket state Socket is part of Mcast group (has heard a SERVER_CLIENT_HELLO and now knows who all is in the mcast group). |
static int |
DRAINED
Socket state There are no more packets in the received queue, and none on the network (i.e., there will never again be one available for receiving). |
private java.util.ArrayList<java.net.InetSocketAddress> |
groupMembers
Addresses of mcast group members. |
private java.util.concurrent.ArrayBlockingQueue<MCastPacket> |
inComingPktBuf
The queue of packets available for the client to receive(). |
private java.net.InetSocketAddress |
lobbyAddress
Location of the lobby (provider by creator of this object). |
private java.net.DatagramSocket |
mySocket
The socket actually used to send and receive to/from the network. |
private int |
numDeparted
Number of clients who have departed (i.e., promised they will send no more packets to the mcast group). |
private java.lang.Thread |
receiver
The receive thread (takes packets off network and puts them in a packet buffer). |
static int |
STARTING
Socket state Initial state: not yet connected. |
private java.util.concurrent.Semaphore |
startSem
Use to blocking user thread, in a call to join(), until receive thread notices that the mcast group has been joined. |
private int |
state
Current state of the socket. |
Constructor Summary | |
---|---|
MCastSocket()
Creates a new MCastSocket, obtaining an ephemeral (random) port number. |
|
MCastSocket(int port)
Creates a new MCastSocket with a specified port number. |
Method Summary | |
---|---|
int |
getClientId(MCastPacket pkt)
Returns a "client id" - an integer in the range 0..getMCastGroupSize()-1. |
int |
getMCastGroupSize()
Returns the number of clients in the mcast group. |
private void |
init()
Private function to initialize (called by constructors). |
void |
join(java.lang.String lobbyHost,
int lobbyPort)
Blocks the calling thread until the mcast group is joined. |
void |
leave()
leave() is called by client when it's done sending. |
MCastPacket |
receive()
|
void |
send(MCastPacket pkt)
This is multicast send. |
private void |
unicastSendTo(MCastPacket pkt,
java.net.InetSocketAddress addr)
Private unicast send helper function. |
private void |
updateMCastClientList(java.lang.String clientList)
Accept mcast client group message from lobby, parse it, can fill in group member data structure. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final int STARTING
public static final int CONNECTED
public static final int CLOSED
public static final int DRAINED
private int state
private java.util.concurrent.Semaphore startSem
private java.net.DatagramSocket mySocket
private java.util.concurrent.ArrayBlockingQueue<MCastPacket> inComingPktBuf
private static final int BUFFER_SIZE
private java.lang.Thread receiver
private java.net.InetSocketAddress lobbyAddress
private java.util.ArrayList<java.net.InetSocketAddress> groupMembers
private int numDeparted
Constructor Detail |
---|
public MCastSocket() throws java.net.SocketException
java.net.SocketException
public MCastSocket(int port) throws java.net.SocketException
inbufSize
- allows the caller to say how big the incoming packet buffer should be.port
- the port number
java.net.SocketException
Method Detail |
---|
private void init()
public int getClientId(MCastPacket pkt)
The client id is not transmitted in the packet. The sender's host IP address and port are looked up in the list of mcast group clients, and the index in that list is the id.
pkt
- The id of the sender of this packet is the value returned.public int getMCastGroupSize()
public void join(java.lang.String lobbyHost, int lobbyPort) throws java.net.UnknownHostException, java.io.IOException
lobbyHost
- A string containing the lobby's host name or IP address.lobbyPort
- The lobby's port.
java.net.UnknownHostException
java.io.IOException
public void leave() throws java.io.IOException, java.lang.InterruptedException
java.io.IOException
java.lang.InterruptedException
public void send(MCastPacket pkt) throws java.io.IOException
java.io.IOException
private void unicastSendTo(MCastPacket pkt, java.net.InetSocketAddress addr) throws java.io.IOException
java.io.IOException
public MCastPacket receive()
private void updateMCastClientList(java.lang.String clientList)
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |