Skip to content

Commit

Permalink
Development update v0.4.0 (#9)
Browse files Browse the repository at this point in the history
new function:
- Daemon thread. No longer worry about server crashes caused by strange bugs
- Online user list query
- Channel list query (show whether it is a public channel)
- Active logout
- Actively exit the channel
- Start the logo

repair:
- Fixed the abnormal exit of the channel maintenance thread that may be triggered when users log out after joining a channel

optimization:
- Cancel POST URL parameters, all POST method parameters are obtained from the request body now
- The request to send information to the channel no longer needs a ticket
- Several performance optimizations
  • Loading branch information
Oct-autumn authored Nov 21, 2021
1 parent f1d1e24 commit 3cfc30c
Show file tree
Hide file tree
Showing 16 changed files with 417 additions and 150 deletions.
11 changes: 5 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@

<groupId>cn.csuosa</groupId>
<artifactId>ChattingRoom-server</artifactId>

<version>beta-0.3.1</version>
<version>beta-0.4.0</version>

<properties>
<maven.compiler.source>14</maven.compiler.source>
<maven.compiler.target>14</maven.compiler.target>
<maven.compiler.source>15</maven.compiler.source>
<maven.compiler.target>15</maven.compiler.target>
</properties>

<dependencies>
Expand Down Expand Up @@ -63,8 +62,8 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>14</source>
<target>14</target>
<source>15</source>
<target>15</target>
</configuration>
</plugin>
</plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,21 @@
import cn.CSUOSA.ChattingRoomServer.OverWriteMethod.Out;
import cn.CSUOSA.ChattingRoomServer.ReturnParams.BoolMsgWithObj;
import org.springframework.lang.Nullable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

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

import static cn.CSUOSA.ChattingRoomServer.User.UserActions.verifyUser;

@RestController
@RequestMapping("/channel")
public class ChannelActions
{
//验证频道存在与ticket是否正确
public static boolean verifyChannel(String name, String ticket)
{
if (ticket == null)
Expand Down Expand Up @@ -43,9 +47,28 @@ public static boolean verifyChannel(String name, String ticket)
}
}

//频道创建相关
//仅验证频道存在
public static boolean verifyChannel(String name)
{
if (name.length() < 4 || name.length() > 64)
return false;
else
{
//查找是否含有非法字符
for (char ch : name.toCharArray())
{
if (((ch < 48) || (ch > 57)) && ((ch < 65) || (ch > 90)) && ((ch < 97) || (ch > 122)))
{
return false;
}
}
return Main.ChannelList.containsKey(name);
}
}

//创建频道
@PostMapping("/create")
public BoolMsgWithObj channelCreation(@RequestParam(value = "usrNick") String usrNick, @RequestParam(value = "name") String name, String usrTicket, @Nullable String ticket)
public BoolMsgWithObj channelCreation(String usrNick, String usrTicket, String name, @Nullable String ticket)
{
if (!verifyUser(usrNick, usrTicket))
return new BoolMsgWithObj(false, "Authentication failed.");
Expand Down Expand Up @@ -82,8 +105,9 @@ public BoolMsgWithObj channelCreation(@RequestParam(value = "usrNick") String us
}
}

//加入频道
@PostMapping("/join")
public BoolMsgWithObj channelJoin(@RequestParam(value = "usrNick") String usrNick, @RequestParam(value = "name") String name, String usrTicket, @Nullable String ticket)
public BoolMsgWithObj channelJoin(String usrNick, String usrTicket, String name, @Nullable String ticket)
{
if (!verifyUser(usrNick, usrTicket))
return new BoolMsgWithObj(false, "Authentication failed.");
Expand Down Expand Up @@ -122,4 +146,37 @@ public BoolMsgWithObj channelJoin(@RequestParam(value = "usrNick") String usrNic
return new BoolMsgWithObj(true, "");
}
}

//退出频道
@PostMapping("/quit")
public BoolMsgWithObj channelQuit(String usrNick, String usrTicket, String name)
{
if (!verifyUser(usrNick, usrTicket))
return new BoolMsgWithObj(false, "Authentication failed.");

if (!verifyChannel(name))
return new BoolMsgWithObj(false, "Un Known Channel.");

if (!Main.ChannelList.get(name).getMembers().contains(Main.UserList.get(usrNick)))
return new BoolMsgWithObj(false, "You are not a member of that channel.");

Out.Info("User [" + usrNick + "] lefts channel [" + name + "]");
Main.ChannelList.get(name).removeMember(Main.UserList.get(usrNick));


return new BoolMsgWithObj(true, "");
}

@GetMapping("/list")
public List<String> listUsers()
{
List<String> userList = new ArrayList<>();
if (!Main.ChannelList.isEmpty())
{
Main.ChannelList.forEach((chaName, chaInfo) -> userList.add(chaName + " " + (chaInfo.getTicket().equals("") ? "[public]" : "[private]")));
}

return userList;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package cn.CSUOSA.ChattingRoomServer.Channel;

import cn.CSUOSA.ChattingRoomServer.Main;
import cn.CSUOSA.ChattingRoomServer.OverWriteMethod.Out;
import cn.CSUOSA.ChattingRoomServer.User.UserInfo;

public class ChannelCleaner implements Runnable
{
@Override
public void run()
{
if (!Main.ChannelList.isEmpty())
{
Main.ChannelList.forEach((chaName, chaInfo) -> {
for (UserInfo nowMember : chaInfo.getMembers())
if (!Main.UserList.containsKey(nowMember.getNick()))
{
Out.Info("User [" + nowMember.getNick() + "] left channel [" + chaName + "]");
chaInfo.removeMember(nowMember);
if (chaInfo.getMembers().isEmpty())
break;
}
if (chaInfo.getMembers().isEmpty() && chaInfo.getAutoClose())
{
Main.ChannelList.remove(chaName);
Out.Info("Channel [" + chaName + "] Closed");
}
});
}
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
package cn.CSUOSA.ChattingRoomServer.ConsoleController;

import cn.CSUOSA.ChattingRoomServer.Main;
import cn.CSUOSA.ChattingRoomServer.OverWriteMethod.Out;
import org.jline.reader.LineReaderBuilder;
import org.jline.terminal.TerminalBuilder;

import java.io.IOException;

import static cn.CSUOSA.ChattingRoomServer.Main.mainController;

public class CmdProcessor implements Runnable
{
private boolean keepRunning = true;

public void stopRunning()
{
keepRunning = false;
synchronized (this) {notify();}
}

@Override
public void run()
{
Expand All @@ -19,12 +28,13 @@ public void run()
.jansi(true)
.build();


Main.lineReader = LineReaderBuilder.builder()
.terminal(Main.terminal)
.build();

String prompt = "ChatServer> ";
while (true)
while (keepRunning)
{
String line = Main.lineReader.readLine(prompt);
String[] args = line.split(" ");
Expand All @@ -40,10 +50,7 @@ private void CommandProcessor(String[] args)
{
switch (args[0])
{
case "exit", "Exit", "EXIT" -> {
Out.Warn("Server Exit.");
System.exit(0);
}
case "stop", "Stop", "STOP" -> mainController.closeServer();
case "list", "List", "LIST" -> Processors.listObject(args);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package cn.CSUOSA.ChattingRoomServer.ConsoleController;

import org.jline.reader.Candidate;
import org.jline.reader.Completer;
import org.jline.reader.LineReader;
import org.jline.reader.ParsedLine;

import java.util.List;

public class CommandCompleter implements Completer
{

@Override
public void complete(LineReader lineReader, ParsedLine parsedLine, List<Candidate> list)
{

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@

public class Processors
{
//列出用户、频道
public static void listObject(String[] args)
{
switch (args[1])
{
case "-u", "--user" -> {
case "user" -> {
if (Main.UserList.isEmpty())
Out.Warn("No User Online.");
else
Expand All @@ -32,7 +33,7 @@ public static void listObject(String[] args)
Out.Info(strBuilder.toString());
}
}
case "-g", "--group" -> {
case "channel" -> {
if (Main.ChannelList.isEmpty())
Out.Warn("No Channel Opened.");
else
Expand Down
33 changes: 23 additions & 10 deletions src/main/java/cn/CSUOSA/ChattingRoomServer/Main.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package cn.CSUOSA.ChattingRoomServer;

import cn.CSUOSA.ChattingRoomServer.Channel.ChannelInfo;
import cn.CSUOSA.ChattingRoomServer.ConsoleController.CmdProcessor;
import cn.CSUOSA.ChattingRoomServer.Message.MessageListEntry;
import cn.CSUOSA.ChattingRoomServer.Message.MessageListMaintain;
import cn.CSUOSA.ChattingRoomServer.OverWriteMethod.Out;
import cn.CSUOSA.ChattingRoomServer.User.UserInfo;
import cn.CSUOSA.ChattingRoomServer.User.UserMapTimer;
import org.jline.reader.LineReader;
import org.jline.terminal.Terminal;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -24,27 +21,43 @@ public class Main
public final static ConcurrentHashMap<String, UserInfo> UserList = new ConcurrentHashMap<>(); //用户列表
public final static ConcurrentHashMap<String, ChannelInfo> ChannelList = new ConcurrentHashMap<>(); //频道列表
public final static ConcurrentHashMap<Long, MessageListEntry> MsgList = new ConcurrentHashMap<>(); //消息队列
public final static UserMapTimer userMapTimer = new UserMapTimer();
public final static MessageListMaintain msgListCleaner = new MessageListMaintain();
public final static MainController mainController = new MainController();

public static Terminal terminal;
public static LineReader lineReader;
public static long msgCount = 0;

private static ConfigurableApplicationContext CAC;

public static ConfigurableApplicationContext CAC;

@Autowired
Environment environment;

public static void main(String[] args)
{
new Thread(new CmdProcessor()).start(); //启动命令行处理器
System.out.println("""
\033[34m
_____ _____ _ _ _ _ _ _
/ __ \\/ ___| | | | | | | | | | (_)
| / \\/\\ `--.| | | | ___| |__ __ _| |_| |_ _ _ __ __ _
| | `--. \\ | | | / __| '_ \\ / _` | __| __| | '_ \\ / _` |
| \\__/\\/\\__/ / |_| | | (__| | | | (_| | |_| |_| | | | | (_| |
\\____/\\____/ \\___/ \\___|_| |_|\\__,_|\\__|\\__|_|_| |_|\\__, |
__/ |
|___/
CSU-OSA Chatting Room Server(v0.4.0-beta)
------
CSU-OSA all rights reserved.
\033[m
""");

CAC = SpringApplication.run(Main.class, args); //启动SpringBoot

new Thread(userMapTimer).start(); //启动昵称占用计时器
new Thread(msgListCleaner).start(); //启动主消息队列维护线程
new Thread(mainController).start(); //启动中控线程

try
{
//等待200ms
Thread.sleep(500);
} catch (InterruptedException e)
{
Expand Down
Loading

0 comments on commit 3cfc30c

Please sign in to comment.