Report 기능 테스트중
@08277e679d74d029b7d1f79e98018fa3dfaf1507
+++ src/main/java/com/munjaon/server/cache/enums/CacheService.java
... | ... | @@ -0,0 +1,15 @@ |
| 1 | +package com.munjaon.server.cache.enums; | |
| 2 | + | |
| 3 | +import lombok.Getter; | |
| 4 | + | |
| 5 | +@Getter | |
| 6 | +public enum CacheService { | |
| 7 | + LOGIN_SERVICE, | |
| 8 | + REPORT_SERVICE; | |
| 9 | + | |
| 10 | + private Object service; | |
| 11 | + | |
| 12 | + public void setService(Object service) { | |
| 13 | + this.service = service; | |
| 14 | + } | |
| 15 | +} |
+++ src/main/java/com/munjaon/server/cache/mapper/ReportMapper.java
... | ... | @@ -0,0 +1,10 @@ |
| 1 | +package com.munjaon.server.cache.mapper; | |
| 2 | + | |
| 3 | +import com.munjaon.server.server.dto.ReportDto; | |
| 4 | +import org.apache.ibatis.annotations.Mapper; | |
| 5 | + | |
| 6 | +@Mapper | |
| 7 | +public interface ReportMapper { | |
| 8 | + ReportDto getReportForUser(String userId); | |
| 9 | + int deleteReport(String msgId); | |
| 10 | +} |
+++ src/main/java/com/munjaon/server/cache/mapper/SerialNoMapper.java
... | ... | @@ -0,0 +1,11 @@ |
| 1 | +package com.munjaon.server.cache.mapper; | |
| 2 | + | |
| 3 | +import org.apache.ibatis.annotations.Mapper; | |
| 4 | + | |
| 5 | +@Mapper | |
| 6 | +public interface SerialNoMapper { | |
| 7 | + String getSerialNoForLock(); | |
| 8 | + int insert(); | |
| 9 | + int update(); | |
| 10 | + String getSerialNo(); | |
| 11 | +} |
+++ src/main/java/com/munjaon/server/cache/service/CacheServiceInjector.java
... | ... | @@ -0,0 +1,29 @@ |
| 1 | +package com.munjaon.server.cache.service; | |
| 2 | + | |
| 3 | +import com.munjaon.server.cache.enums.CacheService; | |
| 4 | +import jakarta.annotation.PostConstruct; | |
| 5 | +import org.springframework.beans.factory.annotation.Autowired; | |
| 6 | +import org.springframework.stereotype.Component; | |
| 7 | + | |
| 8 | +import java.util.EnumSet; | |
| 9 | + | |
| 10 | +@Component | |
| 11 | +public class CacheServiceInjector { | |
| 12 | + @Autowired | |
| 13 | + private MemberService memberService; | |
| 14 | + @Autowired | |
| 15 | + private ReportService reportService; | |
| 16 | + | |
| 17 | + @PostConstruct | |
| 18 | + public void postConstruct() { | |
| 19 | + for (CacheService svc : EnumSet.allOf(CacheService.class)) { | |
| 20 | + switch (svc) { | |
| 21 | + case LOGIN_SERVICE : svc.setService(memberService); | |
| 22 | + break; | |
| 23 | + case REPORT_SERVICE: svc.setService(reportService); | |
| 24 | + break; | |
| 25 | + default:break; | |
| 26 | + } | |
| 27 | + } | |
| 28 | + } | |
| 29 | +} |
+++ src/main/java/com/munjaon/server/cache/service/ReportService.java
... | ... | @@ -0,0 +1,22 @@ |
| 1 | +package com.munjaon.server.cache.service; | |
| 2 | + | |
| 3 | +import com.munjaon.server.cache.mapper.ReportMapper; | |
| 4 | +import com.munjaon.server.server.dto.ReportDto; | |
| 5 | +import lombok.RequiredArgsConstructor; | |
| 6 | +import lombok.extern.slf4j.Slf4j; | |
| 7 | +import org.springframework.stereotype.Service; | |
| 8 | + | |
| 9 | +@Slf4j | |
| 10 | +@Service | |
| 11 | +@RequiredArgsConstructor | |
| 12 | +public class ReportService { | |
| 13 | + private final ReportMapper reportMapper; | |
| 14 | + | |
| 15 | + public ReportDto getReportForUser(String userId) { | |
| 16 | + return reportMapper.getReportForUser(userId); | |
| 17 | + } | |
| 18 | + | |
| 19 | + public int deleteReport(String msgId) { | |
| 20 | + return reportMapper.deleteReport(msgId); | |
| 21 | + } | |
| 22 | +} |
+++ src/main/java/com/munjaon/server/cache/service/SerialNoService.java
... | ... | @@ -0,0 +1,25 @@ |
| 1 | +package com.munjaon.server.cache.service; | |
| 2 | + | |
| 3 | +import com.munjaon.server.cache.mapper.SerialNoMapper; | |
| 4 | +import lombok.RequiredArgsConstructor; | |
| 5 | +import lombok.extern.slf4j.Slf4j; | |
| 6 | +import org.springframework.stereotype.Service; | |
| 7 | +import org.springframework.transaction.annotation.Transactional; | |
| 8 | + | |
| 9 | +@Slf4j | |
| 10 | +@Service | |
| 11 | +@RequiredArgsConstructor | |
| 12 | +public class SerialNoService { | |
| 13 | + private final SerialNoMapper serialNoMapper; | |
| 14 | + | |
| 15 | + @Transactional | |
| 16 | + public String getSerialNo() { | |
| 17 | + if (serialNoMapper.getSerialNoForLock() == null) { | |
| 18 | + serialNoMapper.insert(); | |
| 19 | + } else { | |
| 20 | + serialNoMapper.update(); | |
| 21 | + } | |
| 22 | + | |
| 23 | + return serialNoMapper.getSerialNo(); | |
| 24 | + } | |
| 25 | +} |
--- src/main/java/com/munjaon/server/config/RunnerConfiguration.java
+++ src/main/java/com/munjaon/server/config/RunnerConfiguration.java
... | ... | @@ -3,6 +3,7 @@ |
| 3 | 3 |
import com.munjaon.server.queue.dto.QueueInfo; |
| 4 | 4 |
import com.munjaon.server.queue.pool.SmsReadQueue; |
| 5 | 5 |
import com.munjaon.server.queue.pool.SmsWriteQueue; |
| 6 |
+import com.munjaon.server.server.service.CollectServerService; |
|
| 6 | 7 |
import com.munjaon.server.server.service.PropertyLoader; |
| 7 | 8 |
import com.munjaon.server.server.service.QueueServerService; |
| 8 | 9 |
import com.munjaon.server.util.ServiceUtil; |
... | ... | @@ -65,4 +66,19 @@ |
| 65 | 66 |
} |
| 66 | 67 |
return args -> System.out.println("Runner Bean #2");
|
| 67 | 68 |
} |
| 69 |
+ |
|
| 70 |
+ @Bean |
|
| 71 |
+ @Order(3) |
|
| 72 |
+ public CommandLineRunner getRunnerBeanForSmsCollector() {
|
|
| 73 |
+ try {
|
|
| 74 |
+ String serviceName = "SMS_COLLECTOR"; |
|
| 75 |
+ String serviceType = serverConfig.getString(serviceName + ".SERVICE_TYPE"); |
|
| 76 |
+ int port = serverConfig.getInt(serviceName + ".SERVICE_PORT"); |
|
| 77 |
+ CollectServerService collectServerService = new CollectServerService(serviceName, serviceType, port); |
|
| 78 |
+ collectServerService.start(); |
|
| 79 |
+ } catch (Exception e) {
|
|
| 80 |
+ throw new RuntimeException(e); |
|
| 81 |
+ } |
|
| 82 |
+ return args -> System.out.println("Runner Bean #2");
|
|
| 83 |
+ } |
|
| 68 | 84 |
} |
--- src/main/java/com/munjaon/server/queue/dto/BasicMessageDto.java
+++ src/main/java/com/munjaon/server/queue/dto/BasicMessageDto.java
... | ... | @@ -8,6 +8,9 @@ |
| 8 | 8 |
@Setter |
| 9 | 9 |
@ToString |
| 10 | 10 |
public class BasicMessageDto {
|
| 11 |
+ /* 임시로 사용하는 변수 */ |
|
| 12 |
+ protected String id; |
|
| 13 |
+ |
|
| 11 | 14 |
protected String userId = ""; // 사용자 아이디 |
| 12 | 15 |
protected final String feeType = "A"; // 요금제(선불 : P / 후불 : A) |
| 13 | 16 |
protected String unitCost = ""; // 단가 |
--- src/main/java/com/munjaon/server/queue/enums/QueueTypeWorker.java
+++ src/main/java/com/munjaon/server/queue/enums/QueueTypeWorker.java
... | ... | @@ -11,6 +11,12 @@ |
| 11 | 11 |
public enum QueueTypeWorker {
|
| 12 | 12 |
MSG_TYPE_SMS("SMS") {
|
| 13 | 13 |
@Override |
| 14 |
+ public int getQueueSize() {
|
|
| 15 |
+ SmsQueueService smsQueueService = (SmsQueueService) QueueService.SMS_QUEUE_SERVICE.getService(); |
|
| 16 |
+ return smsQueueService.getQueueSize(); |
|
| 17 |
+ } |
|
| 18 |
+ |
|
| 19 |
+ @Override |
|
| 14 | 20 |
public boolean isExistQueue(String name) {
|
| 15 | 21 |
SmsQueueService smsQueueService = (SmsQueueService) QueueService.SMS_QUEUE_SERVICE.getService(); |
| 16 | 22 |
return smsQueueService.isExistQueue(name); |
... | ... | @@ -65,6 +71,12 @@ |
| 65 | 71 |
} |
| 66 | 72 |
}, |
| 67 | 73 |
MSG_TYPE_LMS("LMS") {
|
| 74 |
+ @Override |
|
| 75 |
+ public int getQueueSize() {
|
|
| 76 |
+ LmsQueueService lmsQueueService = (LmsQueueService) QueueService.LMS_QUEUE_SERVICE.getService(); |
|
| 77 |
+ return lmsQueueService.getQueueSize(); |
|
| 78 |
+ } |
|
| 79 |
+ |
|
| 68 | 80 |
@Override |
| 69 | 81 |
public boolean isExistQueue(String name) {
|
| 70 | 82 |
LmsQueueService lmsQueueService = (LmsQueueService) QueueService.LMS_QUEUE_SERVICE.getService(); |
... | ... | @@ -121,6 +133,12 @@ |
| 121 | 133 |
}, |
| 122 | 134 |
MSG_TYPE_MMS("MMS") {
|
| 123 | 135 |
@Override |
| 136 |
+ public int getQueueSize() {
|
|
| 137 |
+ MmsQueueService mmsQueueService = (MmsQueueService) QueueService.MMS_QUEUE_SERVICE.getService(); |
|
| 138 |
+ return mmsQueueService.getQueueSize(); |
|
| 139 |
+ } |
|
| 140 |
+ |
|
| 141 |
+ @Override |
|
| 124 | 142 |
public boolean isExistQueue(String name) {
|
| 125 | 143 |
MmsQueueService mmsQueueService = (MmsQueueService) QueueService.MMS_QUEUE_SERVICE.getService(); |
| 126 | 144 |
return mmsQueueService.isExistQueue(name); |
... | ... | @@ -176,6 +194,12 @@ |
| 176 | 194 |
}, |
| 177 | 195 |
MSG_TYPE_KAT("KAT") {
|
| 178 | 196 |
@Override |
| 197 |
+ public int getQueueSize() {
|
|
| 198 |
+ KakaoAlarmQueueService kakaoAlarmQueueService = (KakaoAlarmQueueService) QueueService.KAT_QUEUE_SERVICE.getService(); |
|
| 199 |
+ return kakaoAlarmQueueService.getQueueSize(); |
|
| 200 |
+ } |
|
| 201 |
+ |
|
| 202 |
+ @Override |
|
| 179 | 203 |
public boolean isExistQueue(String name) {
|
| 180 | 204 |
KakaoAlarmQueueService kakaoAlarmQueueService = (KakaoAlarmQueueService) QueueService.KAT_QUEUE_SERVICE.getService(); |
| 181 | 205 |
return kakaoAlarmQueueService.isExistQueue(name); |
... | ... | @@ -230,6 +254,12 @@ |
| 230 | 254 |
} |
| 231 | 255 |
}, |
| 232 | 256 |
MSG_TYPE_KFT("KFT") {
|
| 257 |
+ @Override |
|
| 258 |
+ public int getQueueSize() {
|
|
| 259 |
+ KakaoFriendQueueService kakaoFriendQueueService = (KakaoFriendQueueService) QueueService.KFT_QUEUE_SERVICE.getService(); |
|
| 260 |
+ return kakaoFriendQueueService.getQueueSize(); |
|
| 261 |
+ } |
|
| 262 |
+ |
|
| 233 | 263 |
@Override |
| 234 | 264 |
public boolean isExistQueue(String name) {
|
| 235 | 265 |
KakaoFriendQueueService kakaoFriendQueueService = (KakaoFriendQueueService) QueueService.KFT_QUEUE_SERVICE.getService(); |
... | ... | @@ -301,6 +331,7 @@ |
| 301 | 331 |
return null; |
| 302 | 332 |
} |
| 303 | 333 |
|
| 334 |
+ public abstract int getQueueSize(); |
|
| 304 | 335 |
public abstract boolean isExistQueue(String name); |
| 305 | 336 |
public abstract void removeQueue(String name); |
| 306 | 337 |
public abstract void addQueue(WriteQueue queue); |
+++ src/main/java/com/munjaon/server/queue/mapper/SmsMapper.java
... | ... | @@ -0,0 +1,9 @@ |
| 1 | +package com.munjaon.server.queue.mapper; | |
| 2 | + | |
| 3 | +import com.munjaon.server.queue.dto.BasicMessageDto; | |
| 4 | +import org.apache.ibatis.annotations.Mapper; | |
| 5 | + | |
| 6 | +@Mapper | |
| 7 | +public interface SmsMapper { | |
| 8 | + int insert(BasicMessageDto messageDto); | |
| 9 | +} |
--- src/main/java/com/munjaon/server/queue/pool/QueuePool.java
+++ src/main/java/com/munjaon/server/queue/pool/QueuePool.java
... | ... | @@ -16,6 +16,11 @@ |
| 16 | 16 |
/** File Queue 분배를 위한 인덱서 */ |
| 17 | 17 |
protected int queueIndex = 0; |
| 18 | 18 |
|
| 19 |
+ public int getQueueSize() {
|
|
| 20 |
+ synchronized (lockMonitor) {
|
|
| 21 |
+ return queuePool.size(); |
|
| 22 |
+ } |
|
| 23 |
+ } |
|
| 19 | 24 |
/** Queue 존재하는지 조회 */ |
| 20 | 25 |
public boolean isExistQueue(String name){
|
| 21 | 26 |
synchronized (lockMonitor) {
|
--- src/main/java/com/munjaon/server/queue/service/KakaoAlarmQueueService.java
+++ src/main/java/com/munjaon/server/queue/service/KakaoAlarmQueueService.java
... | ... | @@ -12,6 +12,12 @@ |
| 12 | 12 |
@RequiredArgsConstructor |
| 13 | 13 |
public class KakaoAlarmQueueService implements QueueAction {
|
| 14 | 14 |
private final KakaoAlarmMemoryQueue memoryQueue = KakaoAlarmMemoryQueue.getInstance(); |
| 15 |
+ |
|
| 16 |
+ @Override |
|
| 17 |
+ public int getQueueSize() {
|
|
| 18 |
+ return 0; |
|
| 19 |
+ } |
|
| 20 |
+ |
|
| 15 | 21 |
@Override |
| 16 | 22 |
public boolean isExistQueue(String name) {
|
| 17 | 23 |
return false; |
--- src/main/java/com/munjaon/server/queue/service/KakaoFriendQueueService.java
+++ src/main/java/com/munjaon/server/queue/service/KakaoFriendQueueService.java
... | ... | @@ -12,6 +12,12 @@ |
| 12 | 12 |
@RequiredArgsConstructor |
| 13 | 13 |
public class KakaoFriendQueueService implements QueueAction {
|
| 14 | 14 |
private final KakaoAlarmMemoryQueue memoryQueue = KakaoAlarmMemoryQueue.getInstance(); |
| 15 |
+ |
|
| 16 |
+ @Override |
|
| 17 |
+ public int getQueueSize() {
|
|
| 18 |
+ return 0; |
|
| 19 |
+ } |
|
| 20 |
+ |
|
| 15 | 21 |
@Override |
| 16 | 22 |
public boolean isExistQueue(String name) {
|
| 17 | 23 |
return false; |
--- src/main/java/com/munjaon/server/queue/service/LmsQueueService.java
+++ src/main/java/com/munjaon/server/queue/service/LmsQueueService.java
... | ... | @@ -16,6 +16,11 @@ |
| 16 | 16 |
private final LmsMemoryQueue memoryQueue = LmsMemoryQueue.getInstance(); |
| 17 | 17 |
|
| 18 | 18 |
@Override |
| 19 |
+ public int getQueueSize() {
|
|
| 20 |
+ return queueInstance.getQueueSize(); |
|
| 21 |
+ } |
|
| 22 |
+ |
|
| 23 |
+ @Override |
|
| 19 | 24 |
public boolean isExistQueue(String name) {
|
| 20 | 25 |
return queueInstance.isExistQueue(name); |
| 21 | 26 |
} |
--- src/main/java/com/munjaon/server/queue/service/MmsQueueService.java
+++ src/main/java/com/munjaon/server/queue/service/MmsQueueService.java
... | ... | @@ -14,6 +14,12 @@ |
| 14 | 14 |
public class MmsQueueService implements QueueAction {
|
| 15 | 15 |
private final MmsQueuePool queueInstance = MmsQueuePool.getInstance(); |
| 16 | 16 |
private final MmsMemoryQueue memoryQueue = MmsMemoryQueue.getInstance(); |
| 17 |
+ |
|
| 18 |
+ @Override |
|
| 19 |
+ public int getQueueSize() {
|
|
| 20 |
+ return queueInstance.getQueueSize(); |
|
| 21 |
+ } |
|
| 22 |
+ |
|
| 17 | 23 |
@Override |
| 18 | 24 |
public boolean isExistQueue(String name) {
|
| 19 | 25 |
return queueInstance.isExistQueue(name); |
--- src/main/java/com/munjaon/server/queue/service/QueueAction.java
+++ src/main/java/com/munjaon/server/queue/service/QueueAction.java
... | ... | @@ -4,6 +4,7 @@ |
| 4 | 4 |
import com.munjaon.server.queue.pool.WriteQueue; |
| 5 | 5 |
|
| 6 | 6 |
public interface QueueAction {
|
| 7 |
+ int getQueueSize(); |
|
| 7 | 8 |
boolean isExistQueue(String name); |
| 8 | 9 |
void removeQueue(String name); |
| 9 | 10 |
void addQueue(WriteQueue queue); |
--- src/main/java/com/munjaon/server/queue/service/SmsQueueService.java
+++ src/main/java/com/munjaon/server/queue/service/SmsQueueService.java
... | ... | @@ -1,6 +1,8 @@ |
| 1 | 1 |
package com.munjaon.server.queue.service; |
| 2 | 2 |
|
| 3 |
+import com.munjaon.server.cache.service.SerialNoService; |
|
| 3 | 4 |
import com.munjaon.server.queue.dto.BasicMessageDto; |
| 5 |
+import com.munjaon.server.queue.mapper.SmsMapper; |
|
| 4 | 6 |
import com.munjaon.server.queue.pool.SmsMemoryQueue; |
| 5 | 7 |
import com.munjaon.server.queue.pool.SmsQueuePool; |
| 6 | 8 |
import com.munjaon.server.queue.pool.WriteQueue; |
... | ... | @@ -12,8 +14,15 @@ |
| 12 | 14 |
@Service |
| 13 | 15 |
@RequiredArgsConstructor |
| 14 | 16 |
public class SmsQueueService implements QueueAction {
|
| 17 |
+ private final SmsMapper smsMapper; |
|
| 15 | 18 |
private final SmsQueuePool queueInstance = SmsQueuePool.getInstance(); |
| 16 | 19 |
private final SmsMemoryQueue memoryQueue = SmsMemoryQueue.getInstance(); |
| 20 |
+ private final SerialNoService serialNoService; |
|
| 21 |
+ |
|
| 22 |
+ @Override |
|
| 23 |
+ public int getQueueSize() {
|
|
| 24 |
+ return queueInstance.getQueueSize(); |
|
| 25 |
+ } |
|
| 17 | 26 |
|
| 18 | 27 |
@Override |
| 19 | 28 |
public boolean isExistQueue(String name) {
|
... | ... | @@ -46,8 +55,12 @@ |
| 46 | 55 |
|
| 47 | 56 |
@Override |
| 48 | 57 |
public int saveMessageToTable(BasicMessageDto data) {
|
| 58 |
+ String serialNo = serialNoService.getSerialNo(); |
|
| 59 |
+ String groupSerialNo = serialNo.replace("MSGID", "MSGGID");
|
|
| 60 |
+ data.setId(serialNo); |
|
| 61 |
+ data.setMsgGroupID(groupSerialNo); |
|
| 49 | 62 |
log.debug("Save message to table : {}", data);
|
| 50 |
- return 0; |
|
| 63 |
+ return smsMapper.insert(data); |
|
| 51 | 64 |
} |
| 52 | 65 |
|
| 53 | 66 |
@Override |
--- src/main/java/com/munjaon/server/server/dto/ConnectUserDto.java
+++ src/main/java/com/munjaon/server/server/dto/ConnectUserDto.java
... | ... | @@ -1,5 +1,6 @@ |
| 1 | 1 |
package com.munjaon.server.server.dto; |
| 2 | 2 |
|
| 3 |
+import com.munjaon.server.cache.dto.MemberDto; |
|
| 3 | 4 |
import com.munjaon.server.server.config.ServerConfig; |
| 4 | 5 |
import lombok.Builder; |
| 5 | 6 |
import lombok.Getter; |
... | ... | @@ -24,6 +25,8 @@ |
| 24 | 25 |
/* 요금제(선불 : P / 후불 : A) */ |
| 25 | 26 |
private final String feeType = "A"; |
| 26 | 27 |
|
| 28 |
+ private MemberDto memberDto; |
|
| 29 |
+ |
|
| 27 | 30 |
public int isAlive() {
|
| 28 | 31 |
if (isLogin) {
|
| 29 | 32 |
if (System.currentTimeMillis() - lastTrafficTime > ServerConfig.LIMIT_LINK_CHECK_TIMEOUT) {
|
+++ src/main/java/com/munjaon/server/server/dto/HeaderDto.java
... | ... | @@ -0,0 +1,17 @@ |
| 1 | +package com.munjaon.server.server.dto; | |
| 2 | + | |
| 3 | +import lombok.Builder; | |
| 4 | +import lombok.Getter; | |
| 5 | +import lombok.Setter; | |
| 6 | +import lombok.ToString; | |
| 7 | + | |
| 8 | +@Getter | |
| 9 | +@Setter | |
| 10 | +@Builder | |
| 11 | +@ToString | |
| 12 | +public class HeaderDto { | |
| 13 | + private boolean isError; | |
| 14 | + private String version; | |
| 15 | + private int command; | |
| 16 | + private int bodyLength; | |
| 17 | +} |
+++ src/main/java/com/munjaon/server/server/dto/ReportDto.java
... | ... | @@ -0,0 +1,19 @@ |
| 1 | +package com.munjaon.server.server.dto; | |
| 2 | + | |
| 3 | +import lombok.Getter; | |
| 4 | +import lombok.Setter; | |
| 5 | +import lombok.ToString; | |
| 6 | + | |
| 7 | +@Getter | |
| 8 | +@Setter | |
| 9 | +@ToString | |
| 10 | +public class ReportDto { | |
| 11 | + private String msgId; | |
| 12 | + private String userId; | |
| 13 | + private String agentMsgId; | |
| 14 | + private String agentCode; | |
| 15 | + private String msgType; | |
| 16 | + private String rsltDate; | |
| 17 | + private String rsltCode; | |
| 18 | + private String rsltNet; | |
| 19 | +} |
+++ src/main/java/com/munjaon/server/server/dto/ReportUserDto.java
... | ... | @@ -0,0 +1,43 @@ |
| 1 | +package com.munjaon.server.server.dto; | |
| 2 | + | |
| 3 | +import com.munjaon.server.cache.dto.MemberDto; | |
| 4 | +import com.munjaon.server.server.config.ServerConfig; | |
| 5 | +import lombok.Builder; | |
| 6 | +import lombok.Getter; | |
| 7 | +import lombok.Setter; | |
| 8 | +import lombok.ToString; | |
| 9 | + | |
| 10 | +@Getter | |
| 11 | +@Setter | |
| 12 | +@Builder | |
| 13 | +@ToString | |
| 14 | +public class ReportUserDto { | |
| 15 | + /* 로그인여부 */ | |
| 16 | + private boolean isLogin; | |
| 17 | + /* 마지막 통신 시간 */ | |
| 18 | + private Long lastTrafficTime; | |
| 19 | + /* 사용자 ID */ | |
| 20 | + private String userId; | |
| 21 | + /* 사용자 접속 IP */ | |
| 22 | + private String remoteIP; | |
| 23 | + | |
| 24 | + private MemberDto memberDto; | |
| 25 | + | |
| 26 | + public int isAlive() { | |
| 27 | + if (isLogin) { | |
| 28 | + if (System.currentTimeMillis() - lastTrafficTime > ServerConfig.LIMIT_LINK_CHECK_TIMEOUT) { | |
| 29 | + return 2; | |
| 30 | + } | |
| 31 | + } else { | |
| 32 | + if (System.currentTimeMillis() - lastTrafficTime > ServerConfig.LIMIT_BIND_TIMEOUT) { | |
| 33 | + return 1; | |
| 34 | + } | |
| 35 | + } | |
| 36 | + | |
| 37 | + return 0; | |
| 38 | + } | |
| 39 | + | |
| 40 | + public void updateLastTrafficTime() { | |
| 41 | + this.lastTrafficTime = System.currentTimeMillis(); | |
| 42 | + } | |
| 43 | +} |
+++ src/main/java/com/munjaon/server/server/packet/Bind.java
... | ... | @@ -0,0 +1,99 @@ |
| 1 | +package com.munjaon.server.server.packet; | |
| 2 | + | |
| 3 | +import java.nio.ByteBuffer; | |
| 4 | + | |
| 5 | +public final class Bind { | |
| 6 | + public static final int BIND_BODY_LENGTH = 41; | |
| 7 | + | |
| 8 | + public static final int BIND_ID_LENGTH = 20; | |
| 9 | + public static final int BIND_ID_POSITION = Header.BODY_POSITION + Header.BODY_LENGTH; | |
| 10 | + public static final int BIND_PWD_LENGTH = 20; | |
| 11 | + public static final int BIND_PWD_POSITION = BIND_ID_POSITION + BIND_ID_LENGTH; | |
| 12 | + public static final int BIND_ENCRYPTION_LENGTH = 1; | |
| 13 | + public static final int BIND_ENCRYPTION_POSITION = BIND_PWD_POSITION + BIND_PWD_LENGTH; | |
| 14 | + | |
| 15 | + public static final int BIND_ACK_BODY_LENGTH = 2; | |
| 16 | + public static final int BIND_ACK_RESULT_CODE_LENGTH = 2; | |
| 17 | + public static final int BIND_ACK_RESULT_CODE_POSITION = Header.HEADER_LENGTH; | |
| 18 | + | |
| 19 | + public static final String ENCRYPTION = "0"; | |
| 20 | + | |
| 21 | + public static ByteBuffer makeBindBuffer(String id, String pwd) { | |
| 22 | + ByteBuffer buffer = ByteBuffer.allocate(Header.HEADER_LENGTH + BIND_BODY_LENGTH); | |
| 23 | + Packet.setDefaultByte(buffer); | |
| 24 | + Header.putHeader(buffer, Header.COMMAND_BIND, BIND_BODY_LENGTH); | |
| 25 | + /* ID */ | |
| 26 | + if (id != null) { | |
| 27 | + buffer.put(BIND_ID_POSITION, id.getBytes()); | |
| 28 | + } | |
| 29 | + /* PWD */ | |
| 30 | + if (pwd != null) { | |
| 31 | + buffer.put(BIND_PWD_POSITION, pwd.getBytes()); | |
| 32 | + } | |
| 33 | + /* ENCRYPTION */ | |
| 34 | + buffer.put(BIND_ENCRYPTION_POSITION, ENCRYPTION.getBytes()); | |
| 35 | + | |
| 36 | + return buffer; | |
| 37 | + } | |
| 38 | + | |
| 39 | + public static ByteBuffer makeBindAckBuffer(String resultCode) { | |
| 40 | + ByteBuffer buffer = ByteBuffer.allocate(Header.HEADER_LENGTH + BIND_ACK_BODY_LENGTH); | |
| 41 | + Packet.setDefaultByte(buffer); | |
| 42 | + Header.putHeader(buffer, Header.COMMAND_BIND_ACK, BIND_ACK_BODY_LENGTH); | |
| 43 | + /* resultCode */ | |
| 44 | + if (resultCode != null) { | |
| 45 | + buffer.put(BIND_ACK_RESULT_CODE_POSITION, resultCode.getBytes()); | |
| 46 | + } | |
| 47 | + | |
| 48 | + return buffer; | |
| 49 | + } | |
| 50 | + | |
| 51 | + public static String getBindId(final ByteBuffer buffer) { | |
| 52 | + if (buffer == null) { | |
| 53 | + return null; | |
| 54 | + } | |
| 55 | + | |
| 56 | + buffer.position(BIND_ID_POSITION); | |
| 57 | + byte[] destArray = new byte[BIND_ID_LENGTH]; | |
| 58 | + buffer.get(destArray); | |
| 59 | + | |
| 60 | + return Packet.getString(destArray); | |
| 61 | + } | |
| 62 | + | |
| 63 | + public static String getBindPwd(final ByteBuffer buffer) { | |
| 64 | + if (buffer == null) { | |
| 65 | + return null; | |
| 66 | + } | |
| 67 | + | |
| 68 | + buffer.position(BIND_PWD_POSITION); | |
| 69 | + byte[] destArray = new byte[BIND_PWD_LENGTH]; | |
| 70 | + buffer.get(destArray); | |
| 71 | + | |
| 72 | + return Packet.getString(destArray); | |
| 73 | + } | |
| 74 | + | |
| 75 | + public static String getBindEncryption(final ByteBuffer buffer) { | |
| 76 | + if (buffer == null) { | |
| 77 | + return null; | |
| 78 | + } | |
| 79 | + | |
| 80 | + buffer.position(BIND_ENCRYPTION_POSITION); | |
| 81 | + byte[] destArray = new byte[BIND_ENCRYPTION_LENGTH]; | |
| 82 | + buffer.get(destArray); | |
| 83 | + | |
| 84 | + return Packet.getString(destArray); | |
| 85 | + } | |
| 86 | + | |
| 87 | + public static String getBindAckResultCode(final ByteBuffer buffer) { | |
| 88 | + if (buffer == null) { | |
| 89 | + return null; | |
| 90 | + } | |
| 91 | + | |
| 92 | + buffer.position(BIND_ACK_RESULT_CODE_POSITION); | |
| 93 | + byte[] destArray = new byte[BIND_ACK_RESULT_CODE_LENGTH]; | |
| 94 | + buffer.get(destArray); | |
| 95 | + | |
| 96 | +// return new String(destArray); | |
| 97 | + return Packet.getString(destArray); | |
| 98 | + } | |
| 99 | +} |
+++ src/main/java/com/munjaon/server/server/packet/CommonMessage.java
... | ... | @@ -0,0 +1,182 @@ |
| 1 | +package com.munjaon.server.server.packet; | |
| 2 | + | |
| 3 | +import com.munjaon.server.util.CommonUtil; | |
| 4 | + | |
| 5 | +import java.nio.ByteBuffer; | |
| 6 | + | |
| 7 | +public final class CommonMessage { | |
| 8 | + /* DELIVER */ | |
| 9 | + /* MSG_ID */ | |
| 10 | + public static final int DELIVER_MESSAGE_ID_LENGTH = 20; | |
| 11 | + public static final int DELIVER_MESSAGE_ID_POSITION = Header.BODY_POSITION + Header.BODY_LENGTH; | |
| 12 | + /* SENDER */ | |
| 13 | + public static final int DELIVER_SENDER_LENGTH = 15; | |
| 14 | + public static final int DELIVER_SENDER_POSITION = DELIVER_MESSAGE_ID_POSITION + DELIVER_MESSAGE_ID_LENGTH; | |
| 15 | + /* RECEIVER */ | |
| 16 | + public static final int DELIVER_RECEIVER_LENGTH = 15; | |
| 17 | + public static final int DELIVER_RECEIVER_POSITION = DELIVER_SENDER_POSITION + DELIVER_SENDER_LENGTH; | |
| 18 | + /* RESERVE_TIME */ | |
| 19 | + public static final int DELIVER_RESERVE_TIME_LENGTH = 14; | |
| 20 | + public static final int DELIVER_RESERVE_TIME_POSITION = DELIVER_RECEIVER_POSITION + DELIVER_RECEIVER_LENGTH; | |
| 21 | + /* REQUEST_TIME */ | |
| 22 | + public static final int DELIVER_REQUEST_TIME_LENGTH = 14; | |
| 23 | + public static final int DELIVER_REQUEST_TIME_POSITION = DELIVER_RESERVE_TIME_POSITION + DELIVER_RESERVE_TIME_LENGTH; | |
| 24 | + /* MSG_TYPE */ | |
| 25 | + public static final int DELIVER_MSG_TYPE_LENGTH = 1; | |
| 26 | + public static final int DELIVER_MSG_TYPE_POSITION = DELIVER_REQUEST_TIME_POSITION + DELIVER_REQUEST_TIME_LENGTH; | |
| 27 | + | |
| 28 | + /* DELIVER_ACK */ | |
| 29 | + /* MSG_ID */ | |
| 30 | + public static final int DELIVER_ACK_MESSAGE_ID_LENGTH = 20; | |
| 31 | + public static final int DELIVER_ACK_MESSAGE_ID_POSITION = Header.BODY_POSITION + Header.BODY_LENGTH; | |
| 32 | + /* RESULT */ | |
| 33 | + public static final int DELIVER_ACK_RESULT_LENGTH = 1; | |
| 34 | + public static final int DELIVER_ACK_RESULT_POSITION = DELIVER_ACK_MESSAGE_ID_POSITION + DELIVER_ACK_MESSAGE_ID_LENGTH; | |
| 35 | + | |
| 36 | + public static void putMessageIdForDeliver(ByteBuffer buffer, String messageId) { | |
| 37 | + if (buffer == null || messageId == null) { | |
| 38 | + return; | |
| 39 | + } | |
| 40 | + buffer.put(DELIVER_MESSAGE_ID_POSITION, messageId.getBytes()); | |
| 41 | + } | |
| 42 | + public static String getMessageIdForDeliver(ByteBuffer buffer) { | |
| 43 | + if (buffer == null) { | |
| 44 | + return null; | |
| 45 | + } | |
| 46 | + | |
| 47 | + buffer.position(DELIVER_MESSAGE_ID_POSITION); | |
| 48 | + byte[] destArray = new byte[DELIVER_MESSAGE_ID_LENGTH]; | |
| 49 | + buffer.get(destArray); | |
| 50 | + | |
| 51 | + return Packet.getString(destArray); | |
| 52 | + } | |
| 53 | + | |
| 54 | + public static void putSenderForDeliver(ByteBuffer buffer, String sender) { | |
| 55 | + if (buffer == null || sender == null) { | |
| 56 | + return; | |
| 57 | + } | |
| 58 | + sender = CommonUtil.cutString(CommonUtil.doNumber(sender), DELIVER_SENDER_LENGTH); | |
| 59 | + buffer.put(DELIVER_SENDER_POSITION, sender.getBytes()); | |
| 60 | + } | |
| 61 | + public static String getSenderForDeliver(ByteBuffer buffer) { | |
| 62 | + if (buffer == null) { | |
| 63 | + return null; | |
| 64 | + } | |
| 65 | + | |
| 66 | + buffer.position(DELIVER_SENDER_POSITION); | |
| 67 | + byte[] destArray = new byte[DELIVER_SENDER_LENGTH]; | |
| 68 | + buffer.get(destArray); | |
| 69 | + | |
| 70 | + return Packet.getString(destArray); | |
| 71 | + } | |
| 72 | + | |
| 73 | + public static void putReceiverForDeliver(ByteBuffer buffer, String receiver) { | |
| 74 | + if (buffer == null || receiver == null) { | |
| 75 | + return; | |
| 76 | + } | |
| 77 | + receiver = CommonUtil.cutString(CommonUtil.doNumber(receiver), DELIVER_RECEIVER_LENGTH); | |
| 78 | + buffer.put(DELIVER_RECEIVER_POSITION, receiver.getBytes()); | |
| 79 | + } | |
| 80 | + public static String getReceiverForDeliver(ByteBuffer buffer) { | |
| 81 | + if (buffer == null) { | |
| 82 | + return null; | |
| 83 | + } | |
| 84 | + | |
| 85 | + buffer.position(DELIVER_RECEIVER_POSITION); | |
| 86 | + byte[] destArray = new byte[DELIVER_RECEIVER_LENGTH]; | |
| 87 | + buffer.get(destArray); | |
| 88 | + | |
| 89 | + return Packet.getString(destArray); | |
| 90 | + } | |
| 91 | + | |
| 92 | + public static void putReserveTimeForDeliver(ByteBuffer buffer, String reserveTime) { | |
| 93 | + if (buffer == null || reserveTime == null) { | |
| 94 | + return; | |
| 95 | + } | |
| 96 | + buffer.put(DELIVER_RESERVE_TIME_POSITION, reserveTime.getBytes()); | |
| 97 | + } | |
| 98 | + public static String getReserveTimeForDeliver(ByteBuffer buffer) { | |
| 99 | + if (buffer == null) { | |
| 100 | + return null; | |
| 101 | + } | |
| 102 | + | |
| 103 | + buffer.position(DELIVER_RESERVE_TIME_POSITION); | |
| 104 | + byte[] destArray = new byte[DELIVER_RESERVE_TIME_LENGTH]; | |
| 105 | + buffer.get(destArray); | |
| 106 | + | |
| 107 | + return Packet.getString(destArray); | |
| 108 | + } | |
| 109 | + | |
| 110 | + public static void putRequestTimeForDeliver(ByteBuffer buffer, String requestTime) { | |
| 111 | + if (buffer == null || requestTime == null) { | |
| 112 | + return; | |
| 113 | + } | |
| 114 | + buffer.put(DELIVER_REQUEST_TIME_POSITION, requestTime.getBytes()); | |
| 115 | + } | |
| 116 | + public static String getRequestTimeForDeliver(ByteBuffer buffer) { | |
| 117 | + if (buffer == null) { | |
| 118 | + return null; | |
| 119 | + } | |
| 120 | + | |
| 121 | + buffer.position(DELIVER_REQUEST_TIME_POSITION); | |
| 122 | + byte[] destArray = new byte[DELIVER_REQUEST_TIME_LENGTH]; | |
| 123 | + buffer.get(destArray); | |
| 124 | + | |
| 125 | + return Packet.getString(destArray); | |
| 126 | + } | |
| 127 | + | |
| 128 | + public static void putMsgTypeForDeliver(ByteBuffer buffer, String msgType) { | |
| 129 | + if (buffer == null || msgType == null) { | |
| 130 | + return; | |
| 131 | + } | |
| 132 | + buffer.put(DELIVER_MSG_TYPE_POSITION, msgType.getBytes()); | |
| 133 | + } | |
| 134 | + public static String getMsgTypeForDeliver(ByteBuffer buffer) { | |
| 135 | + if (buffer == null) { | |
| 136 | + return null; | |
| 137 | + } | |
| 138 | + | |
| 139 | + buffer.position(DELIVER_MSG_TYPE_POSITION); | |
| 140 | + byte[] destArray = new byte[DELIVER_MSG_TYPE_LENGTH]; | |
| 141 | + buffer.get(destArray); | |
| 142 | + | |
| 143 | + return Packet.getString(destArray); | |
| 144 | + } | |
| 145 | + | |
| 146 | + | |
| 147 | + public static void putMessageIdForDeliverAck(ByteBuffer buffer, String messageId) { | |
| 148 | + if (buffer == null || messageId == null) { | |
| 149 | + return; | |
| 150 | + } | |
| 151 | + buffer.put(DELIVER_ACK_MESSAGE_ID_POSITION, messageId.getBytes()); | |
| 152 | + } | |
| 153 | + public static String getMessageIdForDeliverAck(ByteBuffer buffer) { | |
| 154 | + if (buffer == null) { | |
| 155 | + return null; | |
| 156 | + } | |
| 157 | + | |
| 158 | + buffer.position(DELIVER_ACK_MESSAGE_ID_POSITION); | |
| 159 | + byte[] destArray = new byte[DELIVER_ACK_MESSAGE_ID_LENGTH]; | |
| 160 | + buffer.get(destArray); | |
| 161 | + | |
| 162 | + return Packet.getString(destArray); | |
| 163 | + } | |
| 164 | + | |
| 165 | + public static void putResultForDeliverAck(ByteBuffer buffer, String result) { | |
| 166 | + if (buffer == null || result == null) { | |
| 167 | + return; | |
| 168 | + } | |
| 169 | + buffer.put(DELIVER_ACK_RESULT_POSITION, result.getBytes()); | |
| 170 | + } | |
| 171 | + public static String getResultForDeliverAck(ByteBuffer buffer) { | |
| 172 | + if (buffer == null) { | |
| 173 | + return null; | |
| 174 | + } | |
| 175 | + | |
| 176 | + buffer.position(DELIVER_ACK_RESULT_POSITION); | |
| 177 | + byte[] destArray = new byte[DELIVER_ACK_RESULT_LENGTH]; | |
| 178 | + buffer.get(destArray); | |
| 179 | + | |
| 180 | + return Packet.getString(destArray); | |
| 181 | + } | |
| 182 | +} |
+++ src/main/java/com/munjaon/server/server/packet/Header.java
... | ... | @@ -0,0 +1,109 @@ |
| 1 | +package com.munjaon.server.server.packet; | |
| 2 | + | |
| 3 | +import com.munjaon.server.util.ByteUtil; | |
| 4 | + | |
| 5 | +import java.nio.ByteBuffer; | |
| 6 | + | |
| 7 | +public final class Header { | |
| 8 | + public static final int HEADER_LENGTH = 10; | |
| 9 | + | |
| 10 | + public static final String VERSION = "ITN10"; | |
| 11 | + public static final int VERSION_LENGTH = 5; | |
| 12 | + public static final int VERSION_POSITION = 0; | |
| 13 | + | |
| 14 | + public static final int COMMAND_LENGTH = 1; | |
| 15 | + public static final int COMMAND_POSITION = VERSION_POSITION + VERSION_LENGTH; | |
| 16 | + | |
| 17 | + public static final String COMMAND_BIND = "1"; | |
| 18 | + public static final String COMMAND_BIND_ACK = "2"; | |
| 19 | + public static final String COMMAND_DELIVER = "3"; | |
| 20 | + public static final String COMMAND_DELIVER_ACK = "4"; | |
| 21 | + public static final String COMMAND_REPORT = "5"; | |
| 22 | + public static final String COMMAND_REPORT_ACK = "6"; | |
| 23 | + public static final String COMMAND_LINK_CHECK = "7"; | |
| 24 | + public static final String COMMAND_LINK_CHECK_ACK = "8"; | |
| 25 | + | |
| 26 | + public static final int BODY_LENGTH = 4; | |
| 27 | + public static final int BODY_POSITION = COMMAND_POSITION + COMMAND_LENGTH; | |
| 28 | + | |
| 29 | + public static final int BODY_BIND_LENGTH = 41; | |
| 30 | + public static final int BODY_BIND_ACK_LENGTH = 2; | |
| 31 | + public static final int BODY_LINK_CHECK_LENGTH = 3; | |
| 32 | + public static final int BODY_LINK_CHECK_ACK_LENGTH = 3; | |
| 33 | + public static final int BODY_DELIVER_SMS_LENGTH = 239; | |
| 34 | + public static final int BODY_DELIVER_SMS_ACK_LENGTH = 21; | |
| 35 | + public static final int BODY_DELIVER_LMS_LENGTH = 2091; | |
| 36 | + public static final int BODY_DELIVER_LMS_ACK_LENGTH = 21; | |
| 37 | + public static final int BODY_DELIVER_MMS_LENGTH = 2091; | |
| 38 | + public static final int BODY_DELIVER_MMS_ACK_LENGTH = 21; | |
| 39 | + public static final int BODY_REPORT_LENGTH = 58; | |
| 40 | + public static final int BODY_REPORT_ACK_LENGTH = 1; | |
| 41 | + | |
| 42 | + public static void putVersion(final ByteBuffer buffer) { | |
| 43 | + if (buffer == null) { | |
| 44 | + return; | |
| 45 | + } | |
| 46 | + buffer.put(VERSION_POSITION, VERSION.getBytes()); | |
| 47 | + } | |
| 48 | + public static String getVersion(final ByteBuffer buffer) { | |
| 49 | + if (buffer == null) { | |
| 50 | + return null; | |
| 51 | + } | |
| 52 | + | |
| 53 | + buffer.position(VERSION_POSITION); | |
| 54 | + byte[] destArray = new byte[VERSION_LENGTH]; | |
| 55 | + buffer.get(destArray); | |
| 56 | + | |
| 57 | + return Packet.getString(destArray); | |
| 58 | +// return new String(destArray); | |
| 59 | + } | |
| 60 | + | |
| 61 | + public static void putCommand(final ByteBuffer buffer, String command) { | |
| 62 | + if (buffer == null) { | |
| 63 | + return; | |
| 64 | + } | |
| 65 | + buffer.put(COMMAND_POSITION, command.getBytes()); | |
| 66 | + } | |
| 67 | + public static String getCommand(final ByteBuffer buffer) { | |
| 68 | + if (buffer == null) { | |
| 69 | + return null; | |
| 70 | + } | |
| 71 | + | |
| 72 | + buffer.position(COMMAND_POSITION); | |
| 73 | + byte[] destArray = new byte[COMMAND_LENGTH]; | |
| 74 | + buffer.get(destArray); | |
| 75 | + | |
| 76 | + return Packet.getString(destArray); | |
| 77 | + } | |
| 78 | + | |
| 79 | + public static void putBodyLength(final ByteBuffer buffer, int bodyLength) { | |
| 80 | + putBodyLength(buffer, Integer.toString(bodyLength)); | |
| 81 | + } | |
| 82 | + public static String getBodyLength(final ByteBuffer buffer) { | |
| 83 | + if (buffer == null) { | |
| 84 | + return null; | |
| 85 | + } | |
| 86 | + | |
| 87 | + buffer.position(BODY_POSITION); | |
| 88 | + byte[] destArray = new byte[BODY_LENGTH]; | |
| 89 | + buffer.get(destArray); | |
| 90 | + System.out.println(ByteUtil.byteToHex(destArray)); | |
| 91 | + | |
| 92 | + return Packet.getString(destArray); | |
| 93 | + } | |
| 94 | + | |
| 95 | + public static void putBodyLength(final ByteBuffer buffer, String bodyLength) { | |
| 96 | + if (buffer == null) { | |
| 97 | + return; | |
| 98 | + } | |
| 99 | + buffer.put(BODY_POSITION, bodyLength.getBytes()); | |
| 100 | + } | |
| 101 | + public static void putHeader(final ByteBuffer buffer, String command, int bodyLength) { | |
| 102 | + putHeader(buffer, command, Integer.toString(bodyLength)); | |
| 103 | + } | |
| 104 | + public static void putHeader(final ByteBuffer buffer, String command, String bodyLength) { | |
| 105 | + putVersion(buffer); | |
| 106 | + putCommand(buffer, command); | |
| 107 | + putBodyLength(buffer, bodyLength); | |
| 108 | + } | |
| 109 | +} |
+++ src/main/java/com/munjaon/server/server/packet/LinkCheck.java
... | ... | @@ -0,0 +1,43 @@ |
| 1 | +package com.munjaon.server.server.packet; | |
| 2 | + | |
| 3 | +import java.nio.ByteBuffer; | |
| 4 | + | |
| 5 | +public final class LinkCheck { | |
| 6 | + public static final int LINK_CHECK_BODY_LENGTH = 3; | |
| 7 | + public static final int LINK_CHECK_BODY_POSITION = Header.HEADER_LENGTH; | |
| 8 | + public static final int LINK_CHECK_ACK_BODY_LENGTH = 3; | |
| 9 | + public static final int LINK_CHECK_ACK_BODY_POSITION = Header.HEADER_LENGTH; | |
| 10 | + | |
| 11 | + public static String LINK_CHECK_VALUE = "100"; | |
| 12 | + public static String LINK_CHECK_ACK_VALUE = "100"; | |
| 13 | + | |
| 14 | + public static ByteBuffer makeLinkCheckBuffer() { | |
| 15 | + ByteBuffer buffer = ByteBuffer.allocate(Header.HEADER_LENGTH + LINK_CHECK_BODY_LENGTH); | |
| 16 | + Packet.setDefaultByte(buffer); | |
| 17 | + Header.putHeader(buffer, Header.COMMAND_LINK_CHECK, LINK_CHECK_BODY_LENGTH); | |
| 18 | + buffer.put(LINK_CHECK_BODY_POSITION, LINK_CHECK_VALUE.getBytes()); | |
| 19 | + | |
| 20 | + return buffer; | |
| 21 | + } | |
| 22 | + | |
| 23 | + public static ByteBuffer makeLinkCheckAckBuffer() { | |
| 24 | + ByteBuffer buffer = ByteBuffer.allocate(Header.HEADER_LENGTH + LINK_CHECK_ACK_BODY_LENGTH); | |
| 25 | + Packet.setDefaultByte(buffer); | |
| 26 | + Header.putHeader(buffer, Header.COMMAND_LINK_CHECK, LINK_CHECK_ACK_BODY_LENGTH); | |
| 27 | + buffer.put(LINK_CHECK_ACK_BODY_POSITION, LINK_CHECK_ACK_VALUE.getBytes()); | |
| 28 | + | |
| 29 | + return buffer; | |
| 30 | + } | |
| 31 | +// public static ByteBuffer bufferForSend; | |
| 32 | +// public static ByteBuffer bufferForAck; | |
| 33 | +// | |
| 34 | +// static { | |
| 35 | +// bufferForSend = ByteBuffer.allocateDirect(Header.HEADER_LENGTH + LINK_CHECK_BODY_LENGTH); | |
| 36 | +// Header.putHeader(bufferForSend, Header.COMMAND_LINK_CHECK, LINK_CHECK_BODY_LENGTH); | |
| 37 | +// bufferForSend.put(LINK_CHECK_BODY_POSITION, LINK_CHECK_VALUE.getBytes()); | |
| 38 | +// | |
| 39 | +// bufferForAck = ByteBuffer.allocateDirect(Header.HEADER_LENGTH + LINK_CHECK_ACK_BODY_LENGTH); | |
| 40 | +// Header.putHeader(bufferForAck, Header.COMMAND_LINK_CHECK_ACK, LINK_CHECK_ACK_BODY_LENGTH); | |
| 41 | +// bufferForSend.put(LINK_CHECK_ACK_BODY_POSITION, LINK_CHECK_ACK_VALUE.getBytes()); | |
| 42 | +// } | |
| 43 | +} |
+++ src/main/java/com/munjaon/server/server/packet/Packet.java
... | ... | @@ -0,0 +1,57 @@ |
| 1 | +package com.munjaon.server.server.packet; | |
| 2 | + | |
| 3 | +import java.nio.ByteBuffer; | |
| 4 | + | |
| 5 | +public final class Packet { | |
| 6 | + public static final byte SET_DEFAULT_BYTE = (byte) 0x00; | |
| 7 | + public static final long LINK_CHECK_CYCLE = 30000L; | |
| 8 | + public static void setDefaultByte(ByteBuffer buffer) { | |
| 9 | + if (buffer == null) { | |
| 10 | + return; | |
| 11 | + } | |
| 12 | +// buffer.clear(); | |
| 13 | + for (int i = 0; i < buffer.capacity(); i++) { | |
| 14 | + buffer.put(i, SET_DEFAULT_BYTE); | |
| 15 | + } | |
| 16 | + } | |
| 17 | + | |
| 18 | + public static String getString(byte[] srcArray) { | |
| 19 | + if (srcArray == null) { | |
| 20 | + return null; | |
| 21 | + } | |
| 22 | + int size = 0; | |
| 23 | + for (int i = 0, len = srcArray.length; i < len; i++) { | |
| 24 | + if (srcArray[i] == SET_DEFAULT_BYTE) { | |
| 25 | + continue; | |
| 26 | + } | |
| 27 | + size++; | |
| 28 | + } | |
| 29 | + byte[] destArray = null; | |
| 30 | + if (size > 0) { | |
| 31 | + destArray = new byte[size]; | |
| 32 | + int index = 0; | |
| 33 | + for (int i = 0, len = srcArray.length; i < len; i++) { | |
| 34 | + if (srcArray[i] == SET_DEFAULT_BYTE) { | |
| 35 | + continue; | |
| 36 | + } | |
| 37 | + destArray[index++] = srcArray[i]; | |
| 38 | + } | |
| 39 | + } | |
| 40 | + | |
| 41 | + return destArray == null ? null : new String(destArray); | |
| 42 | + } | |
| 43 | + | |
| 44 | + public static void mergeBuffers(ByteBuffer dest, ByteBuffer srcHead, ByteBuffer srcBody) { | |
| 45 | + if (dest == null || srcHead == null || srcBody == null) { | |
| 46 | + return; | |
| 47 | + } | |
| 48 | + if (dest.capacity() != (srcHead.capacity() + srcBody.capacity())) { | |
| 49 | + return; | |
| 50 | + } | |
| 51 | + byte[] srcHeadArray = srcHead.array(); | |
| 52 | + byte[] srcBodyArray = srcBody.array(); | |
| 53 | + | |
| 54 | + dest.put(0, srcHeadArray); | |
| 55 | + dest.put(srcHeadArray.length, srcBodyArray); | |
| 56 | + } | |
| 57 | +} |
+++ src/main/java/com/munjaon/server/server/packet/SmsMessage.java
... | ... | @@ -0,0 +1,44 @@ |
| 1 | +package com.munjaon.server.server.packet; | |
| 2 | + | |
| 3 | +import com.munjaon.server.util.CommonUtil; | |
| 4 | + | |
| 5 | +import java.nio.ByteBuffer; | |
| 6 | + | |
| 7 | +public final class SmsMessage { | |
| 8 | + public static final int DELIVER_SMS_BODY_LENGTH = 239; | |
| 9 | + public static final int DELIVER_SMS_ACK_BODY_LENGTH = 21; | |
| 10 | + | |
| 11 | + /* DELIVER */ | |
| 12 | + /* MESSAGE */ | |
| 13 | + public static final int DELIVER_MESSAGE_LENGTH = 160; | |
| 14 | + public static final int DELIVER_MESSAGE_POSITION = CommonMessage.DELIVER_MSG_TYPE_POSITION + CommonMessage.DELIVER_MSG_TYPE_LENGTH; | |
| 15 | + | |
| 16 | + public static void putMessageForDeliver(ByteBuffer buffer, String message) { | |
| 17 | + if (buffer == null || message == null) { | |
| 18 | + return; | |
| 19 | + } | |
| 20 | + message = CommonUtil.cutString(message, DELIVER_MESSAGE_LENGTH); | |
| 21 | + buffer.put(DELIVER_MESSAGE_POSITION, message.getBytes()); | |
| 22 | + } | |
| 23 | + public static String getMessageForDeliver(ByteBuffer buffer) { | |
| 24 | + if (buffer == null) { | |
| 25 | + return null; | |
| 26 | + } | |
| 27 | + | |
| 28 | + buffer.position(DELIVER_MESSAGE_POSITION); | |
| 29 | + byte[] destArray = new byte[DELIVER_MESSAGE_LENGTH]; | |
| 30 | + buffer.get(destArray); | |
| 31 | + | |
| 32 | + return Packet.getString(destArray); | |
| 33 | + } | |
| 34 | + | |
| 35 | + public static ByteBuffer makeDeliverAckBuffer(String msgId, String status) { | |
| 36 | + ByteBuffer buffer = ByteBuffer.allocate(Header.HEADER_LENGTH + DELIVER_SMS_ACK_BODY_LENGTH); | |
| 37 | + Packet.setDefaultByte(buffer); | |
| 38 | + Header.putHeader(buffer, Header.COMMAND_DELIVER_ACK, DELIVER_SMS_ACK_BODY_LENGTH); | |
| 39 | + buffer.put(CommonMessage.DELIVER_ACK_MESSAGE_ID_POSITION, msgId.getBytes()); | |
| 40 | + buffer.put(CommonMessage.DELIVER_ACK_RESULT_POSITION, status.getBytes()); | |
| 41 | + | |
| 42 | + return buffer; | |
| 43 | + } | |
| 44 | +} |
--- src/main/java/com/munjaon/server/server/sample/ExecutorServiceTest4.java
+++ src/main/java/com/munjaon/server/server/sample/ExecutorServiceTest4.java
... | ... | @@ -14,10 +14,10 @@ |
| 14 | 14 |
service.submit("job3");
|
| 15 | 15 |
service.submit("job4");
|
| 16 | 16 |
|
| 17 |
- for (int i = 0 ; i < 4; i++) {
|
|
| 18 |
- String result = service.take(); |
|
| 19 |
- System.out.println(result); |
|
| 20 |
- } |
|
| 17 |
+// for (int i = 0 ; i < 4; i++) {
|
|
| 18 |
+// String result = service.take(); |
|
| 19 |
+// System.out.println(result); |
|
| 20 |
+// } |
|
| 21 | 21 |
|
| 22 | 22 |
System.out.println("end");
|
| 23 | 23 |
service.close(); |
... | ... | @@ -33,14 +33,14 @@ |
| 33 | 33 |
|
| 34 | 34 |
public void submit(String job) {
|
| 35 | 35 |
executor.submit(() -> {
|
| 36 |
- String threadName = Thread.currentThread().getName(); |
|
| 36 |
+// String threadName = Thread.currentThread().getName(); |
|
| 37 | 37 |
System.out.println("finished " + job);
|
| 38 |
- String result = job + ", " + threadName; |
|
| 39 |
- try {
|
|
| 40 |
- queue.put(result); |
|
| 41 |
- } catch (InterruptedException e) {
|
|
| 42 |
- Thread.currentThread().interrupt(); |
|
| 43 |
- } |
|
| 38 |
+// String result = job + ", " + threadName; |
|
| 39 |
+// try {
|
|
| 40 |
+// queue.put(result); |
|
| 41 |
+// } catch (InterruptedException e) {
|
|
| 42 |
+// Thread.currentThread().interrupt(); |
|
| 43 |
+// } |
|
| 44 | 44 |
}); |
| 45 | 45 |
} |
| 46 | 46 |
|
+++ src/main/java/com/munjaon/server/server/service/CollectServerService.java
... | ... | @@ -0,0 +1,403 @@ |
| 1 | +package com.munjaon.server.server.service; | |
| 2 | + | |
| 3 | +import com.munjaon.server.cache.dto.MemberDto; | |
| 4 | +import com.munjaon.server.cache.enums.CacheService; | |
| 5 | +import com.munjaon.server.cache.service.MemberService; | |
| 6 | +import com.munjaon.server.queue.dto.BasicMessageDto; | |
| 7 | +import com.munjaon.server.queue.enums.QueueTypeWorker; | |
| 8 | +import com.munjaon.server.server.dto.ConnectUserDto; | |
| 9 | +import com.munjaon.server.server.dto.HeaderDto; | |
| 10 | +import com.munjaon.server.server.packet.*; | |
| 11 | +import com.munjaon.server.util.LogUtil; | |
| 12 | +import lombok.Getter; | |
| 13 | +import org.json.simple.JSONObject; | |
| 14 | + | |
| 15 | +import java.io.IOException; | |
| 16 | +import java.net.InetSocketAddress; | |
| 17 | +import java.net.Socket; | |
| 18 | +import java.net.SocketAddress; | |
| 19 | +import java.nio.ByteBuffer; | |
| 20 | +import java.nio.channels.SelectionKey; | |
| 21 | +import java.nio.channels.Selector; | |
| 22 | +import java.nio.channels.ServerSocketChannel; | |
| 23 | +import java.nio.channels.SocketChannel; | |
| 24 | +import java.time.LocalDateTime; | |
| 25 | +import java.time.format.DateTimeFormatter; | |
| 26 | +import java.util.Iterator; | |
| 27 | +import java.util.List; | |
| 28 | +import java.util.Map; | |
| 29 | +import java.util.concurrent.ConcurrentHashMap; | |
| 30 | +import java.util.concurrent.ExecutorService; | |
| 31 | +import java.util.concurrent.Executors; | |
| 32 | + | |
| 33 | +public class CollectServerService extends Service { | |
| 34 | + private final InetSocketAddress listenAddress; | |
| 35 | + private CollectorThreadService threadService; | |
| 36 | + private Selector selector; | |
| 37 | + private final String serviceType; | |
| 38 | + | |
| 39 | + public CollectServerService(String serviceName, String serviceType, int port) { | |
| 40 | + super(serviceName); | |
| 41 | + this.listenAddress = new InetSocketAddress(port); | |
| 42 | + this.serviceType = serviceType; | |
| 43 | + } | |
| 44 | + @Override | |
| 45 | + public void checkReady() { | |
| 46 | + QueueTypeWorker worker = QueueTypeWorker.find(this.serviceType); | |
| 47 | + if (worker != null && worker.getQueueSize() > 0) { | |
| 48 | + this.IS_READY_YN = true; | |
| 49 | + } else { | |
| 50 | + this.IS_READY_YN = false; | |
| 51 | + } | |
| 52 | + } | |
| 53 | + | |
| 54 | + @Override | |
| 55 | + public void initResources() { | |
| 56 | + try { | |
| 57 | + initCollectChannel(); | |
| 58 | + threadService = new CollectorThreadService(8, this.serviceType, logger); | |
| 59 | + } catch (IOException e) { | |
| 60 | + saveSystemLog(e); | |
| 61 | + throw new RuntimeException(e); | |
| 62 | + } | |
| 63 | + } | |
| 64 | + | |
| 65 | + private void initCollectChannel() throws IOException { | |
| 66 | + selector = Selector.open(); | |
| 67 | + /* 채널 생성 */ | |
| 68 | + ServerSocketChannel serverChannel = ServerSocketChannel.open(); | |
| 69 | + /* non-Blocking 설정 */ | |
| 70 | + serverChannel.configureBlocking(false); | |
| 71 | + /* 서버 ip, port 설정 */ | |
| 72 | + serverChannel.socket().bind(listenAddress); | |
| 73 | + /* 채널에 accept 대기 설정 */ | |
| 74 | + serverChannel.register(selector, SelectionKey.OP_ACCEPT); | |
| 75 | + } | |
| 76 | + | |
| 77 | + private void closeCollectChannel() throws IOException { | |
| 78 | + selector.close(); | |
| 79 | + } | |
| 80 | + | |
| 81 | + @Override | |
| 82 | + public void releaseResources() { | |
| 83 | + try { | |
| 84 | + closeCollectChannel(); | |
| 85 | + threadService.close(); | |
| 86 | + } catch (IOException e) { | |
| 87 | + saveSystemLog(e); | |
| 88 | + throw new RuntimeException(e); | |
| 89 | + } | |
| 90 | + } | |
| 91 | + | |
| 92 | + @Override | |
| 93 | + public void doService() { | |
| 94 | + while (isRun()) { | |
| 95 | + try { | |
| 96 | + execInterest(); | |
| 97 | + } catch (Exception e) { | |
| 98 | + throw new RuntimeException(e); | |
| 99 | + } | |
| 100 | + } | |
| 101 | + } | |
| 102 | + | |
| 103 | + private void execInterest() throws IOException { | |
| 104 | + if (selector.select(1000) == 0) { | |
| 105 | + return ; | |
| 106 | + } | |
| 107 | + Iterator<SelectionKey> keys = selector.selectedKeys().iterator(); | |
| 108 | + while (keys.hasNext()) { | |
| 109 | + SelectionKey key = keys.next(); | |
| 110 | + if (key.isValid()) { | |
| 111 | + if (key.isAcceptable()) { // 접속일 경우.. | |
| 112 | + saveSystemLog("isAcceptable"); | |
| 113 | + threadService.submit(selector, key, 1); | |
| 114 | + } else if (key.isReadable()) { // 수신일 경우.. | |
| 115 | + saveSystemLog("isReadable"); | |
| 116 | + threadService.submit(selector, key, 2); | |
| 117 | + } else if (key.isWritable()) { // 발신일 경우.. | |
| 118 | + saveSystemLog("isWritable"); | |
| 119 | + threadService.submit(selector, key, 3); | |
| 120 | + } | |
| 121 | + } | |
| 122 | + /* 키 셋에서 제거. */ | |
| 123 | + keys.remove(); | |
| 124 | + } | |
| 125 | + } | |
| 126 | + | |
| 127 | + @Override | |
| 128 | + public JSONObject monitorService() { | |
| 129 | + return null; | |
| 130 | + } | |
| 131 | + | |
| 132 | + private static class CollectorThreadService { | |
| 133 | + @Getter | |
| 134 | + private String serviceType; | |
| 135 | + @Getter | |
| 136 | + private final int maxCore; | |
| 137 | + private final ExecutorService executor; | |
| 138 | + private final Map<String, ConnectUserDto> connectUserMap = new ConcurrentHashMap<>(); | |
| 139 | + private final LogUtil logger; | |
| 140 | + | |
| 141 | + public CollectorThreadService(String serviceType, LogUtil logger) { | |
| 142 | + this(Runtime.getRuntime().availableProcessors(), serviceType, logger); | |
| 143 | + } | |
| 144 | + | |
| 145 | + public CollectorThreadService(int maxCore, String serviceType, LogUtil logger) { | |
| 146 | + this.maxCore = maxCore; | |
| 147 | + this.executor = Executors.newFixedThreadPool(maxCore); | |
| 148 | + this.logger = logger; | |
| 149 | + } | |
| 150 | + | |
| 151 | + public void submit(Selector selector, SelectionKey key, int interestOps) { | |
| 152 | + executor.submit(() -> { | |
| 153 | + switch (interestOps) { | |
| 154 | + case 1 : accept(selector, key); break; | |
| 155 | + case 2 : read(selector, key); break; | |
| 156 | + case 3 : write(selector, key); break; | |
| 157 | + default : break; | |
| 158 | + } | |
| 159 | + }); | |
| 160 | + } | |
| 161 | + | |
| 162 | + private void accept(Selector selector, SelectionKey key) { | |
| 163 | + try { | |
| 164 | + /* 키 채널을 가져온다. */ | |
| 165 | + ServerSocketChannel serverChannel = (ServerSocketChannel) key.channel(); | |
| 166 | + /* accept을 해서 Socket 채널을 가져온다. */ | |
| 167 | + SocketChannel channel = serverChannel.accept(); | |
| 168 | + channel.configureBlocking(false); | |
| 169 | + /* 소켓 취득 */ | |
| 170 | + Socket socket = channel.socket(); | |
| 171 | + SocketAddress remoteAddr = socket.getRemoteSocketAddress(); | |
| 172 | + saveSystemLog("accept : " + Thread.currentThread().getName()); | |
| 173 | + saveSystemLog("Connected to: " + remoteAddr); | |
| 174 | + // Socket 채널을 channel에 수신 등록한다 | |
| 175 | + channel.register(selector, SelectionKey.OP_READ, ConnectUserDto.builder().lastTrafficTime(System.currentTimeMillis()).remoteIP(remoteAddr.toString()).build()); | |
| 176 | + } catch (Exception e) { | |
| 177 | + throw new RuntimeException(e); | |
| 178 | + } | |
| 179 | + } | |
| 180 | + | |
| 181 | +// private void read(Selector selector, SelectionKey key) { | |
| 182 | +// try { | |
| 183 | +// saveSystemLog("read : " + Thread.currentThread().getName()); | |
| 184 | +// // 키 채널을 가져온다. | |
| 185 | +// SocketChannel channel = (SocketChannel) key.channel(); | |
| 186 | +// ConnectUserDto userDto = (ConnectUserDto) key.attachment(); | |
| 187 | +// | |
| 188 | +// int size = -1; | |
| 189 | +// | |
| 190 | +// ByteBuffer headBuffer = ByteBuffer.allocate(Header.HEADER_LENGTH); | |
| 191 | +// ByteBuffer bindBuffer = ByteBuffer.allocate(Bind.BIND_BODY_LENGTH); | |
| 192 | +// ByteBuffer linkBuffer = ByteBuffer.allocate(LinkCheck.LINK_CHECK_BODY_LENGTH); | |
| 193 | +// ByteBuffer msgBuffer = ByteBuffer.allocate(SmsMessage.DELIVER_SMS_BODY_LENGTH); | |
| 194 | +// try { | |
| 195 | +// size = channel.read(headBuffer); | |
| 196 | +// String command = Header.getCommand(headBuffer); | |
| 197 | +// System.out.println("command : " + command); | |
| 198 | +// if ("1".equals(command)) { | |
| 199 | +// size = channel.read(bindBuffer); | |
| 200 | +// } else if ("3".equals(command)) { | |
| 201 | +// size = channel.read(msgBuffer); | |
| 202 | +// } else if ("7".equals(command)) { | |
| 203 | +// size = channel.read(linkBuffer); | |
| 204 | +// } else { | |
| 205 | +// size = -1; | |
| 206 | +// } | |
| 207 | +// | |
| 208 | +// System.out.println("size : " + size); | |
| 209 | +// } catch (IOException e) {} | |
| 210 | +// if (size < 0) { | |
| 211 | +// expireConnectUser(key); | |
| 212 | +// } else if (size > 0) { | |
| 213 | +//// String command = Header.getCommand(buffer); | |
| 214 | +//// saveSystemLog("command : " + command); | |
| 215 | +//// switch (Integer.parseInt(command)) { | |
| 216 | +//// case 1 : recvBind(channel, buffer, userDto); break; | |
| 217 | +//// case 3 : recvDeliver(channel, buffer, userDto); break; | |
| 218 | +//// case 7 : recvLinkCheck(key); break; | |
| 219 | +//// default: expireConnectUser(key); break; | |
| 220 | +//// } | |
| 221 | +// } | |
| 222 | +// } catch (Exception e) { | |
| 223 | +// e.printStackTrace(); | |
| 224 | +// } | |
| 225 | +// System.out.println("read"); | |
| 226 | +// } | |
| 227 | + | |
| 228 | + private void read(Selector selector, SelectionKey key) { | |
| 229 | + try { | |
| 230 | + saveSystemLog("read : " + Thread.currentThread().getName()); | |
| 231 | + // 키 채널을 가져온다. | |
| 232 | + SocketChannel channel = (SocketChannel) key.channel(); | |
| 233 | + ConnectUserDto userDto = (ConnectUserDto) key.attachment(); | |
| 234 | + | |
| 235 | + int size = -1; | |
| 236 | + ByteBuffer buffer = ByteBuffer.allocate(256); | |
| 237 | + try { | |
| 238 | + size = channel.read(buffer); | |
| 239 | + } catch (IOException e) {} | |
| 240 | + if (size < 0) { | |
| 241 | + expireConnectUser(key); | |
| 242 | + } else if (size > 0) { | |
| 243 | + String command = Header.getCommand(buffer); | |
| 244 | + saveSystemLog("command : " + command); | |
| 245 | + switch (Integer.parseInt(command)) { | |
| 246 | + case 1 : recvBind(channel, buffer, userDto); break; | |
| 247 | + case 3 : recvDeliver(channel, buffer, userDto); break; | |
| 248 | + case 7 : recvLinkCheck(key); break; | |
| 249 | + default: expireConnectUser(key); break; | |
| 250 | + } | |
| 251 | + } | |
| 252 | + } catch (Exception e) { | |
| 253 | + e.printStackTrace(); | |
| 254 | + } | |
| 255 | + System.out.println("read"); | |
| 256 | + } | |
| 257 | + | |
| 258 | + private void recvBind(SocketChannel channel, ByteBuffer buffer, ConnectUserDto userDto) { | |
| 259 | + String resultCode = "00"; | |
| 260 | + try { | |
| 261 | + String id = Bind.getBindId(buffer); | |
| 262 | + String pwd = Bind.getBindPwd(buffer); | |
| 263 | + saveSystemLog("id : " + id); | |
| 264 | + saveSystemLog("pwd : " + pwd); | |
| 265 | + if (id == null || pwd == null) { | |
| 266 | + resultCode = "50"; | |
| 267 | + } else { | |
| 268 | + if (connectUserMap.containsKey(id)) { | |
| 269 | + resultCode = "60"; | |
| 270 | + } else { | |
| 271 | + MemberService svc = (MemberService) CacheService.LOGIN_SERVICE.getService(); | |
| 272 | + MemberDto memberDto = null; | |
| 273 | + if (svc != null) { | |
| 274 | + memberDto = svc.get(id); | |
| 275 | + } | |
| 276 | + if (memberDto == null || !pwd.equals(memberDto.getAccessKey())) { | |
| 277 | + resultCode = "20"; | |
| 278 | + } else { | |
| 279 | + userDto.setUserId(id); | |
| 280 | + userDto.setLogin(true); | |
| 281 | + userDto.setMemberDto(memberDto); | |
| 282 | + } | |
| 283 | + } | |
| 284 | + } | |
| 285 | + } catch (Exception e) { | |
| 286 | + resultCode = "10"; | |
| 287 | + e.printStackTrace(); | |
| 288 | + } | |
| 289 | + | |
| 290 | + try { | |
| 291 | + saveSystemLog("resultCode : " + resultCode); | |
| 292 | + channel.write(Bind.makeBindAckBuffer(resultCode)); | |
| 293 | + } catch (IOException e) { | |
| 294 | + e.printStackTrace(); | |
| 295 | + } | |
| 296 | + } | |
| 297 | + | |
| 298 | + private void recvDeliver(SocketChannel channel, ByteBuffer buffer, ConnectUserDto userDto) throws IOException { | |
| 299 | + BasicMessageDto messageDto = new BasicMessageDto(); | |
| 300 | + messageDto.setRouterSeq("40"); | |
| 301 | + messageDto.setServiceType("4"); | |
| 302 | + messageDto.setUserId(userDto.getUserId()); | |
| 303 | + messageDto.setRemoteIP(userDto.getRemoteIP()); | |
| 304 | + messageDto.setSendStatus("0"); | |
| 305 | + messageDto.setUserMsgID(CommonMessage.getMessageIdForDeliver(buffer)); | |
| 306 | + messageDto.setUserSender(CommonMessage.getSenderForDeliver(buffer)); | |
| 307 | + messageDto.setUserReceiver(CommonMessage.getReceiverForDeliver(buffer)); | |
| 308 | + messageDto.setReserveDt(CommonMessage.getReserveTimeForDeliver(buffer)); | |
| 309 | + messageDto.setRequestDt(CommonMessage.getRequestTimeForDeliver(buffer)); | |
| 310 | + messageDto.setUnitCost("10.4"); | |
| 311 | + messageDto.setUserMessage(SmsMessage.getMessageForDeliver(buffer)); | |
| 312 | + | |
| 313 | + QueueTypeWorker worker = QueueTypeWorker.find("SMS"); | |
| 314 | + if (worker != null) { | |
| 315 | + worker.pushQueue(messageDto); | |
| 316 | + channel.write(SmsMessage.makeDeliverAckBuffer(messageDto.getUserMsgID(), messageDto.getSendStatus())); | |
| 317 | + } | |
| 318 | + } | |
| 319 | + private void recvLinkCheck(SelectionKey key) throws IOException { | |
| 320 | + SocketChannel channel = (SocketChannel) key.channel(); | |
| 321 | + channel.write(LinkCheck.makeLinkCheckAckBuffer()); | |
| 322 | + } | |
| 323 | + private void expireConnectUser(SelectionKey key) { | |
| 324 | + if (key == null || !key.isValid()) { | |
| 325 | + return; | |
| 326 | + } | |
| 327 | + try { | |
| 328 | + SocketChannel channel = (SocketChannel) key.channel(); | |
| 329 | + ConnectUserDto userDto = (ConnectUserDto) key.attachment(); | |
| 330 | + if (userDto != null && userDto.getUserId() != null) { | |
| 331 | + connectUserMap.remove(userDto.getUserId()); | |
| 332 | + key.attach(null); | |
| 333 | + } | |
| 334 | + // 소켓 채널 닫기 | |
| 335 | + channel.close(); | |
| 336 | + // 키 닫기 | |
| 337 | + key.cancel(); | |
| 338 | + } catch (IOException e) { | |
| 339 | + e.printStackTrace(); | |
| 340 | + } | |
| 341 | + } | |
| 342 | + private HeaderDto getHeader(SocketChannel channel) { | |
| 343 | + HeaderDto headerDto = HeaderDto.builder().build(); | |
| 344 | + int size = -1; | |
| 345 | + ByteBuffer buffer = ByteBuffer.allocate(Header.HEADER_LENGTH); | |
| 346 | + if (channel != null) { | |
| 347 | + try { | |
| 348 | + saveSystemLog("Key is valid : "); | |
| 349 | +// SocketChannel channel = (SocketChannel) key.channel(); | |
| 350 | + size = channel.read(buffer); | |
| 351 | + } catch (IOException e) {} | |
| 352 | + } | |
| 353 | + | |
| 354 | + if (size < 0) { | |
| 355 | + saveSystemLog("Is Error : "); | |
| 356 | + headerDto.setError(true); | |
| 357 | + } else { | |
| 358 | + saveSystemLog("version : " + Header.getVersion(buffer)); | |
| 359 | + saveSystemLog("Command : " + Header.getCommand(buffer)); | |
| 360 | + saveSystemLog("BodyLength : " + Header.getBodyLength(buffer)); | |
| 361 | + headerDto.setVersion(Header.getVersion(buffer)); | |
| 362 | + headerDto.setCommand(Integer.parseInt(Header.getCommand(buffer))); | |
| 363 | + headerDto.setBodyLength(Integer.parseInt(Header.getBodyLength(buffer))); | |
| 364 | + } | |
| 365 | + | |
| 366 | + saveSystemLog("READ HEADER : " + size); | |
| 367 | + | |
| 368 | + return headerDto; | |
| 369 | + } | |
| 370 | + | |
| 371 | + private void write(Selector selector, SelectionKey key) { | |
| 372 | + System.out.println("write"); | |
| 373 | + } | |
| 374 | + | |
| 375 | + private void saveSystemLog(Object obj) { | |
| 376 | + saveLog(obj, true); | |
| 377 | + } | |
| 378 | + | |
| 379 | + private void saveLog(Object obj) { | |
| 380 | + saveLog(obj, false); | |
| 381 | + } | |
| 382 | + | |
| 383 | + private void saveLog(Object obj, boolean isConsoleOutput) { | |
| 384 | + if (isConsoleOutput) { | |
| 385 | + System.out.println(LocalDateTime.now().format(DateTimeFormatter.ofPattern(LOG_DATE_FORMAT)) + " {{" + serviceType + "}} " + obj); | |
| 386 | + } | |
| 387 | + | |
| 388 | + if (logger == null) { | |
| 389 | + return; | |
| 390 | + } | |
| 391 | + | |
| 392 | + logger.log(obj); | |
| 393 | + } | |
| 394 | + | |
| 395 | + public void close() { | |
| 396 | + List<Runnable> unfinishedTasks = executor.shutdownNow(); | |
| 397 | + connectUserMap.clear(); | |
| 398 | + if (!unfinishedTasks.isEmpty()) { | |
| 399 | + saveSystemLog("Not all tasks finished before calling close: " + unfinishedTasks.size()); | |
| 400 | + } | |
| 401 | + } | |
| 402 | + } | |
| 403 | +} |
+++ src/main/java/com/munjaon/server/server/service/ReportServerService.java
... | ... | @@ -0,0 +1,226 @@ |
| 1 | +package com.munjaon.server.server.service; | |
| 2 | + | |
| 3 | +import com.munjaon.server.server.dto.ConnectUserDto; | |
| 4 | +import com.munjaon.server.util.LogUtil; | |
| 5 | +import lombok.Getter; | |
| 6 | +import org.json.simple.JSONObject; | |
| 7 | + | |
| 8 | +import java.io.IOException; | |
| 9 | +import java.net.InetSocketAddress; | |
| 10 | +import java.net.Socket; | |
| 11 | +import java.net.SocketAddress; | |
| 12 | +import java.nio.channels.SelectionKey; | |
| 13 | +import java.nio.channels.Selector; | |
| 14 | +import java.nio.channels.ServerSocketChannel; | |
| 15 | +import java.nio.channels.SocketChannel; | |
| 16 | +import java.time.LocalDateTime; | |
| 17 | +import java.time.format.DateTimeFormatter; | |
| 18 | +import java.util.Iterator; | |
| 19 | +import java.util.List; | |
| 20 | +import java.util.Map; | |
| 21 | +import java.util.concurrent.ConcurrentHashMap; | |
| 22 | +import java.util.concurrent.ExecutorService; | |
| 23 | +import java.util.concurrent.Executors; | |
| 24 | + | |
| 25 | +public class ReportServerService extends Service { | |
| 26 | + private final InetSocketAddress listenAddress; | |
| 27 | + private final Map<String, ConnectUserDto> connectUserMap = new ConcurrentHashMap<>(); | |
| 28 | + private ReporterThreadService threadService; | |
| 29 | + private Selector selector; | |
| 30 | + | |
| 31 | + public ReportServerService(String serviceName, int port) { | |
| 32 | + super(serviceName); | |
| 33 | + this.listenAddress = new InetSocketAddress(port); | |
| 34 | + } | |
| 35 | + @Override | |
| 36 | + public void checkReady() { | |
| 37 | + this.IS_READY_YN = true; | |
| 38 | + } | |
| 39 | + | |
| 40 | + @Override | |
| 41 | + public void initResources() { | |
| 42 | + try { | |
| 43 | + initReportChannel(); | |
| 44 | + threadService = new ReporterThreadService(8, logger); | |
| 45 | + } catch (IOException e) { | |
| 46 | + saveSystemLog(e); | |
| 47 | + throw new RuntimeException(e); | |
| 48 | + } | |
| 49 | + } | |
| 50 | + | |
| 51 | + private void initReportChannel() throws IOException { | |
| 52 | + selector = Selector.open(); | |
| 53 | + /* 채널 생성 */ | |
| 54 | + ServerSocketChannel serverChannel = ServerSocketChannel.open(); | |
| 55 | + /* non-Blocking 설정 */ | |
| 56 | + serverChannel.configureBlocking(false); | |
| 57 | + /* 서버 ip, port 설정 */ | |
| 58 | + serverChannel.socket().bind(listenAddress); | |
| 59 | + /* 채널에 accept 대기 설정 */ | |
| 60 | + serverChannel.register(selector, SelectionKey.OP_ACCEPT); | |
| 61 | + } | |
| 62 | + | |
| 63 | + private void closeReportChannel() throws IOException { | |
| 64 | + selector.close(); | |
| 65 | + } | |
| 66 | + | |
| 67 | + @Override | |
| 68 | + public void releaseResources() { | |
| 69 | + try { | |
| 70 | + closeReportChannel(); | |
| 71 | + threadService.close(); | |
| 72 | + } catch (IOException e) { | |
| 73 | + saveSystemLog(e); | |
| 74 | + throw new RuntimeException(e); | |
| 75 | + } | |
| 76 | + } | |
| 77 | + | |
| 78 | + @Override | |
| 79 | + public void doService() { | |
| 80 | + | |
| 81 | + } | |
| 82 | + | |
| 83 | + private void execInterest() throws IOException { | |
| 84 | + if (selector.select(1000) == 0) { | |
| 85 | + return ; | |
| 86 | + } | |
| 87 | + Iterator<SelectionKey> keys = selector.selectedKeys().iterator(); | |
| 88 | + while (keys.hasNext()) { | |
| 89 | + SelectionKey key = keys.next(); | |
| 90 | + if (key.isValid()) { | |
| 91 | + if (key.isAcceptable()) { // 접속일 경우.. | |
| 92 | + saveSystemLog("isAcceptable"); | |
| 93 | + accept(selector, key); | |
| 94 | + } else if (key.isReadable()) { // 수신일 경우.. | |
| 95 | + saveSystemLog("isReadable"); | |
| 96 | +// threadService.submit(selector, key, 2); | |
| 97 | + } | |
| 98 | + } | |
| 99 | + /* 키 셋에서 제거. */ | |
| 100 | + keys.remove(); | |
| 101 | + } | |
| 102 | + } | |
| 103 | + | |
| 104 | + private void accept(Selector selector, SelectionKey key) { | |
| 105 | + try { | |
| 106 | + /* 키 채널을 가져온다. */ | |
| 107 | + ServerSocketChannel serverChannel = (ServerSocketChannel) key.channel(); | |
| 108 | + /* accept을 해서 Socket 채널을 가져온다. */ | |
| 109 | + SocketChannel channel = serverChannel.accept(); | |
| 110 | + channel.configureBlocking(false); | |
| 111 | + /* 소켓 취득 */ | |
| 112 | + Socket socket = channel.socket(); | |
| 113 | + SocketAddress remoteAddr = socket.getRemoteSocketAddress(); | |
| 114 | + saveSystemLog("Connected to: " + remoteAddr); | |
| 115 | + // Socket 채널을 channel에 수신 등록한다 | |
| 116 | + channel.register(selector, SelectionKey.OP_READ, ConnectUserDto.builder().lastTrafficTime(System.currentTimeMillis()).remoteIP(remoteAddr.toString()).build()); | |
| 117 | + } catch (Exception e) { | |
| 118 | + throw new RuntimeException(e); | |
| 119 | + } | |
| 120 | + } | |
| 121 | + | |
| 122 | +// private void read(Selector selector, SelectionKey key) { | |
| 123 | +// try { | |
| 124 | +// saveSystemLog("read : " + Thread.currentThread().getName()); | |
| 125 | +// // 키 채널을 가져온다. | |
| 126 | +// SocketChannel channel = (SocketChannel) key.channel(); | |
| 127 | +// ConnectUserDto userDto = (ConnectUserDto) key.attachment(); | |
| 128 | +// | |
| 129 | +// int size = -1; | |
| 130 | +// ByteBuffer buffer = ByteBuffer.allocate(256); | |
| 131 | +// try { | |
| 132 | +// size = channel.read(buffer); | |
| 133 | +// } catch (IOException e) {} | |
| 134 | +// if (size < 0) { | |
| 135 | +// expireConnectUser(key); | |
| 136 | +// } else if (size > 0) { | |
| 137 | +// String command = Header.getCommand(buffer); | |
| 138 | +// saveSystemLog("command : " + command); | |
| 139 | +// switch (Integer.parseInt(command)) { | |
| 140 | +// case 1 : recvBind(channel, buffer, userDto); break; | |
| 141 | +// case 6 : recvDeliver(channel, buffer, userDto); break; | |
| 142 | +// case 8 : recvLinkCheck(key); break; | |
| 143 | +// default: expireConnectUser(key); break; | |
| 144 | +// } | |
| 145 | +// } | |
| 146 | +// } catch (Exception e) { | |
| 147 | +// e.printStackTrace(); | |
| 148 | +// } | |
| 149 | +// System.out.println("read"); | |
| 150 | +// } | |
| 151 | + | |
| 152 | + private void expireConnectUser(SelectionKey key) { | |
| 153 | + if (key == null || !key.isValid()) { | |
| 154 | + return; | |
| 155 | + } | |
| 156 | + try { | |
| 157 | + SocketChannel channel = (SocketChannel) key.channel(); | |
| 158 | + ConnectUserDto userDto = (ConnectUserDto) key.attachment(); | |
| 159 | + if (userDto != null && userDto.getUserId() != null) { | |
| 160 | + connectUserMap.remove(userDto.getUserId()); | |
| 161 | + key.attach(null); | |
| 162 | + } | |
| 163 | + // 소켓 채널 닫기 | |
| 164 | + channel.close(); | |
| 165 | + // 키 닫기 | |
| 166 | + key.cancel(); | |
| 167 | + } catch (IOException e) { | |
| 168 | + e.printStackTrace(); | |
| 169 | + } | |
| 170 | + } | |
| 171 | + | |
| 172 | + @Override | |
| 173 | + public JSONObject monitorService() { | |
| 174 | + return null; | |
| 175 | + } | |
| 176 | + | |
| 177 | + private static class ReporterThreadService { | |
| 178 | + @Getter | |
| 179 | + private final int maxCore; | |
| 180 | + private final ExecutorService executor; | |
| 181 | + private final Map<String, ConnectUserDto> connectUserMap = new ConcurrentHashMap<>(); | |
| 182 | + private final LogUtil logger; | |
| 183 | + | |
| 184 | + public ReporterThreadService(LogUtil logger) { | |
| 185 | + this(Runtime.getRuntime().availableProcessors(), logger); | |
| 186 | + } | |
| 187 | + | |
| 188 | + public ReporterThreadService(int maxCore, LogUtil logger) { | |
| 189 | + this.maxCore = maxCore; | |
| 190 | + this.executor = Executors.newFixedThreadPool(maxCore); | |
| 191 | + this.logger = logger; | |
| 192 | + } | |
| 193 | + | |
| 194 | + private void write(Selector selector, SelectionKey key) { | |
| 195 | + System.out.println("write"); | |
| 196 | + } | |
| 197 | + | |
| 198 | + private void saveSystemLog(Object obj) { | |
| 199 | + saveLog(obj, true); | |
| 200 | + } | |
| 201 | + | |
| 202 | + private void saveLog(Object obj) { | |
| 203 | + saveLog(obj, false); | |
| 204 | + } | |
| 205 | + | |
| 206 | + private void saveLog(Object obj, boolean isConsoleOutput) { | |
| 207 | + if (isConsoleOutput) { | |
| 208 | + System.out.println(LocalDateTime.now().format(DateTimeFormatter.ofPattern(LOG_DATE_FORMAT)) + " {{ReporterThreadService}} " + obj); | |
| 209 | + } | |
| 210 | + | |
| 211 | + if (logger == null) { | |
| 212 | + return; | |
| 213 | + } | |
| 214 | + | |
| 215 | + logger.log(obj); | |
| 216 | + } | |
| 217 | + | |
| 218 | + public void close() { | |
| 219 | + List<Runnable> unfinishedTasks = executor.shutdownNow(); | |
| 220 | + connectUserMap.clear(); | |
| 221 | + if (!unfinishedTasks.isEmpty()) { | |
| 222 | + saveSystemLog("Not all tasks finished before calling close: " + unfinishedTasks.size()); | |
| 223 | + } | |
| 224 | + } | |
| 225 | + } | |
| 226 | +} |
--- src/main/java/com/munjaon/server/server/service/SocketServerService.java
... | ... | @@ -1,30 +0,0 @@ |
| 1 | -package com.munjaon.server.server.service; | |
| 2 | - | |
| 3 | -import org.json.simple.JSONObject; | |
| 4 | - | |
| 5 | -public class SocketServerService extends Service { | |
| 6 | - @Override | |
| 7 | - public void checkReady() { | |
| 8 | - | |
| 9 | - } | |
| 10 | - | |
| 11 | - @Override | |
| 12 | - public void initResources() { | |
| 13 | - | |
| 14 | - } | |
| 15 | - | |
| 16 | - @Override | |
| 17 | - public void releaseResources() { | |
| 18 | - | |
| 19 | - } | |
| 20 | - | |
| 21 | - @Override | |
| 22 | - public void doService() { | |
| 23 | - | |
| 24 | - } | |
| 25 | - | |
| 26 | - @Override | |
| 27 | - public JSONObject monitorService() { | |
| 28 | - return null; | |
| 29 | - } | |
| 30 | -} |
--- src/main/java/com/munjaon/server/util/CommonUtil.java
+++ src/main/java/com/munjaon/server/util/CommonUtil.java
... | ... | @@ -69,7 +69,7 @@ |
| 69 | 69 |
return isValid; |
| 70 | 70 |
} |
| 71 | 71 |
// 해당 길이만큼 문자열을 자르는 함수 |
| 72 |
- public static String CutString(String str,int limit) |
|
| 72 |
+ public static String cutString(String str, int limit) |
|
| 73 | 73 |
{
|
| 74 | 74 |
int len = str.length(); |
| 75 | 75 |
int sumLength=0; |
--- src/main/java/com/munjaon/server/util/SerialNoUtil.java
+++ src/main/java/com/munjaon/server/util/SerialNoUtil.java
... | ... | @@ -5,7 +5,7 @@ |
| 5 | 5 |
|
| 6 | 6 |
public final class SerialNoUtil {
|
| 7 | 7 |
public static String getSerialNo() {
|
| 8 |
- return LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd")) + getMultiAlphabet(3) + getMultiNumeric(3) + getMultiAlphabet(2) + getMultiNumeric(6);
|
|
| 8 |
+ return getMultiAlphabet(3) + getMultiNumeric(3) + getMultiAlphabet(2) + getMultiNumeric(6); |
|
| 9 | 9 |
} |
| 10 | 10 |
|
| 11 | 11 |
public static String getMultiAlphabet(final int count) {
|
+++ src/main/resources/sqlmap/report_sql.xml
... | ... | @@ -0,0 +1,22 @@ |
| 1 | +<?xml version="1.0" encoding="UTF-8"?> | |
| 2 | +<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> | |
| 3 | +<mapper namespace="com.munjaon.server.cache.mapper.ReportMapper"> | |
| 4 | + <select id="getReportForUser" resultType="ReportDto"> | |
| 5 | + /* ReportMapper.getReportForUser */ | |
| 6 | + SELECT | |
| 7 | + MSG_ID | |
| 8 | + , USER_ID | |
| 9 | + , AGENT_MSG_ID | |
| 10 | + , AGENT_CODE | |
| 11 | + , MSG_TYPE | |
| 12 | + , RSLT_DATE | |
| 13 | + , RSLT_CODE | |
| 14 | + , RSLT_NET | |
| 15 | + FROM mj_msg_report | |
| 16 | + WHERE USER_ID = #{userId} | |
| 17 | + LIMIT 1 | |
| 18 | + </select> | |
| 19 | + <delete id="deleteReport"> | |
| 20 | + DELETE FROM mj_msg_report WHERE MSG_ID = #{msgId} | |
| 21 | + </delete> | |
| 22 | +</mapper>(No newline at end of file) |
+++ src/main/resources/sqlmap/serialno_sql.xml
... | ... | @@ -0,0 +1,29 @@ |
| 1 | +<?xml version="1.0" encoding="UTF-8"?> | |
| 2 | +<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> | |
| 3 | +<mapper namespace="com.munjaon.server.cache.mapper.SerialNoMapper"> | |
| 4 | + <select id="getSerialNoForLock" resultType="string"> | |
| 5 | + /* SerialNoMapper.getSerialNoForLock */ | |
| 6 | + SELECT NEXT_ID | |
| 7 | + FROM IDS | |
| 8 | + WHERE TABLE_NAME = 'MSG_ID' | |
| 9 | + FOR UPDATE | |
| 10 | + </select> | |
| 11 | + | |
| 12 | + <insert id="insert"> | |
| 13 | + /* SerialNoMapper.insert */ | |
| 14 | + INSERT IDS (TABLE_NAME, NEXT_ID) VALUES ('MSG_ID', 1) | |
| 15 | + </insert> | |
| 16 | + | |
| 17 | + <update id="update"> | |
| 18 | + /* SerialNoMapper.update */ | |
| 19 | + UPDATE IDS SET NEXT_ID = NEXT_ID + 1 | |
| 20 | + WHERE TABLE_NAME = 'MSG_ID' | |
| 21 | + </update> | |
| 22 | + | |
| 23 | + <select id="getSerialNo" resultType="string"> | |
| 24 | + /* SerialNoMapper.getSerialNo */ | |
| 25 | + SELECT CONCAT('MSGID_', lpad(NEXT_ID, 14, '0')) | |
| 26 | + FROM IDS | |
| 27 | + WHERE TABLE_NAME = 'MSG_ID' | |
| 28 | + </select> | |
| 29 | +</mapper>(No newline at end of file) |
+++ src/main/resources/sqlmap/sms_sql.xml
... | ... | @@ -0,0 +1,20 @@ |
| 1 | +<?xml version="1.0" encoding="UTF-8"?> | |
| 2 | +<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> | |
| 3 | +<mapper namespace="com.munjaon.server.queue.mapper.SmsMapper"> | |
| 4 | + <insert id="insert"> | |
| 5 | + INSERT INTO MJ_MSG_DATA ( | |
| 6 | + MSG_ID | |
| 7 | + , MSG_GROUP_ID | |
| 8 | + , USER_ID | |
| 9 | + , AGENT_MSG_ID | |
| 10 | + , AGENT_CODE | |
| 11 | + , CUR_STATE | |
| 12 | + , REQ_DATE | |
| 13 | + , CALL_TO | |
| 14 | + , CALL_FROM | |
| 15 | + , SUBJECT | |
| 16 | + , SMS_TXT | |
| 17 | + , MSG_TYPE | |
| 18 | + )VALUES (#{id}, #{msgGroupID}, #{userId}, #{userMsgID}, '04', 0, NOW(), #{userReceiver}, #{userSender}, NULL, #{userMessage}, '4' ) | |
| 19 | + </insert> | |
| 20 | +</mapper>(No newline at end of file) |
Add a comment
Delete comment
Once you delete this comment, you won't be able to recover it. Are you sure you want to delete this comment?