Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write an application using your preferred programming language to implement a si

ID: 3773757 • Letter: W

Question

Write an application using your preferred programming language to implement a simple chat system through socket programming using the following specifications.

The application is implemented using a module named “p2pchat”

The application uses TCP as the transport protocol.

The p2pchat module works as both client and server using multithreading to allow for sending and receiving at the same time.

When the p2pchat module starts using the command “p2pchat”, it listens on port “5789” for incoming connections on a separate thread (listening thread).

If the module starts using the command “p2pchat [ip-address]”, it performs the start procedure (i.e. listens on the 5789 port), and then initiates a connection with another p2pchat with the IP address given in the online command on a separate thread (sending thread).

When the user writes a text on any p2pchat peer and presses enter, the text message appears on the other p2pchat peer window.

Bonus: 2 bonus marks will be given to students who can add the support for non-text chat to the p2pchat program e.g. sending special symbols, sending a file, sending image, etc.

Explanation / Answer

package p2p.web.businessobject;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import p2p.web.dataaccess.ServerSettingsDAO;
import p2p.web.datatransferobject.ServerSettingsDTO;
import p2p.web.domainmodel.ServerSettings;

/**
* @author Sergey
*
*         28.11.2008
*
*/
@Service("serverSettingsBO")
public class ServerSettingsBO {

        private ServerSettingsDAO serverSettingsDAO;

        /**
         * @param serverSettingsDAO
         */
        @Autowired
        public ServerSettingsBO(ServerSettingsDAO serverSettingsDAO) {
                this.serverSettingsDAO = serverSettingsDAO;
        }

        /**
         * @return
         */
        public ServerSettingsDTO getServerSettings() {
                List<ServerSettings> serverSettingsList =
serverSettingsDAO.getServerSettings();
                ServerSettingsDTO serverSettingsDTO = new ServerSettingsDTO();
                for (ServerSettings serverSettings : serverSettingsList) {
                        if
(serverSettings.getSettingVar().equals(ServerSettings.SEED_HOME_NAME)) {
                                
serverSettingsDTO.setWebAddress(serverSettings.getSettingVal());
                        }
                        if
(serverSettings.getSettingVar().equals(ServerSettings.SEED_PORT)) {
                                
serverSettingsDTO.setWebPort(serverSettings.getSettingVal());
                        }
                }
                
serverSettingsDTO.setProtocols(serverSettingsDAO.getProtocols());
                return serverSettingsDTO;
        }

        /**
         * @param serverSettings
         */
        public void saveServerSettings(ServerSettingsDTO serverSettingsDTO) {
                List<ServerSettings> serverSettingsList = new
ArrayList<ServerSettings>();

                ServerSettings serverSettings = new ServerSettings();
                serverSettings.setSettingVar(ServerSettings.SEED_HOME_NAME);
                
serverSettings.setSettingVal(serverSettingsDTO.getWebAddress());
                serverSettingsList.add(serverSettings);

                serverSettings = new ServerSettings();
                serverSettings.setSettingVar(ServerSettings.SEED_PORT);
                serverSettings.setSettingVal(serverSettingsDTO.getWebPort());
                serverSettingsList.add(serverSettings);

                serverSettings = new ServerSettings();
                
serverSettings.setSettingVar(ServerSettings.SEED_HOME_PROTOCOL);
                
serverSettings.setSettingVal(serverSettingsDTO.getWebProtocol());
                serverSettingsList.add(serverSettings);

                serverSettingsDAO.saveServerSettings(serverSettingsList);
        }

}

File [added]: UserDetailsBO.java


package p2p.web.businessobject;

import org.acegisecurity.GrantedAuthority;
import org.acegisecurity.GrantedAuthorityImpl;
import org.acegisecurity.userdetails.UserDetails;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import p2p.web.dataaccess.UserDetailsDAO;
import p2p.web.datatransferobject.UserAccauntDTO;
import p2p.web.domainmodel.UserAccount;

/**
* @author S.Timofiychuk
*
* 08.12.2008
*
*/
@Service("userDetailsBO")
public class UserDetailsBO {

        private UserDetailsDAO userDetailsDAO;

