Android Open Source - schat S Chat Client Listener






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  ww .j av a 2 s .c  om*/
import crypto.Envelope;
import data.User;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.Socket;

/**
 * It will be listening to the server and
 * notify the user if the server has a new message
 * for him.
 *
 * @author Gary Ye
 */
public class SChatClientListener extends Thread {
    private Socket socket;
    private User receiver;

    /**
     * Create a new listener thread, which is constantly listening
     * to the server.
     *
     * @param socket   the socket to listen to
     * @param receiver the receiver or the client to handle
     * @throws IOException
     */
    public SChatClientListener(Socket socket, User receiver) throws IOException {
        this.socket = socket;
        this.receiver = receiver;
    }

    public void run() {
        boolean isRunning = true;
        ObjectInputStream in = null;
        try {
            in = new ObjectInputStream(socket.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
        while (isRunning) {
            try {
                Envelope envelope = (Envelope) in.readObject();
                switch (envelope.getType()) {
                    case CHAT_MESSAGE:
                        receiver.receiveMessage(envelope);
                        break;
                    case PUBLIC_KEY_RESPONSE:
                        receiver.registerUser(envelope);
                        break;
                    case LOGIN_SUCCESS:
                        break;
                    default:
                        System.err.println("Unknown envelope type!");
                        isRunning = false;
                        break;
                }
            } catch (IOException e) {
                System.err.println("Connection to the server closed.");
                break;
            } catch (ClassNotFoundException e) {
                System.err.println("Class not found: " + e.getMessage());
                break;
            }
        }

        try {
            in.close();
        } catch (IOException ignored) {
        }
        try {
            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * Notify the user with the received envelope
     *
     * @param envelope the envelope
     */
    public void notifyUser(Envelope envelope) {
        receiver.receiveMessage(envelope);
    }
}




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