public class Chat extends ChatEventSource
Modifier and Type | Class and Description |
---|---|
class |
Chat.Listener
Runnable encapsulating listener task
|
class |
Chat.Message |
ChatEventSource.ChatEvent, ChatEventSource.ChatEventType
Modifier and Type | Field and Description |
---|---|
private java.util.Map<java.lang.Integer,java.lang.String> |
clients |
private Context |
context |
private int |
index |
private int |
lamport |
private int |
lastLamportDelivered |
static int |
MESSAGE_TIMEOUT |
private java.util.PriorityQueue<Chat.Message> |
messageQueue |
static int |
RECEIVE_BUFFER_SIZE |
private Handler |
receiveHandler |
private boolean |
registered |
private Handler |
requestHandler |
(package private) boolean |
requestPending |
(package private) boolean |
requestResult |
(package private) java.lang.String |
requestResultMessage |
(package private) long |
requestTimestamp |
static int |
RESPONSE_TIMEOUT |
static java.lang.String |
SERVER_ADDRESS |
static int |
SERVER_PORT |
private java.net.DatagramSocket |
socket |
static int |
SOCKET_TIMEOUT |
private java.util.Map<java.lang.Integer,java.lang.Integer> |
timeVector |
eventListenerList
Constructor and Description |
---|
Chat(Context context) |
Modifier and Type | Method and Description |
---|---|
static void |
chatLog(java.lang.String msg) |
java.util.Map<java.lang.Integer,java.lang.String> |
clients() |
static java.lang.String |
clientsToString(java.util.Map<java.lang.Integer,java.lang.String> clients) |
private boolean |
connect()
Open connection to chat server
|
boolean |
connected() |
static void |
debugLog(java.lang.String msg) |
void |
deregister()
Try to deregister with the server
|
protected void |
finalize() |
void |
getClients()
Update the clients list and inform listeners
|
void |
getInfo()
Get information about the server
|
private boolean |
isDeliverable(Chat.Message message)
Determines whether a message is deliverable
|
static java.util.Map<java.lang.Integer,java.lang.String> |
parseClientArray(JSONObject json) |
static java.util.Map<java.lang.Integer,java.lang.Integer> |
parseTimeVector(JSONObject json) |
void |
register(java.lang.String username)
Try to register with the chat server
|
boolean |
registered() |
void |
send(java.lang.String message)
Send a message to the chat server
|
private void |
sendGetClients()
Sends get_clients request to server
|
private void |
sendGetInfo()
Sends Information request to the server
|
private void |
sendMessage(java.lang.String message,
java.util.Map<java.lang.Integer,java.lang.Integer> timeVector,
int lamport)
Sends a message send request to the chat server
|
private void |
sendRegister(java.lang.String username)
Sends registration request to the server
|
private void |
sendRequest(JSONObject request)
Sends a JSONObject to the server
|
private void |
sendUnregister()
Sends deregister request to the server
|
static JSONObject |
TimeVectorToJSON(java.util.Map<java.lang.Integer,java.lang.Integer> timeVector) |
private void |
updateClients()
Update clients list
|
addChatEventListener, dispatchEvent, removeChatEventListener
public static final java.lang.String SERVER_ADDRESS
public static final int SERVER_PORT
public static final int RECEIVE_BUFFER_SIZE
public static final int SOCKET_TIMEOUT
public static final int RESPONSE_TIMEOUT
public static final int MESSAGE_TIMEOUT
private Context context
private java.net.DatagramSocket socket
private int index
private int lamport
private int lastLamportDelivered
private java.util.Map<java.lang.Integer,java.lang.Integer> timeVector
private java.util.Map<java.lang.Integer,java.lang.String> clients
private java.util.PriorityQueue<Chat.Message> messageQueue
private boolean registered
private Handler requestHandler
private Handler receiveHandler
boolean requestPending
long requestTimestamp
boolean requestResult
java.lang.String requestResultMessage
public java.util.Map<java.lang.Integer,java.lang.String> clients()
public boolean connected()
public boolean registered()
protected void finalize() throws java.lang.Throwable
finalize
in class java.lang.Object
java.lang.Throwable
public void register(java.lang.String username)
username
- user name to register withpublic void deregister()
public void getClients()
public void getInfo()
public void send(java.lang.String message)
message
- message to sendprivate void updateClients()
private boolean connect()
private void sendRequest(JSONObject request) throws java.io.IOException
request
- The JSONObject
to sendjava.io.IOException
private void sendRegister(java.lang.String username) throws java.io.IOException
username
- The username to register withjava.io.IOException
private void sendUnregister() throws java.io.IOException
java.io.IOException
private void sendGetClients() throws java.io.IOException
java.io.IOException
private void sendGetInfo() throws java.io.IOException
java.io.IOException
private void sendMessage(java.lang.String message, java.util.Map<java.lang.Integer,java.lang.Integer> timeVector, int lamport) throws java.io.IOException
message
- The message to sendtimeVector
- Vector Time when sending the messagelamport
- Lamport timestamp when sending the messagejava.io.IOException
private boolean isDeliverable(Chat.Message message)
message
- message in questiontrue
if message is deliverable false
if message should be delayedpublic static java.util.Map<java.lang.Integer,java.lang.String> parseClientArray(JSONObject json)
public static java.lang.String clientsToString(java.util.Map<java.lang.Integer,java.lang.String> clients)
public static java.util.Map<java.lang.Integer,java.lang.Integer> parseTimeVector(JSONObject json)
public static JSONObject TimeVectorToJSON(java.util.Map<java.lang.Integer,java.lang.Integer> timeVector)
public static void chatLog(java.lang.String msg)
public static void debugLog(java.lang.String msg)