        @Autowired
        public UserDetailsBO(UserDetailsDAO userDetailsDAO) {
                this.userDetailsDAO = userDetailsDAO;
        }

        /**
         * @param name
         * @return
         */
        public UserDetails loadUserByUsername(String name) {
                UserAccount userAccount = null;
                try {
                        userAccount = userDetailsDAO.loadUserByUsername(name);
                } catch (Exception e) {
                        e.printStackTrace();
                }
                UserDetails userDetails = null;
                if (userAccount != null) {
                        userDetails = new UserAccauntDTO();
                        ((UserAccauntDTO)
userDetails).setUsername(userAccount.getUsername());
                        ((UserAccauntDTO)
userDetails).setPassword(userAccount.getPassword());
                        ((UserAccauntDTO)
userDetails).setAccountNonExpired(userAccount.getAccountNonExpired());
                        ((UserAccauntDTO)
userDetails).setAccountNonLocked(userAccount.getAccountNonLocked());
                        ((UserAccauntDTO)
userDetails).setCredentialsNonExpired(userAccount
                                        .getCredentialsNonExpired());
                        ((UserAccauntDTO)
userDetails).setEnabled(userAccount.getEnabled());
                        GrantedAuthority[] grantedAuthorities = new
GrantedAuthorityImpl[1];
                        grantedAuthorities[0] = new
GrantedAuthorityImpl(userAccount.getAuthority().getUserRole());
                        ((UserAccauntDTO)
userDetails).setAuthorities(grantedAuthorities);
                }
                return userDetails;
        }

}

Client Server program:

File [added]: RegisterClientBO.java

package p2p.web.businessobject;

import java.sql.Timestamp;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.acegisecurity.Authentication;
import org.acegisecurity.GrantedAuthority;
import org.acegisecurity.context.SecurityContext;
import org.acegisecurity.context.SecurityContextHolder;
import org.acegisecurity.providers.encoding.Md5PasswordEncoder;
import org.acegisecurity.providers.encoding.PasswordEncoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import p2p.web.dataaccess.RegisterClientDAO;
import p2p.web.domainmodel.Authority;
import p2p.web.domainmodel.Language;
import p2p.web.domainmodel.UserAccount;

/**
* @author s.timofiychuk
*
*/
@Service("registerClientBO")
public class RegisterClientBO {

        private static final String ANONYMOUS_USER_ROLE_KEY =
"ROLE_ANONYMOUS";

        private RegisterClientDAO registerClientDAO;

        /**
         * @param registerClientDAO
         */
        @Autowired
        public RegisterClientBO(RegisterClientDAO registerClientDAO) {
                this.registerClientDAO = registerClientDAO;
        }

        /**
         * @return
         */
        public List<Language> getLanguages() {
                return registerClientDAO.getLanguages();
        }

        /**
         * @return
         */
        public List<Authority> getAvailableAuthorities() {
                SecurityContext context = SecurityContextHolder.getContext();
                Authentication authentication = context.getAuthentication();
                GrantedAuthority[] userAuthorities =
authentication.getAuthorities();
                int priority = registerClientDAO.getMinPermission();
                for (GrantedAuthority grantedAuthority : userAuthorities) {
                        int p =
registerClientDAO.getPriorityByAuthority(grantedAuthority);
                        if (p < priority) {
                                priority = p;
                        }
                }
                List<Authority> authorities =
registerClientDAO.getAllAuthorities();
                Iterator<Authority> iter = authorities.iterator();
                while (iter.hasNext()) {
                        Authority authority = iter.next();
                        if (authority.getPriorityRole() < priority) {
                                iter.remove();
                        }
                        if
(authority.getUserRole().equals(ANONYMOUS_USER_ROLE_KEY)) {
                                iter.remove();
                        }
                }
                return authorities;
        }

