Skip to content

Commit

Permalink
Merge pull request #27 from kenta-shimizu/v5
Browse files Browse the repository at this point in the history
Add SecsMessageBuilder
  • Loading branch information
kenta-shimizu authored Jan 27, 2024
2 parents e3c4e5d + 5516c93 commit c81323f
Show file tree
Hide file tree
Showing 12 changed files with 776 additions and 105 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,27 @@
import com.shimizukenta.secs.UnsetSocketAddressException;
import com.shimizukenta.secs.hsms.HsmsCommunicateState;
import com.shimizukenta.secs.hsms.HsmsConnectionMode;
import com.shimizukenta.secs.hsms.HsmsConnectionModeIllegalStateException;
import com.shimizukenta.secs.hsmsgs.HsmsGsCommunicatorConfig;

public abstract class AbstractHsmsGsActiveCommunicator extends AbstractHsmsGsCommunicator {

public AbstractHsmsGsActiveCommunicator(HsmsGsCommunicatorConfig config) {
super(config);

config.connectionMode().addChangeListener(mode -> {
if (mode != HsmsConnectionMode.ACTIVE) {
try {
this.close();
}
catch (IOException giveup) {
}
}
});
}

@Override
public void open() throws IOException {

if ( this.config().connectionMode().get() != HsmsConnectionMode.ACTIVE ) {
throw new HsmsConnectionModeIllegalStateException("NOT ACTIVE");
}

super.open();

this.executorService().execute(() -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,28 @@
import com.shimizukenta.secs.UnsetSocketAddressException;
import com.shimizukenta.secs.hsms.HsmsCommunicateState;
import com.shimizukenta.secs.hsms.HsmsConnectionMode;
import com.shimizukenta.secs.hsms.HsmsConnectionModeIllegalStateException;
import com.shimizukenta.secs.hsmsgs.HsmsGsCommunicatorConfig;
import com.shimizukenta.secs.local.property.TimeoutProperty;

public abstract class AbstractHsmsGsPassiveCommunicator extends AbstractHsmsGsCommunicator {

public AbstractHsmsGsPassiveCommunicator(HsmsGsCommunicatorConfig config) {
super(config);

config.connectionMode().addChangeListener(mode -> {
if (mode != HsmsConnectionMode.PASSIVE) {
try {
this.close();
}
catch (IOException giveup) {
}
}
});
}

@Override
public void open() throws IOException {

if ( this.config().connectionMode().get() != HsmsConnectionMode.PASSIVE ) {
throw new HsmsConnectionModeIllegalStateException("NOT PASSIVE");
}

super.open();

this.executorService().execute(() -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import com.shimizukenta.secs.UnsetSocketAddressException;
import com.shimizukenta.secs.hsms.HsmsCommunicateState;
import com.shimizukenta.secs.hsms.HsmsConnectionMode;
import com.shimizukenta.secs.hsms.HsmsConnectionModeIllegalStateException;
import com.shimizukenta.secs.hsms.HsmsException;
import com.shimizukenta.secs.hsms.HsmsMessage;
import com.shimizukenta.secs.hsms.HsmsMessageRejectReason;
Expand All @@ -30,15 +29,21 @@ public abstract class AbstractHsmsSsActiveCommunicator extends AbstractHsmsSsCom

public AbstractHsmsSsActiveCommunicator(HsmsSsCommunicatorConfig config) {
super(Objects.requireNonNull(config));

config.connectionMode().addChangeListener(mode -> {
if (mode != HsmsConnectionMode.ACTIVE) {
try {
this.close();
}
catch (IOException giveup) {
}
}
});
}

@Override
public void open() throws IOException {

if ( this.config().connectionMode().get() != HsmsConnectionMode.ACTIVE ) {
throw new HsmsConnectionModeIllegalStateException("NOT ACTIVE");
}

super.open();

this.executorService().execute(() -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import com.shimizukenta.secs.UnsetSocketAddressException;
import com.shimizukenta.secs.hsms.HsmsCommunicateState;
import com.shimizukenta.secs.hsms.HsmsConnectionMode;
import com.shimizukenta.secs.hsms.HsmsConnectionModeIllegalStateException;
import com.shimizukenta.secs.hsms.HsmsException;
import com.shimizukenta.secs.hsms.HsmsMessage;
import com.shimizukenta.secs.hsms.HsmsMessageRejectReason;
Expand Down Expand Up @@ -45,15 +44,21 @@ public abstract class AbstractHsmsSsPassiveCommunicator extends AbstractHsmsSsCo

public AbstractHsmsSsPassiveCommunicator(HsmsSsCommunicatorConfig config) {
super(Objects.requireNonNull(config));

config.connectionMode().addChangeListener(mode -> {
if (mode != HsmsConnectionMode.PASSIVE) {
try {
this.close();
}
catch (IOException giveup) {
}
}
});
}

@Override
public void open() throws IOException {

if ( this.config().connectionMode().get() != HsmsConnectionMode.PASSIVE ) {
throw new HsmsConnectionModeIllegalStateException("NOT PASSIVE");
}

super.open();

this.executorService().execute(() -> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.shimizukenta.secs.impl;

import java.util.concurrent.atomic.AtomicInteger;

import com.shimizukenta.secs.SecsCommunicator;
import com.shimizukenta.secs.SecsMessage;
import com.shimizukenta.secs.secs2.Secs2;

public abstract class AbstractSecsMessageBuilder<M extends SecsMessage, C extends SecsCommunicator> implements SecsMessageBuilder<M, C> {

private final AtomicInteger autoNum;

public AbstractSecsMessageBuilder() {
this.autoNum = new AtomicInteger(0);
}

// abstract protected boolean isEquip();
//
// abstract protected byte[] device2Bytes();

abstract protected boolean isEquip(C communicator);

abstract protected byte[] device2Bytes(C communicator);

protected byte[] system4Bytes(C communicator) {

final int num = autoNum.incrementAndGet();

if (isEquip(communicator)) {

byte[] dev2bs = this.device2Bytes(communicator);

return new byte[] {
dev2bs[0],
dev2bs[1],
(byte)(num >> 8),
(byte)num
};

} else {

return new byte[] {
(byte)0x00,
(byte)0x00,
(byte)(num >> 8),
(byte)num
};
}
}

@Override
public M build(C communicator, int strm, int func, boolean wbit) {
return this.build(communicator, strm, func, wbit, Secs2.empty());
}

@Override
public M build(C communicator, SecsMessage primaryMsg, int strm, int func, boolean wbit) {
return this.build(communicator, primaryMsg, strm, func, wbit, Secs2.empty());
}

}
24 changes: 24 additions & 0 deletions src/main/java/com/shimizukenta/secs/impl/SecsMessageBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.shimizukenta.secs.impl;

import com.shimizukenta.secs.SecsCommunicator;
import com.shimizukenta.secs.SecsMessage;
import com.shimizukenta.secs.secs2.Secs2;

/**
* SsecsMeessageBuilder.
*
* @author kenta-shimizu
*
* @param <M> the Secs-Message extends
* @param <C> the SecsCommunicator extends
*/
public interface SecsMessageBuilder<M extends SecsMessage, C extends SecsCommunicator> {

public M build(C communicator, int strm, int func, boolean wbit);

public M build(C communicator, int strm, int func, boolean wbit, Secs2 body);

public M build(C communicator, SecsMessage primaryMsg, int strm, int func, boolean wbit);

public M build(C communicator, SecsMessage primaryMsg, int strm, int func, boolean wbit, Secs2 body);
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public abstract class AbstractSecs1Communicator extends AbstractSecsCommunicator
public AbstractSecs1Communicator(AbstractSecs1CommunicatorConfig config) {
super(config);
this.config = config;
this.msgBuilder = new AbstractSecs1MessageBuilder(this) {};
this.msgBuilder = new AbstractSecs1MessageBuilder() {};
this.circuit = new AbstractSecs1Circuit(this) {};

this.secs1MsgRecvQueueBiObserver = new Secs1MessageReceiveQueueBiObserver(this);
Expand Down Expand Up @@ -91,7 +91,7 @@ public Optional<SecsMessage> templateSend(int strm, int func, boolean wbit, Secs

try {

return this.circuit.send(this.messageBuilder().build(strm, func, wbit, secs2))
return this.circuit.send(this.messageBuilder().build(this, strm, func, wbit, secs2))
.map(m -> (SecsMessage)m);
}
catch (Secs1TooBigMessageBodyException e) {
Expand All @@ -104,7 +104,7 @@ public Optional<SecsMessage> templateSend(SecsMessage primaryMsg, int strm, int
throws SecsSendMessageException, SecsWaitReplyMessageException, SecsException, InterruptedException {

try {
return this.circuit.send(this.messageBuilder().build(primaryMsg, strm, func, wbit, secs2))
return this.circuit.send(this.messageBuilder().build(this, primaryMsg, strm, func, wbit, secs2))
.map(m -> (SecsMessage)m);
}
catch (Secs1TooBigMessageBodyException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,85 +4,46 @@
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;

import com.shimizukenta.secs.SecsMessage;
import com.shimizukenta.secs.impl.AbstractSecsMessageBuilder;
import com.shimizukenta.secs.secs1.Secs1Communicator;
import com.shimizukenta.secs.secs1.Secs1MessageBlock;
import com.shimizukenta.secs.secs1.Secs1TooBigMessageBodyException;
import com.shimizukenta.secs.secs2.Secs2;
import com.shimizukenta.secs.secs2.Secs2BytesParseException;
import com.shimizukenta.secs.secs2.impl.Secs2BytesParsers;

public abstract class AbstractSecs1MessageBuilder implements Secs1MessageBuilder {
public abstract class AbstractSecs1MessageBuilder extends AbstractSecsMessageBuilder<AbstractSecs1Message, Secs1Communicator> implements Secs1MessageBuilder {

private final AbstractSecs1Communicator comm;

public AbstractSecs1MessageBuilder(AbstractSecs1Communicator communicator) {
this.comm = communicator;
public AbstractSecs1MessageBuilder() {
/* Nothing */
}

private final AtomicInteger autoNumber = new AtomicInteger(0);

protected byte[] getAutoNumber2Bytes() {
int n = autoNumber.incrementAndGet();
return new byte[] {
(byte)(n >> 8),
(byte)n
};
@Override
protected boolean isEquip(Secs1Communicator communicator) {
return communicator.isEquip();
}

protected byte[] getDeviceId2Bytes() {
int n = this.comm.deviceId();
@Override
protected byte[] device2Bytes(Secs1Communicator communicator) {
int n = communicator.deviceId();
return new byte[] {
(byte)((n >> 8) & 0x7F),
(byte)n
};
}

protected byte[] getSystem4Bytes() {

byte[] aa = this.getAutoNumber2Bytes();

if ( this.comm.isEquip() ) {

byte[] ss = this.getDeviceId2Bytes();

return new byte[] {
ss[0],
ss[1],
aa[0],
aa[1]
};

} else {

return new byte[] {
(byte)0x0,
(byte)0x0,
aa[0],
aa[1]
};
}
}

@Override
public AbstractSecs1Message build(
int strm,
int func,
boolean wbit) {

return this.build(strm, func, wbit, Secs2.empty());
}

@Override
public AbstractSecs1Message build(
Secs1Communicator communicator,
int strm,
int func,
boolean wbit,
Secs2 body) {

byte[] dd = this.getDeviceId2Bytes();
byte[] ssss = this.getSystem4Bytes();
byte[] dd = this.device2Bytes(communicator);
byte[] ssss = this.system4Bytes(communicator);

byte[] header = new byte[] {
dd[0],
Expand All @@ -97,7 +58,7 @@ public AbstractSecs1Message build(
ssss[3]
};

if ( this.comm.isEquip() ) {
if ( this.isEquip(communicator) ) {
header[0] |= (byte)0x80;
}

Expand All @@ -107,26 +68,17 @@ public AbstractSecs1Message build(

return build(header, body);
}

@Override
public AbstractSecs1Message build(
SecsMessage primaryMsg,
int strm,
int func,
boolean wbit) {

return this.build(primaryMsg, strm, func, wbit, Secs2.empty());
}


@Override
public AbstractSecs1Message build(
Secs1Communicator communicator,
SecsMessage primaryMsg,
int strm,
int func,
boolean wbit,
Secs2 body) {

byte[] dd = this.getDeviceId2Bytes();
byte[] dd = this.device2Bytes(communicator);
byte[] ppbb = primaryMsg.header10Bytes();

byte[] header = new byte[] {
Expand All @@ -142,7 +94,7 @@ public AbstractSecs1Message build(
ppbb[9]
};

if ( this.comm.isEquip() ) {
if ( this.isEquip(communicator) ) {
header[0] |= (byte)0x80;
}

Expand Down
Loading

0 comments on commit c81323f

Please sign in to comment.