Android Open Source - schat S Chat Client






From Project

Back to project page schat.

License

The source code is released under:

MIT License

If you think the Android project schat listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package networking;
//  w  w  w  .  jav a 2s . c o m
import crypto.Cryptography;
import crypto.Envelope;
import data.Content;
import data.DatabaseManager;
import data.Message;
import data.User;
import data.contents.ChatContent;
import data.contents.Login;
import data.contents.PublicKeyRequest;
import data.contents.Registration;

import java.io.IOException;
import java.net.Socket;
import java.util.Calendar;

/**
 * This class manages the network communication between the client and the server
 * from the client's point of view.
 *
 * @author Gary Ye
 */
public class SChatClient extends Thread {
    private User client;
    private Socket socket;
    private SChatClientListener listener;
    private SChatClientWriter sender;
    private DatabaseManager manager;

    /**
     * Generate a SChatClient, which can listen and send messages
     * simultaneously.
     *
     * @param client the client to handle
     * @throws IOException
     */
    public SChatClient(User client, DatabaseManager manager) throws IOException {
        this(client, SChatServer.SERVER_NAME, SChatServer.PORT_ADDRESS);
    }

    /**
     * Generate a SChatClient, which can listen and send messages
     * simultaneously. It is going to interact with the given server.
     *
     * @param client     the client
     * @param hostName   the host name of the server
     * @param portNumber the port number
     * @throws IOException
     */
    public SChatClient(User client, String hostName, int portNumber) throws IOException {
        this(client, hostName, portNumber, null);
    }

    /**
     * Generate a SChatClient, which can listen and send messages
     * simultaneously. It is going to interact with the given server.
     *
     * @param client     the client
     * @param hostName   the host name of the server
     * @param portNumber the port number
     * @param manager    the database manager
     * @throws IOException
     */
    public SChatClient(User client, String hostName, int portNumber, DatabaseManager manager) throws IOException {
        this.client = client;
        this.socket = new Socket(hostName, portNumber);
        this.listener = new SChatClientListener(socket, client);
        this.sender = new SChatClientWriter(socket);
        this.manager = manager;
        listener.start();
    }

    public boolean isConnected() {
        if(socket != null)
            return socket.isConnected() && !socket.isClosed();
        else
            return false;
    }

    /**
     * Send the given ChatContent to the server with the given
     * receiver id.
     *
     * @param chatContent the chat content
     * @param receiverId  the receiver id
     */
    public void sendMessage(ChatContent chatContent, String receiverId) {
        Message<ChatContent> message = new Message<>(Calendar.getInstance().getTime(), client.getId(), receiverId, chatContent);
        sender.send(encrypt(message));
    }

    /**
     * Try to login to the server
     *
     * @throws IOException
     */
    public void loginToServer() throws IOException {
        Message<Login> loginMessage = new Message<Login>(Calendar.getInstance().getTime(), client.getId(), SChatServer.SERVER_ID,
                new Login(client.getId()));
        sender.send(encrypt(loginMessage));
    }

    public void registerToServer() throws IOException {
        Message<Registration> registrationMessage =
                new Message<Registration>(
                        Calendar.getInstance().getTime(),
                        client.getId(),
                        SChatServer.SERVER_ID,
                        new Registration(new Login(client.getId()), client.getKeyPair().getPublic())
                );
        sender.send(encrypt(registrationMessage));
    }

    /**
     * Envelope a message
     *
     * @param message the message
     * @return an enveloped message
     */
    public Envelope encrypt(Message<? extends Content> message) {
        String receiverId = message.getReceiver();
        User receiver = manager.getUserFromGivenId(receiverId);
        return new Envelope(message, Cryptography.gen_symm_key(), receiver.getPublicKey(), client.getKeyPair().getPrivate());
    }

    public void sendPublicKeyRequest(String id) {
        Message<PublicKeyRequest> loginMessage = new Message<PublicKeyRequest>(Calendar.getInstance().getTime(), client.getId(), SChatServer.SERVER_ID,
                new PublicKeyRequest(id));
        sender.send(encrypt(loginMessage));
    }
}




Java Source Code List

.ServerMain.java
com.activities.Activity_Chat.java
com.activities.Activity_ContactList.java
com.activities.ContactView.java
com.data.AddContact.java
com.data.AndroidSQLManager.java
com.data.ApplicationUser.java
com.data.ChatAdapter.java
com.data.ChatArrayList.java
com.data.MySQLiteHelper.java
com.security.AndroidKeyPairManager.java
com.security.PRNGFixes.java
com.services.MessageService.java
crypto.CryptoConstants.java
crypto.Cryptography.java
crypto.Envelope.java
crypto.SecureMessage.java
crypto.TestCrypto.java
data.ChatMessage.java
data.Content.java
data.DatabaseManager.java
data.KeyPairManager.java
data.Message.java
data.SQLiteManager.java
data.User.java
data.contents.ChatContent.java
data.contents.LoginSuccess.java
data.contents.Login.java
data.contents.PublicKeyRequest.java
data.contents.PublicKeyResponse.java
data.contents.Registration.java
networking.SChatClientListener.java
networking.SChatClientWriter.java
networking.SChatClient.java
networking.SChatServerThread.java
networking.SChatServer.java