Small updates to IntentManager and TCPSocket while creating a "PrimaryManager"

This commit is contained in:
Obique PSWG
2018-01-20 12:52:46 -06:00
parent 9e3c80cca8
commit 2275d64eb2
3 changed files with 94 additions and 8 deletions
@@ -27,6 +27,9 @@
***********************************************************************************/
package com.projectswg.common.control;
import com.projectswg.common.concurrency.PswgThreadPool;
import com.projectswg.common.debug.Log;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@@ -39,9 +42,6 @@ import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;
import com.projectswg.common.concurrency.PswgThreadPool;
import com.projectswg.common.debug.Log;
public class IntentManager {
private static final AtomicReference<IntentManager> INSTANCE = new AtomicReference<>(null);
@@ -70,6 +70,7 @@ public class IntentManager {
if (!initialized.getAndSet(false))
return;
processThreads.stop(true);
processThreads.awaitTermination(1000);
}
public int getIntentCount() {
@@ -0,0 +1,73 @@
/***********************************************************************************
* Copyright (c) 2018 /// Project SWG /// www.projectswg.com *
* *
* ProjectSWG is the first NGE emulator for Star Wars Galaxies founded on *
* July 7th, 2011 after SOE announced the official shutdown of Star Wars Galaxies. *
* Our goal is to create an emulator which will provide a server for players to *
* continue playing a game similar to the one they used to play. We are basing *
* it on the final publish of the game prior to end-game events. *
* *
* This file is part of Holocore. *
* *
* --------------------------------------------------------------------------------*
* *
* Holocore is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Affero General Public License as *
* published by the Free Software Foundation, either version 3 of the *
* License, or (at your option) any later version. *
* *
* Holocore is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Affero General Public License for more details. *
* *
* You should have received a copy of the GNU Affero General Public License *
* along with Holocore. If not, see <http://www.gnu.org/licenses/>. *
* *
***********************************************************************************/
package com.projectswg.common.control;
public class PrimaryManager {
private final Manager manager;
public PrimaryManager() {
this(new DefaultManager());
}
public PrimaryManager(Manager manager) {
this.manager = manager;
}
public void addChildService(Service service) {
manager.addChildService(service);
}
public boolean initialize() {
return manager.initialize();
}
public boolean start() {
boolean ret = manager.start();
if (!ret)
terminate();
return ret;
}
public boolean isOperational() {
return manager.isOperational();
}
public boolean stop() {
return manager.stop();
}
public boolean terminate() {
return manager.terminate();
}
private static class DefaultManager extends Manager {
}
}
@@ -46,7 +46,7 @@ public class TCPSocket {
private final CallbackManager<TCPSocketCallback> callbackManager;
private final TCPSocketListener listener;
private final InetSocketAddress address;
private final AtomicReference<InetSocketAddress> address;
private final AtomicReference<SocketState> state;
private final Object stateMutex;
private final int bufferSize;
@@ -57,7 +57,7 @@ public class TCPSocket {
public TCPSocket(InetSocketAddress address, int bufferSize) {
this.callbackManager = new CallbackManager<>("tcpsocket-"+address, 1);
this.listener = new TCPSocketListener();
this.address = address;
this.address = new AtomicReference<>(address);
this.state = new AtomicReference<>(SocketState.CLOSED);
this.stateMutex = new Object();
this.bufferSize = bufferSize;
@@ -67,12 +67,24 @@ public class TCPSocket {
this.socketOutputStream = null;
}
public TCPSocket(int bufferSize) {
this(null, bufferSize);
}
public TCPSocket() {
this(1024);
}
public int getBufferSize() {
return bufferSize;
}
public InetSocketAddress getRemoteAddress() {
return address;
return address.get();
}
public void setRemoteAddress(InetSocketAddress address) {
this.address.set(address);
}
public Socket getSocket() {
@@ -110,7 +122,7 @@ public class TCPSocket {
synchronized (stateLock()) {
try {
checkAndSetState(SocketState.CREATED, SocketState.CONNECTING);
socket.connect(address);
socket.connect(getRemoteAddress());
socketInputStream = socket.getInputStream();
socketOutputStream = socket.getOutputStream();
} catch (IOException e) {
@@ -238,7 +250,7 @@ public class TCPSocket {
public void start() {
Assert.test(!running.get(), "Cannot start listener! Already started!");
Assert.isNull(thread, "Cannot start listener! Already started!");
thread = new Thread(this, "TCPServer Port#" + address.getPort());
thread = new Thread(this, "TCPServer Port#" + getRemoteAddress().getPort());
running.set(true);
thread.start();
}