Skip to content

Commit

Permalink
[-] Fixed error with PPP connection
Browse files Browse the repository at this point in the history
  • Loading branch information
VitalyKravtsov2016 committed Jul 28, 2022
1 parent ce2b8db commit a2f683f
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 83 deletions.
7 changes: 6 additions & 1 deletion Source/Core/src/com/shtrih/fiscalprinter/port/PPPThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
public class PPPThread implements Runnable {

private long ctx = 0;
private String status = "";
private Thread thread = null;
private final PPPConfig config;
private static CompositeLogger logger = CompositeLogger.getLogger(PPPThread.class);
Expand Down Expand Up @@ -71,7 +72,10 @@ public String getStatus()
if (isStarted() && (ctx != 0))
{
result = Api.api_status(ctx);
logger.debug("PPP status =" + result);
if (!status.equalsIgnoreCase(result)) {
status = result;
logger.debug("PPP status =" + result);
}
}
return result;
}
Expand All @@ -90,6 +94,7 @@ public void stop() {
}
thread.join();
thread = null;
status = "";
} catch (Exception e) {
logger.error(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public class PPPPort implements PrinterPort, PrinterPort.IPortEvents {
private int connectTimeout = 3000;
private int readTimeout = 3000;
private boolean opened = false;
private boolean portOpened = false;
private String localSocketName = null;
private long lastTimeInMillis = 0;
private static CompositeLogger logger = CompositeLogger.getLogger(PPPPort.class);
Expand All @@ -60,12 +61,16 @@ public boolean isOpened() {
return opened;
}

public synchronized void open(int timeout) throws Exception
{
if (isOpened()) {
return;
}
public synchronized void open(int timeout) throws Exception {
if (isOpened()) return;
logger.debug("open");
connect(timeout);
portOpened = true;
logger.debug("open: OK");
}

public synchronized void connect(int timeout) throws Exception {
if (isOpened()) return;
openTimeout = timeout;
localSocketName = UUID.randomUUID().toString();

Expand All @@ -76,30 +81,24 @@ public synchronized void open(int timeout) throws Exception
startPPPThread();
openLocalSocket(timeout);
startDispatchThread();
pppThread.waitForStatus("{\"phase\":\"PPP_PHASE_RUNNING\",\"status\":\"RUNNING\"}", 30000);
pppThread.waitForStatus("{\"phase\":\"PPP_PHASE_RUNNING\",\"status\":\"RUNNING\"}", 60000);
openSocket();
opened = true;
if (events != null) {
events.onConnect();
}

logger.debug("open: OK");
}

public void openSocket() throws Exception
{
if ((lastTimeInMillis != 0) && ((Calendar.getInstance().getTimeInMillis()-lastTimeInMillis) > 60000))
{
logger.debug("Reopen connection after 60 seconds of inacticity");
public void openSocket() throws Exception {
if ((lastTimeInMillis != 0) && ((Calendar.getInstance().getTimeInMillis() - lastTimeInMillis) > 60000)) {
logger.debug("Reopen connection after 60 seconds of inactivity");
closeSocket();
}

if (socket == null){
if (socket == null) {
socket = new Socket();
socket.setTcpNoDelay(true);
}
if (!socket.isConnected())
{
if (!socket.isConnected()) {
logger.debug("socket.connect");
socket.setSoTimeout(connectTimeout);
socket.connect(new InetSocketAddress("127.0.0.1", 7778));
Expand All @@ -109,8 +108,7 @@ public void openSocket() throws Exception
}
}

public void startPPPThread() throws Exception
{
public void startPPPThread() throws Exception {
if (pppThread != null) {
return;
}
Expand All @@ -131,21 +129,20 @@ public void startPPPThread() throws Exception
pppThread.waitForStatus("\"status\":\"RUNNING\"", 5000);
}

public void openLocalSocket(int timeout) throws Exception
{
public void openLocalSocket(int timeout) throws Exception {
if (localSocket != null) {
return;
}
logger.debug("openLocalSocket");
localSocket = new LocalSocket();
long time = Calendar.getInstance().getTimeInMillis() + timeout;
for (;;) {
for (; ; ) {
try {
localSocket.connect(new LocalSocketAddress(localSocketName));
break;
} catch (IOException e) {
logger.error(e.getMessage());
if (Calendar.getInstance().getTimeInMillis() > time){
if (Calendar.getInstance().getTimeInMillis() > time) {
throw e;
}
Thread.sleep(100);
Expand All @@ -155,24 +152,29 @@ public void openLocalSocket(int timeout) throws Exception
}

public synchronized void close() {
if (!isOpened()) {
return;
}
if (!isOpened()) return;
logger.debug("close");
disconnect();
printerPort.setPortEvents(null);
printerPort.close();
portOpened = false;
logger.debug("close: OK");
}

public synchronized void disconnect() {
if (!isOpened()) return;

opened = false;
stopDispatchThread();
stopPPPThread();
closeLocalSocket();
closeSocket();
printerPort.setPortEvents(null);
printerPort.close();
if (events != null) {
events.onDisconnect();
}
logger.debug("close: OK");
}

public void closeLocalSocket(){
public void closeLocalSocket() {
if (localSocket == null) return;

try {
Expand All @@ -183,16 +185,14 @@ public void closeLocalSocket(){
localSocket = null;
}

public void stopPPPThread()
{
if (pppThread == null) return;
public void stopPPPThread() {
if (pppThread == null) return;

pppThread.stop();
pppThread = null;
}

public void startDispatchThread()
{
public void startDispatchThread() {
if (dispatchThread != null) return;

logger.debug("startDispatchThread");
Expand All @@ -219,8 +219,7 @@ public void stopDispatchThread() {
logger.debug("stopDispatchThread: OK");
}

public void closeSocket()
{
public void closeSocket() {
logger.debug("closeSocket");

if (socket == null) {
Expand All @@ -236,12 +235,10 @@ public void closeSocket()
logger.debug("closeSocket: OK");
}

public void dispatchProc()
{
public void dispatchProc() {
logger.debug("dispatchProc.start");
try {
while (true)
{
while (true) {
Thread.sleep(0);
dispatchPackets();
}
Expand All @@ -252,8 +249,7 @@ public void dispatchProc()
logger.debug("dispatchProc.end");
}

public void dispatchPackets() throws Exception
{
public void dispatchPackets() throws Exception {
int count;
byte[] data;

Expand Down Expand Up @@ -299,21 +295,18 @@ public int byteToInt(int B) {
return B;
}

public int readByte() throws Exception
{
public int readByte() throws Exception {
return byteToInt(readBytes(1)[0]);
}

public byte[] readBytes(int len) throws Exception
{
public byte[] readBytes(int len) throws Exception {
open();

long time = Calendar.getInstance().getTimeInMillis() + readTimeout;

byte[] data = new byte[len];
int offset = 0;
while (len > 0)
{
while (len > 0) {
openSocket();

int count = Math.min(len, socket.getInputStream().available());
Expand All @@ -327,26 +320,24 @@ public byte[] readBytes(int len) throws Exception
offset += count;
}
lastTimeInMillis = Calendar.getInstance().getTimeInMillis();
if (lastTimeInMillis > time){
if (lastTimeInMillis > time) {
noConnectionError();
}
Thread.sleep(0);
}
return data;
}

public void write(byte[] b) throws Exception
{
public void write(byte[] b) throws Exception {
open();
for (int i = 0; i < 2; i++) {
try {
openSocket();
socket.getOutputStream().write(b);
socket.getOutputStream().flush();
return;
} catch (SocketException e)
{
logger.error("write: " + e.getMessage(), e);
} catch (SocketException e) {
logger.error("write: " + e.getMessage(), e);
closeSocket();
if (i == 1) {
throw e;
Expand All @@ -368,7 +359,7 @@ public void setBaudRate(int baudRate) throws Exception {

public void setTimeout(int timeout) throws Exception {
readTimeout = timeout;
if (isOpened()){
if (isOpened()) {
socket.setSoTimeout(readTimeout);
}
logger.debug("setTimeout(" + readTimeout + ")");
Expand All @@ -390,46 +381,35 @@ public boolean isSearchByBaudRateEnabled() {
return false;
}

public String[] getPortNames() throws Exception
{
public String[] getPortNames() throws Exception {
return printerPort.getPortNames();
}

public void setPortEvents(IPortEvents events)
{
public void setPortEvents(IPortEvents events) {
this.events = events;
}

public String readParameter(int parameterID){
switch (parameterID){
case PrinterPort.PARAMID_IS_RELIABLE: return "1";
default: return null;
public String readParameter(int parameterID) {
switch (parameterID) {
case PrinterPort.PARAMID_IS_RELIABLE:
return "1";
default:
return null;
}
}

public void onConnect()
{
if (isOpened())
{
public void onConnect() {
if (portOpened) {
try {
startDispatchThread();
pppThread.waitForStatus("{\"phase\":\"PPP_PHASE_RUNNING\",\"status\":\"RUNNING\"}", 30000);
}
catch(Exception e){
connect(openTimeout);
} catch (Exception e) {
logger.error("Failed to reconnect, " + e.getMessage());
}
if (events != null) {
events.onConnect();
}
}
}

public void onDisconnect(){
if (isOpened()) {
stopDispatchThread();
if (events != null) {
events.onDisconnect();
}
}
public void onDisconnect() {
disconnect();
}

}

0 comments on commit a2f683f

Please sign in to comment.