-
-
Save BubuntuClu/25ea688f51e44710b51ceb359bf8c663 to your computer and use it in GitHub Desktop.
aftermarakesh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<module type="JAVA_MODULE" version="4"> | |
<component name="NewModuleRootManager" inherit-compiler-output="true"> | |
<exclude-output /> | |
<content url="file://$MODULE_DIR$" /> | |
<orderEntry type="inheritedJdk" /> | |
<orderEntry type="sourceFolder" forTests="false" /> | |
</component> | |
</module> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<project version="4"> | |
<component name="CompilerConfiguration"> | |
<resourceExtensions /> | |
<wildcardResourcePatterns> | |
<entry name="!?*.java" /> | |
<entry name="!?*.form" /> | |
<entry name="!?*.class" /> | |
<entry name="!?*.groovy" /> | |
<entry name="!?*.scala" /> | |
<entry name="!?*.flex" /> | |
<entry name="!?*.kt" /> | |
<entry name="!?*.clj" /> | |
<entry name="!?*.aj" /> | |
</wildcardResourcePatterns> | |
<annotationProcessing> | |
<profile default="true" name="Default" enabled="false"> | |
<processorPath useClasspath="true" /> | |
</profile> | |
</annotationProcessing> | |
</component> | |
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<component name="CopyrightManager"> | |
<settings default="" /> | |
</component> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<project version="4"> | |
<component name="ProjectLevelVcsManager" settingsEditedManually="false"> | |
<OptionsSetting value="true" id="Add" /> | |
<OptionsSetting value="true" id="Remove" /> | |
<OptionsSetting value="true" id="Checkout" /> | |
<OptionsSetting value="true" id="Update" /> | |
<OptionsSetting value="true" id="Status" /> | |
<OptionsSetting value="true" id="Edit" /> | |
<ConfirmationsSetting value="0" id="Add" /> | |
<ConfirmationsSetting value="0" id="Remove" /> | |
</component> | |
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK" /> | |
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<project version="4"> | |
<component name="ProjectModuleManager"> | |
<modules> | |
<module fileurl="file://$PROJECT_DIR$/.idea/82863f8aab987059d69892fb4b7b3c1a-237f2cbc637758d453d2353691d78fed9ca8f12e.iml" filepath="$PROJECT_DIR$/.idea/82863f8aab987059d69892fb4b7b3c1a-237f2cbc637758d453d2353691d78fed9ca8f12e.iml" /> | |
</modules> | |
</component> | |
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FChat |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<project version="4"> | |
<component name="CompilerConfiguration"> | |
<resourceExtensions /> | |
<wildcardResourcePatterns> | |
<entry name="!?*.java" /> | |
<entry name="!?*.form" /> | |
<entry name="!?*.class" /> | |
<entry name="!?*.groovy" /> | |
<entry name="!?*.scala" /> | |
<entry name="!?*.flex" /> | |
<entry name="!?*.kt" /> | |
<entry name="!?*.clj" /> | |
<entry name="!?*.aj" /> | |
</wildcardResourcePatterns> | |
<annotationProcessing> | |
<profile default="true" name="Default" enabled="false"> | |
<processorPath useClasspath="true" /> | |
</profile> | |
<profile default="false" name="Maven default annotation processors profile" enabled="true"> | |
<sourceOutputDir name="target/generated-sources/annotations" /> | |
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" /> | |
<outputRelativeToContentRoot value="true" /> | |
<processorPath useClasspath="true" /> | |
<module name="FChat" /> | |
</profile> | |
</annotationProcessing> | |
<bytecodeTargetLevel> | |
<module name="FChat" target="1.5" /> | |
</bytecodeTargetLevel> | |
</component> | |
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<component name="CopyrightManager"> | |
<settings default="" /> | |
</component> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<project version="4"> | |
<component name="Encoding"> | |
<file url="file://$PROJECT_DIR$" charset="UTF-8" /> | |
<file url="file://$PROJECT_DIR$/src/main/java/Server/ChatServer.java" charset="UTF-8" /> | |
<file url="file://$PROJECT_DIR$/src/main/java/main.java" charset="UTF-8" /> | |
<file url="PROJECT" charset="UTF-8" /> | |
</component> | |
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<project version="4"> | |
<component name="EntryPointsManager"> | |
<entry_points version="2.0" /> | |
</component> | |
<component name="MavenProjectsManager"> | |
<option name="originalFiles"> | |
<list> | |
<option value="$PROJECT_DIR$/pom.xml" /> | |
</list> | |
</option> | |
</component> | |
<component name="ProjectLevelVcsManager" settingsEditedManually="false"> | |
<OptionsSetting value="true" id="Add" /> | |
<OptionsSetting value="true" id="Remove" /> | |
<OptionsSetting value="true" id="Checkout" /> | |
<OptionsSetting value="true" id="Update" /> | |
<OptionsSetting value="true" id="Status" /> | |
<OptionsSetting value="true" id="Edit" /> | |
<ConfirmationsSetting value="0" id="Add" /> | |
<ConfirmationsSetting value="0" id="Remove" /> | |
</component> | |
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK"> | |
<output url="file://$PROJECT_DIR$/out" /> | |
</component> | |
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<project version="4"> | |
<component name="ProjectModuleManager"> | |
<modules> | |
<module fileurl="file://$PROJECT_DIR$/FChat.iml" filepath="$PROJECT_DIR$/FChat.iml" /> | |
</modules> | |
</component> | |
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<project version="4"> | |
<component name="Palette2"> | |
<group name="Swing"> | |
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.png" removable="false" auto-create-binding="false" can-attach-label="false"> | |
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" /> | |
</item> | |
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.png" removable="false" auto-create-binding="false" can-attach-label="false"> | |
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" /> | |
</item> | |
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.png" removable="false" auto-create-binding="false" can-attach-label="false"> | |
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" /> | |
</item> | |
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.png" removable="false" auto-create-binding="false" can-attach-label="true"> | |
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" /> | |
</item> | |
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.png" removable="false" auto-create-binding="true" can-attach-label="false"> | |
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" /> | |
<initial-values> | |
<property name="text" value="Button" /> | |
</initial-values> | |
</item> | |
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.png" removable="false" auto-create-binding="true" can-attach-label="false"> | |
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" /> | |
<initial-values> | |
<property name="text" value="RadioButton" /> | |
</initial-values> | |
</item> | |
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.png" removable="false" auto-create-binding="true" can-attach-label="false"> | |
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" /> | |
<initial-values> | |
<property name="text" value="CheckBox" /> | |
</initial-values> | |
</item> | |
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.png" removable="false" auto-create-binding="false" can-attach-label="false"> | |
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" /> | |
<initial-values> | |
<property name="text" value="Label" /> | |
</initial-values> | |
</item> | |
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.png" removable="false" auto-create-binding="true" can-attach-label="true"> | |
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1"> | |
<preferred-size width="150" height="-1" /> | |
</default-constraints> | |
</item> | |
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.png" removable="false" auto-create-binding="true" can-attach-label="true"> | |
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1"> | |
<preferred-size width="150" height="-1" /> | |
</default-constraints> | |
</item> | |
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.png" removable="false" auto-create-binding="true" can-attach-label="true"> | |
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1"> | |
<preferred-size width="150" height="-1" /> | |
</default-constraints> | |
</item> | |
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.png" removable="false" auto-create-binding="true" can-attach-label="true"> | |
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3"> | |
<preferred-size width="150" height="50" /> | |
</default-constraints> | |
</item> | |
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.png" removable="false" auto-create-binding="true" can-attach-label="true"> | |
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3"> | |
<preferred-size width="150" height="50" /> | |
</default-constraints> | |
</item> | |
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.png" removable="false" auto-create-binding="true" can-attach-label="true"> | |
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3"> | |
<preferred-size width="150" height="50" /> | |
</default-constraints> | |
</item> | |
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.png" removable="false" auto-create-binding="true" can-attach-label="true"> | |
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" /> | |
</item> | |
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.png" removable="false" auto-create-binding="true" can-attach-label="false"> | |
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3"> | |
<preferred-size width="150" height="50" /> | |
</default-constraints> | |
</item> | |
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.png" removable="false" auto-create-binding="true" can-attach-label="false"> | |
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3"> | |
<preferred-size width="150" height="50" /> | |
</default-constraints> | |
</item> | |
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.png" removable="false" auto-create-binding="true" can-attach-label="false"> | |
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3"> | |
<preferred-size width="150" height="50" /> | |
</default-constraints> | |
</item> | |
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.png" removable="false" auto-create-binding="true" can-attach-label="false"> | |
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3"> | |
<preferred-size width="200" height="200" /> | |
</default-constraints> | |
</item> | |
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.png" removable="false" auto-create-binding="false" can-attach-label="false"> | |
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3"> | |
<preferred-size width="200" height="200" /> | |
</default-constraints> | |
</item> | |
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.png" removable="false" auto-create-binding="true" can-attach-label="true"> | |
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" /> | |
</item> | |
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.png" removable="false" auto-create-binding="true" can-attach-label="false"> | |
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" /> | |
</item> | |
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.png" removable="false" auto-create-binding="false" can-attach-label="false"> | |
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" /> | |
</item> | |
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.png" removable="false" auto-create-binding="true" can-attach-label="false"> | |
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" /> | |
</item> | |
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.png" removable="false" auto-create-binding="false" can-attach-label="false"> | |
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1"> | |
<preferred-size width="-1" height="20" /> | |
</default-constraints> | |
</item> | |
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.png" removable="false" auto-create-binding="false" can-attach-label="false"> | |
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" /> | |
</item> | |
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.png" removable="false" auto-create-binding="true" can-attach-label="false"> | |
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" /> | |
</item> | |
</group> | |
</component> | |
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
start mvn clean |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
start mvn compile |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4"> | |
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_5" inherit-compiler-output="false"> | |
<output url="file://$MODULE_DIR$/target/classes" /> | |
<output-test url="file://$MODULE_DIR$/target/test-classes" /> | |
<content url="file://$MODULE_DIR$"> | |
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" /> | |
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" /> | |
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" /> | |
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" /> | |
<excludeFolder url="file://$MODULE_DIR$/target" /> | |
</content> | |
<orderEntry type="inheritedJdk" /> | |
<orderEntry type="sourceFolder" forTests="false" /> | |
</component> | |
</module> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
start mvn package |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<project xmlns="http://maven.apache.org/POM/4.0.0" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>FChat</groupId> | |
<artifactId>FChat</artifactId> | |
<version>1.0-SNAPSHOT</version> | |
<packaging>jar</packaging> | |
<properties> | |
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | |
</properties> | |
<dependencies> | |
<dependency> | |
<groupId>junit</groupId> | |
<artifactId>junit</artifactId> | |
<version>4.12</version> | |
<scope>test</scope> | |
</dependency> | |
</dependencies> | |
<build> | |
<resources> | |
<resource> | |
<directory>src/main/resources</directory> | |
</resource> | |
</resources> | |
</build> | |
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ChatHelper; | |
import Interface.ICommand; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class ChatCommand implements ICommand { | |
private static List<String> command; | |
private static List<String> adminCommand; | |
public ChatCommand(){ | |
command=new ArrayList<String>(); | |
command.add("/help"); | |
command.add("/setname"); | |
command.add("/who"); | |
command.add("/exit"); | |
adminCommand=new ArrayList<String>(); | |
adminCommand.add("/shutdown"); | |
} | |
public List<String> getCommand(){ | |
return command; | |
} | |
public boolean isCommand(String message) | |
{ | |
for(String com:command) { | |
if(message.indexOf(com) == 0) | |
return true; | |
} | |
for(String com:adminCommand) { | |
if(message.indexOf(com) == 0) | |
return true; | |
} | |
return false; | |
} | |
public List<String> getAdminCommand(){ | |
return adminCommand; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ChatHelper; | |
import Config.Config; | |
import Interface.IHistory; | |
import java.util.LinkedList; | |
import java.util.Queue; | |
public class ChatHistory implements IHistory { | |
private static Queue<String> history; | |
public ChatHistory() { | |
history = new LinkedList<String>(); | |
} | |
public void addMessage(String message){ | |
if (history.size() > Config.HISTORY_LENGTH){ | |
history.poll(); | |
} | |
history.add(message); | |
} | |
public Queue<String> getHistory(){ | |
return history; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ChatHelper; | |
import Interface.INames; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class ChatNames implements INames{ | |
private static List<String> Names; | |
public ChatNames() { | |
Names = new ArrayList<String>(); | |
} | |
public void addName(String name){ | |
Names.add(name); | |
} | |
public void deleteName(String name){ | |
Names.remove(name); | |
} | |
public List<String> getNames(){ | |
return Names; | |
} | |
public boolean isNameFree(String name) | |
{ | |
return !Names.contains(name); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ChatHelper; | |
import Classes.User; | |
import Config.Config; | |
import Interface.IHistory; | |
import java.io.IOException; | |
import java.nio.ByteBuffer; | |
import java.nio.channels.SelectionKey; | |
import java.nio.channels.SocketChannel; | |
import java.nio.charset.StandardCharsets; | |
import java.util.Map; | |
/** | |
* Created by zolotukhinas on 07.04.2016. | |
*/ | |
public class MessageUtils { | |
public static void sendMessageToUser(SelectionKey key, String message, Map<SocketChannel, User> usersOnServer) throws IOException { | |
SocketChannel channelClient = (SocketChannel) key.channel(); | |
for (Map.Entry<SocketChannel, User> entry : usersOnServer.entrySet()) { | |
SocketChannel channel = entry.getKey(); | |
if (channelClient == channel) { | |
send(channelClient, ("server:" + message.trim())); | |
key.interestOps(SelectionKey.OP_READ); | |
break; | |
} | |
} | |
} | |
public static void sendMessageToUsers(SelectionKey key, String data, boolean isServerInfo, Map<SocketChannel, User> usersOnServer) throws IOException { | |
SocketChannel channelClient = (SocketChannel) key.channel(); | |
String hostName = (isServerInfo) ? "server" : (usersOnServer.get(channelClient)).getName(); | |
for (Map.Entry<SocketChannel, User> entry : usersOnServer.entrySet()) { | |
SocketChannel channel = entry.getKey(); | |
if (channelClient != channel) { | |
send(channel, (hostName + ":" + data)); | |
} | |
} | |
key.interestOps(SelectionKey.OP_READ); | |
} | |
public static void sendHelloMessageToNewUser(User user) { | |
try { | |
send(user.getUserChannel(), "Welcome.Please enter a nickname using \"/setname\", otherwise you will be " + | |
Config.DEFAULT_USER_NAME + ".\n You can type \"/help\" to get list of commands\n"); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
public static void sendHistoryToNewUser(User user, IHistory History) { | |
try { | |
for (String mes : History.getHistory()) | |
send(user.getUserChannel(), "History:" + mes.trim() + "\n"); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
private static void send(SocketChannel channel, String Data) throws IOException { | |
channel.write(ByteBuffer.wrap((Data.trim() + "\n").getBytes(StandardCharsets.UTF_8))); | |
} | |
public static void sendMessageFromUser(SelectionKey key, Map<SocketChannel, User> usersOnServer, String message) { | |
try { | |
sendMessageToUsers(key, message, false, usersOnServer); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
public static String receiveMessageFromUser(SocketChannel channel) { | |
ByteBuffer buffer = ByteBuffer.allocate(8192); | |
int numRead = -1; | |
try { | |
numRead = channel.read(buffer); | |
return new String(buffer.array(), StandardCharsets.UTF_8); | |
} catch (IOException e) { | |
//if we cant read, it means that user has left | |
if (numRead == -1) { | |
return null; | |
} | |
e.printStackTrace(); | |
} | |
return null; | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ChatHelper; | |
import Classes.User; | |
import Config.Config; | |
import java.io.IOException; | |
import java.nio.channels.SelectionKey; | |
import java.nio.channels.ServerSocketChannel; | |
import java.nio.channels.SocketChannel; | |
import java.util.Map; | |
/** | |
* Created by zolotukhinas on 07.04.2016. | |
*/ | |
public class UserWorker { | |
private List<string> sdhgsadgsa; | |
getUsers() { | |
return Collections.immutable(sdhgsadgsa) | |
} | |
// TODO: Singleton service? | |
public static User addUser(SelectionKey key, Map<SocketChannel, User> usersOnServers) { | |
try { | |
ServerSocketChannel serverChannel = (ServerSocketChannel) key.channel(); | |
SocketChannel channel = serverChannel.accept(); | |
channel.configureBlocking(false); | |
User user = new User(channel, Config.DEFAULT_USER_NAME); | |
usersOnServers.put(channel, user); | |
return user; | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
//TODO null is very very bad | |
return null; | |
} | |
public static User removeUser(SelectionKey key, Map<SocketChannel, User> usersOnServers) { | |
SocketChannel channel = (SocketChannel) key.channel(); | |
User delUser = usersOnServers.get(channel); | |
if (!delUser.getName().equals("Unknown")) { | |
//TODO | |
try { | |
MessageUtils.sendMessageToUsers(key, "User " + delUser + " leave the chat.whistle",true, usersOnServers); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
//TODO | |
//sendToUser(key, "kicked"); | |
usersOnServers.remove(channel); | |
return delUser; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Classes; | |
import java.nio.channels.SocketChannel; | |
/** | |
* Created by zolotukhinas on 07.04.2016. | |
*/ | |
public class User { | |
private SocketChannel userChannel; | |
private String name; | |
public User (SocketChannel sc,String name){ | |
this.userChannel = sc; | |
this.name = name; | |
} | |
public SocketChannel getUserChannel() { | |
return userChannel; | |
} | |
public void setUserChannel(SocketChannel userChannel) { | |
this.userChannel = userChannel; | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Config; | |
import java.io.FileNotFoundException; | |
import java.io.IOException; | |
import java.util.Properties; | |
public class Config { | |
public static int PORT; | |
public static int HISTORY_LENGTH; | |
public static String HOST; | |
public static String DEFAULT_USER_NAME; | |
public void getProp() | |
{ | |
Properties properties = new Properties(); | |
try { | |
properties.load(getClass().getClassLoader().getResourceAsStream("settings"));// propertiesFile); | |
PORT = Integer.parseInt(properties.getProperty("PORT")); | |
HISTORY_LENGTH = Integer.parseInt(properties.getProperty("HISTORY_LENGTH")); | |
HOST = properties.getProperty("HOST"); | |
DEFAULT_USER_NAME=properties.getProperty("DEFAULT_USER_NAME"); | |
} catch (FileNotFoundException ex) { | |
System.err.println("Properties config file not found"); | |
} catch (IOException ex) { | |
System.err.println("Error while reading file"); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Interface; | |
import java.util.List; | |
public interface ICommand { | |
List<String> getCommand(); | |
boolean isCommand(String message); | |
List<String> getAdminCommand(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Interface; | |
import java.util.Queue; | |
public interface IHistory { | |
void addMessage(String message); | |
Queue<String> getHistory(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Interface; | |
import java.util.List; | |
public interface INames { | |
void addName(String name); | |
void deleteName(String name); | |
List<String> getNames(); | |
boolean isNameFree(String name); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Interface; | |
import Classes.User; | |
import java.io.IOException; | |
import java.nio.channels.SelectionKey; | |
import java.nio.channels.SocketChannel; | |
public interface IServer { | |
boolean startServer()throws IOException; | |
void stopServer(); | |
void listenServer(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Interface.IServer; | |
import Server.ChatServer; | |
import java.io.IOException; | |
public class main { | |
public static void main(String[] args) { | |
try { | |
IServer server = new ChatServer(); | |
System.out.println("Starting"); | |
if(server.startServer()) { | |
System.out.println("Working..."); | |
server.listenServer(); | |
} | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Server; | |
import ChatHelper.MessageUtils; | |
import Classes.User; | |
import Config.Config; | |
import Interface.*; | |
import ChatHelper.*; | |
import java.io.IOException; | |
import java.net.InetSocketAddress; | |
import java.nio.channels.*; | |
import java.util.*; | |
interface ICommand { | |
getKey():string | |
execute(string params):void | |
} | |
class BaseCommand implements ICommand { | |
prepare() { | |
//magic | |
} | |
} | |
class MessageService { | |
private | |
@InjectService<ICommandService> | |
private User user; | |
} | |
@RootOnly | |
class SayHelloCommand implements ICommand { | |
@InjectService | |
private MessageService service; | |
getKey() { | |
return "say_hello" | |
} | |
execute() { | |
service.sendMessages() | |
} | |
} | |
public class CommandService { | |
private Map<string, ICommand> commandsMap; | |
private static CommandService _instance; | |
private CommandService() { | |
commandsMap = new Map<string, ICommand.class>(); | |
} | |
public static CommandService getInstance() { | |
if (CommandService._instance == null) { | |
CommandService._instance = new CommandService(); | |
} | |
return CommandService._instance; | |
} | |
public CommandService() { | |
commandsMap = new Map<string, ICommand> | |
} | |
public boolean registerCommand<T extends ICommand>(String key) { | |
commandsMap.add(command.getKey(), T.class) | |
} | |
public executeCommand(String key, string params) { | |
Activator.createClass<ICommand>("") | |
commandsMap.get(key).execute(params) | |
} | |
} | |
import .....WhoCommand | |
public class CommandsRegistrator { | |
public static void registerTo(ICommandService service) { | |
service.register("who", WhoCommand.class) | |
service.register("blablabla", ) | |
} | |
} | |
public class ChatServer implements IServer { | |
private Selector serverSel; | |
//interfaces for painless replacement classes, if we need another logic | |
private IHistory History; | |
private ICommand Command; | |
private INames Names; | |
private ServerSocketChannel serverChannel; | |
private Map<SocketChannel, User> usersOnServer; | |
private List<SelectionKey> sel; | |
public ChatServer() throws IOException { | |
Config Config = new Config(); | |
Config.getProp(); | |
History = new ChatHistory(); | |
Command = new ChatCommand(); | |
Names = new ChatNames(); | |
usersOnServer = new HashMap<SocketChannel, User>(); | |
sel = new ArrayList<SelectionKey>(); | |
} | |
public void listenServer() { | |
try { | |
while (true) { | |
serverSel.select(); | |
Iterator keys = serverSel.selectedKeys().iterator(); | |
while (keys.hasNext()) { | |
SelectionKey key = (SelectionKey) keys.next(); | |
keys.remove(); | |
if (!key.isValid()) | |
continue; | |
if (key.isAcceptable()) { | |
User user=addNewUser(key); | |
writeHelloAndHistoryToNewUser(user); | |
registerNewUser(user); | |
} | |
else if (key.isReadable()) { | |
workWithMessageFromUser(key); | |
} | |
else if (key.isWritable()) { | |
key.interestOps(SelectionKey.OP_READ); | |
} | |
} | |
} | |
} catch(ClosedChannelException ex) | |
{ | |
System.out.println("BIG BADA BOOM!"); | |
} catch (Exception ex) { | |
System.out.println("BIG BADA BOOM!"); | |
} | |
} | |
public boolean startServer() { | |
try { | |
serverSel = Selector.open(); | |
serverChannel = ServerSocketChannel.open(); | |
serverChannel.configureBlocking(false); | |
serverChannel.socket().bind(new InetSocketAddress(Config.HOST, Config.PORT)); | |
serverChannel.register(serverSel, SelectionKey.OP_ACCEPT); | |
return true; | |
} | |
catch(Exception ex) | |
{ | |
System.out.println(ex); | |
return false; | |
} | |
} | |
public void stopServer() { | |
try { | |
serverSel.close(); | |
serverChannel.close(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
//TODO put it in serverHelpers | |
private User addNewUser(SelectionKey key) throws IOException { | |
if (!sel.contains(key)) { | |
sel.add(key); | |
} | |
return UserWorker.addUser(key, usersOnServer); | |
} | |
private void registerNewUser(User user){ | |
try { | |
user.getUserChannel().register(this.serverSel,SelectionKey.OP_READ); | |
} catch (ClosedChannelException e) { | |
e.printStackTrace(); | |
} | |
} | |
private void writeHelloAndHistoryToNewUser(User user){ | |
writeHelloToNewUser(user); | |
writeHistoryToNewUser(user); | |
} | |
private void writeHelloToNewUser(User user){ | |
MessageUtils.sendHelloMessageToNewUser(user); | |
} | |
private void writeHistoryToNewUser(User user){ | |
MessageUtils.sendHistoryToNewUser(user, History); | |
} | |
private void workWithMessageFromUser(SelectionKey key) { | |
SocketChannel channel = (SocketChannel) key.channel(); | |
String message = MessageUtils.receiveMessageFromUser(usersOnServer.get(channel).getUserChannel()); | |
if (message == null) { | |
clientDisconnection(key); | |
return; | |
} | |
if (false) | |
{ | |
//TODO check on command | |
} | |
else { | |
History.addMessage(message); | |
MessageUtils.sendMessageFromUser(key, usersOnServer, message); | |
key.interestOps(SelectionKey.OP_READ); | |
} | |
} | |
private void clientDisconnection(SelectionKey key) { | |
try { | |
Names.deleteName(UserWorker.removeUser(key, usersOnServer).getName()); | |
key.interestOps(SelectionKey.OP_READ); | |
key.channel().close(); | |
key.cancel(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
private void clientIdentification(SelectionKey key, String Name) throws IOException { | |
MessageUtils.sendMessageToUsers(key, "One "+Config.DEFAULT_USER_NAME+" became " + Name + " .\n",true, usersOnServer); | |
} | |
/* private isMessageCommand(SelectionKey key){ | |
SocketChannel userChannel = (SocketChannel) key.channel(); | |
userChannel = (SocketChannel) key.channel(); | |
if (usersOnServer.get(userChannel).toLowerCase().equals("admin")) { | |
for (String adminCom : Command.getAdminCommand()) { | |
if (message.indexOf(adminCom) == 0) { | |
if (adminCom.equals("/shutdown")) { | |
sendToUsers(key, "Server shuts down.Try to reconnect in few minutes.\n", true); | |
stopServer(); | |
break; | |
} | |
} | |
} | |
} | |
for (String com : Command.getCommand()) | |
if (message.indexOf(com) == 0) { | |
if (com.equals("/setname")) { | |
String TrimName = message.substring(9).trim(); | |
String CurName=usersOnServer.get(userChannel); | |
if (Names.isNameFree(TrimName.toLowerCase())) { | |
Names.addName(TrimName); | |
if (!CurName.equals(Config.DEFAULT_USER_NAME)) { | |
Names.deleteName(CurName); | |
sendToUsers(key, "User " + CurName + " change name to " + TrimName, true); | |
} | |
else | |
clientIdentification(key, TrimName); | |
sendToUser(key, "Name Accepted."); | |
usersOnServer.put(userChannel, TrimName); | |
break; | |
} else { | |
sendToUser(key, "Name is busy.\nPlease try another or u will be "+dataMap.get(userChannel)+"."); | |
break; | |
} | |
} | |
if (com.equals("/help")) { | |
for (String command : Command.getCommand()) | |
sendToUser(key, command + "\n"); | |
if (usersOnServer.get(userChannel).toLowerCase().equals("admin")) | |
for (String command : Command.getAdminCommand()) | |
sendToUser(key, command + "\n"); | |
} | |
if (com.equals("/who")) { | |
for (String name : Names.getNames()) | |
sendToUser(key, name + "\n"); | |
} | |
if (com.equals("/exit")) { | |
clientDisconnection(userChannel, key); | |
break; | |
} | |
} | |
} | |
*/ | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PORT=5000 | |
HISTORY_LENGTH=100 | |
HOST=127.0.0.1 | |
DEFAULT_USER_NAME=Unknown |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*import ChatHelper.*; | |
import Server.chatServer; | |
import org.junit.Test; | |
import java.io.*; | |
import java.net.Socket; | |
import java.nio.charset.StandardCharsets; | |
import static junit.framework.Assert.*; | |
public class TestModule { | |
//region TestHistory | |
@Test | |
public void TestAddHistory(){ | |
chatHistory History= new chatHistory(); | |
History.addMessage("asd"); | |
assertEquals(1,History.getHistory().size()); | |
} | |
//endregion | |
//region TestName | |
@Test | |
public void TestAddName(){ | |
chatNames Names= new chatNames(); | |
Names.addName("asd"); | |
assertEquals(1,Names.getNames().size()); | |
} | |
@Test | |
public void TestDeleteName(){ | |
chatNames Names= new chatNames(); | |
Names.addName("asd"); | |
Names.addName("asd1"); | |
Names.deleteName("asd"); | |
assertEquals(1, Names.getNames().size()); | |
} | |
@Test | |
public void TestisNameFree(){ | |
chatNames Names= new chatNames(); | |
Names.addName("asd"); | |
Names.addName("asd1"); | |
assertEquals(true, Names.isNameFree("adsg")); | |
assertEquals(false, Names.isNameFree("asd1")); | |
} | |
//endregion | |
//region TestCommand | |
@Test | |
public void TestGetCommand(){ | |
chatCommand Commands= new chatCommand(); | |
assertEquals(4, Commands.getCommand().size()); | |
} | |
@Test | |
public void TestIsCommand(){ | |
chatCommand Commands= new chatCommand(); | |
assertEquals(true, Commands.isCommand("/help")); | |
assertEquals(false, Commands.isCommand("/hlep")); | |
} | |
@Test | |
public void TestGetAdminCommand(){ | |
chatCommand Commands= new chatCommand(); | |
assertEquals(1, Commands.getAdminCommand().size()); | |
} | |
//endregion | |
//region TestServer | |
@Test | |
public void TestStartServer(){ | |
try { | |
chatServer server = new chatServer(); | |
assertEquals(true,server.startServer()); | |
server.stopServer(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
//endregion | |
} | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
start java -cp target/FChat-1.0-SNAPSHOT.jar main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment