Skip to content →

Multiple Sessions

A SessionManager' is used to manage all the sessions. The SessionManager’ keeps a list of sessions. When it receives data, it will first check which session the data belongs to. Then the data is given to the corresponding session. If it does not belong to any session in the list, a new session will be created and added to the session list.

The basic code looks like this:


import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Daoyuan
 */
public class SessionManager extends Thread {

String user;
    String host;
    int port;
    String voiceFile;
    String defaultVoice;
    boolean listening = true;
    public static ArrayList sessionList;
    DatagramSocket serverSocket = null;

    public SessionManager(String user, String host, int port, String voiceFile,
            String defaultVoice) {
        this.user = user;
        this.host = host;
        this.port = port;
        this.voiceFile = voiceFile;
this.defaultVoice = defaultVoice;
        sessionList = new ArrayList();
        try {
            serverSocket = new DatagramSocket(port);
        } catch (SocketException ex) {
            Logger.getLogger(SessionManager.class.getName()).log(Level.SEVERE, null, ex);
            System.out.println("Unable to listen on port " + port);
        }
    }

    @Override
    public void run() {

        byte[] receiveData = newbyte[2048];

        new Thread() {

            @Override
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(5);
                    } catch (InterruptedException ex) {
                        Logger.getLogger(SessionManager.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    if (sessionList.size() != 0) {
                        for (int i= 0; i < sessionList.size(); i++) {
                            if (sessionList.get(i).status.equals("destroyed")) {
                                sessionList.remove(i);
                            }
                        }
                    }
                }
            }
        }.start();

        while (listening) {
            DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
try {
                serverSocket.receive(receivePacket);
            } catch (IOException ex) {
                Logger.getLogger(SessionManager.class.getName()).log(Level.SEVERE, null, ex);
            }

            if (isNewSession(receivePacket)) {
                System.out.println("New session created!");
            }
        }

        serverSocket.close();
    }

    public void stopListening() {
        listening = false;
}

    private boolean isNewSession(DatagramPacket receivePacket) {
        InetAddress IPAddress = receivePacket.getAddress();
        int remotePort = receivePacket.getPort();
        String sessionName = IPAddress + ":" + remotePort;
        if (sessionList.size() == 0) {
            Session s = new Session(this.serverSocket, IPAddress, remotePort, voiceFile, defaultVoice, user);
            s.setName(sessionName);
s.requests.add(receivePacket);
            sessionList.add(s);
            s.start();
            return true;
        } else {
            for (int i = 0; i < sessionList.size(); i++) {
                if (sessionList.get(i).getName().equals(sessionName)) {
                    sessionList.get(i).requests.add(receivePacket);
                    return false;
                }
            }

            Session s = newSession(this.serverSocket, IPAddress, remotePort, voiceFile, defaultVoice, user);
            s.setName(sessionName);
            s.requests.add(receivePacket);
            sessionList.add(s);
            s.start();
            return true;
        }
    }
}

Published in SIP Speaker

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.