        /**
         * @param userAccount
         * @return
         */
        public boolean add(UserAccount userAccount) {
                if
(registerClientDAO.isExistsAccountForUserName(userAccount.getUsername())) {
                        return false;
                }
                userAccount.setRegDate(new Timestamp(new Date().getTime()));
                userAccount.setAccountNonExpired(true);
                userAccount.setAccountNonLocked(true);
                userAccount.setCredentialsNonExpired(true);
                if (userAccount.getLanguage().getId() == null) {
                        userAccount.getLanguage().setId(1L);
                }
                if (userAccount.getEnabled()==null) {
                        userAccount.setEnabled(true);
                }
                PasswordEncoder passwordEncoder = new Md5PasswordEncoder();
                
userAccount.setPassword(passwordEncoder.encodePassword(userAccount.getPassword(),
null));
                registerClientDAO.add(userAccount);
                return true;
        }

}

File [added]: ServersListBO.java

package p2p.web.businessobject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import p2p.web.dataaccess.ServersListDAO;
import p2p.web.domainmodel.SuperSeed;

/**
* @author S.Timofiychuk
*
*         25.11.2008
*
*/
@Service("serversListBO")
public class ServersListBO {

        private ServersListDAO serversListDAO;

        @Autowired
        public ServersListBO(ServersListDAO serversListDAO) {
                this.serversListDAO = serversListDAO;
        }

        /**
         * @return
         */
        public SuperSeed getSuperSeedList() {
                return serversListDAO.getSuperSeedList();
        }

}

File [added]: Peer.java

package p2p.web.businessobject;

import p2p.chat.action.ChatCoreJxta;
import p2p.chat.action.IChatCore;
import p2p.chat.dataobject.gui.Settings;

/**
* @author s.timofiychuk
*
*         27.11.2008
*
*/
public class Peer implements Runnable {

        private Settings settings;
        private IChatCore chat;
        private boolean isRunnning = false;

        public Peer(Settings settings) {
                this.settings = settings;
        }

        public void run() {
                try {
                        chat = ChatCoreJxta.getInstance(getSettings());
                        // chat.registerPeer();
                        isRunnning = true;
                        chat.join(null);
                        isRunnning = false;
                } catch (Exception e) {
                        e.printStackTrace();
                }
        }

        /**
         * @return the settings
         */
        public Settings getSettings() {
                return settings;
        }

        private void setSettings(Settings settings) {
                this.settings = settings;
        }

        public void destroy() {
                if (chat != null) {
                        setSettings(null);
                        chat.destroy();
                        chat = null;
                        isRunnning = false;
                }
        }

        /**
         * @return the chat
         */
        public IChatCore getChat() {
                return chat;
        }

}

StartChatBO.java

ackage p2p.web.businessobject;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import p2p.web.dataaccess.StartChatDAO;
import p2p.web.domainmodel.Language;

/**
* @author s.timofiychuk
*
* 20.11.2008
*
*/
@Component("startChatBO")
public class StartChatBO {

        private final StartChatDAO startChatDAO;

        /**
         * @param startChatDAO
         */
        @Autowired
        public StartChatBO(StartChatDAO startChatDAO) {
                this.startChatDAO = startChatDAO;
        }

        /**
         * @return
         */
        public List<Language> getAllLangCode() {
                return startChatDAO.getAllLangCodes();
        }

        public Language getLanguageById(Language language, Long id) {
                language.setId(id == null ? 1L : id);
                return startChatDAO.getLanguageById(language);
        }

        /**
         * @param contextPath
         * @return
         */
        public String getRdvListUri(String contextPath) {
                StringBuffer rdvUri = new StringBuffer();
                rdvUri.append("http://";)
                .append(startChatDAO.getSeedHostName())
                .append(":")
                .append(startChatDAO.getSeedPort())
                .append(contextPath)
                .append("/rdv.list");
                return rdvUri.toString();
        }

        /**
         * @param request
         * @return
         */
        public String getRelayListUri(String contextPath) {
                StringBuffer relayUri = new StringBuffer();
                relayUri.append("http://";)
                .append(startChatDAO.getSeedHostName())
                .append(":")
                .append(startChatDAO.getSeedPort())
                .append(contextPath)
                .append("/relay.list");
                return relayUri.toString();
        }

        /**
         * @return
         */
        public String getSeedHostName() {
                return startChatDAO.getSeedHostName();
        }

        /**
         * @return
         */
        public Integer getSeedHostPort() {
                return startChatDAO.getSeedPort();
        }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote