|
| 1 | +/* |
| 2 | + * Copyright (c) 2023 Dario Lucia (https://www.dariolucia.eu) |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + */ |
| 17 | + |
| 18 | +package eu.dariolucia.reatmetric.driver.spacecraft.connectors; |
| 19 | + |
| 20 | +import eu.dariolucia.reatmetric.api.common.Pair; |
| 21 | +import eu.dariolucia.reatmetric.api.transport.AbstractTransportConnector; |
| 22 | +import eu.dariolucia.reatmetric.api.transport.exceptions.TransportException; |
| 23 | +import eu.dariolucia.reatmetric.api.value.StringUtil; |
| 24 | +import eu.dariolucia.reatmetric.core.api.IServiceCoreContext; |
| 25 | +import eu.dariolucia.reatmetric.driver.spacecraft.activity.ForwardDataUnitProcessingStatus; |
| 26 | +import eu.dariolucia.reatmetric.driver.spacecraft.activity.IForwardDataUnitStatusSubscriber; |
| 27 | +import eu.dariolucia.reatmetric.driver.spacecraft.activity.cltu.ICltuConnector; |
| 28 | +import eu.dariolucia.reatmetric.driver.spacecraft.definition.SpacecraftConfiguration; |
| 29 | + |
| 30 | +import java.io.IOException; |
| 31 | +import java.io.InputStream; |
| 32 | +import java.net.Socket; |
| 33 | +import java.rmi.RemoteException; |
| 34 | +import java.time.Instant; |
| 35 | +import java.util.Collections; |
| 36 | +import java.util.List; |
| 37 | +import java.util.concurrent.CopyOnWriteArrayList; |
| 38 | +import java.util.concurrent.ExecutorService; |
| 39 | +import java.util.concurrent.Executors; |
| 40 | +import java.util.logging.Level; |
| 41 | +import java.util.logging.Logger; |
| 42 | + |
| 43 | +/** |
| 44 | + * This connector established a TCP/IP connection to a host on a given port, and uses this connection to: |
| 45 | + * <ul> |
| 46 | + * <li>Receive CADUs</li> |
| 47 | + * <li>Send CLTUs</li> |
| 48 | + * </ul> |
| 49 | + */ |
| 50 | +public class CltuCaduTcpConnector extends AbstractTransportConnector implements ICltuConnector { |
| 51 | + private static final Logger LOG = Logger.getLogger(CltuCaduTcpConnector.class.getName()); |
| 52 | + |
| 53 | + private volatile Instant lastSamplingTime; |
| 54 | + private volatile long rxBytes; // injected bytes |
| 55 | + private volatile long txBytes; // injected bytes |
| 56 | + private String driverName; |
| 57 | + private SpacecraftConfiguration spacecraftConfig; |
| 58 | + private IServiceCoreContext context; |
| 59 | + private String host; |
| 60 | + private int port; |
| 61 | + private int caduLength; |
| 62 | + private int asmLength; |
| 63 | + |
| 64 | + private final List<IForwardDataUnitStatusSubscriber> subscribers = new CopyOnWriteArrayList<>(); |
| 65 | + private volatile Socket socket; |
| 66 | + |
| 67 | + private final ExecutorService cltuForwarderExecutor = Executors.newSingleThreadExecutor(r -> { |
| 68 | + Thread t = new Thread(r, "CLTU/CADU - CLTU forward thread"); |
| 69 | + t.setDaemon(true); |
| 70 | + return t; |
| 71 | + }); |
| 72 | + |
| 73 | + private Thread readingTmThread = null; |
| 74 | + |
| 75 | + public CltuCaduTcpConnector() { |
| 76 | + super("CLTU/CADU Connector", "CLTU/CADU Connector"); |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + protected Pair<Long, Long> computeBitrate() { |
| 81 | + Instant now = Instant.now(); |
| 82 | + if(lastSamplingTime != null) { |
| 83 | + long theRxBytes = rxBytes; // Not atomic, but ... who cares |
| 84 | + long theTxBytes = txBytes; // Not atomic, but ... who cares |
| 85 | + rxBytes = 0; |
| 86 | + txBytes = 0; |
| 87 | + long msDiff = now.toEpochMilli() - lastSamplingTime.toEpochMilli(); |
| 88 | + long rxRate = Math.round((theRxBytes * 8000.0) / (msDiff)); |
| 89 | + long txRate = Math.round((theTxBytes * 8000.0) / (msDiff)); |
| 90 | + lastSamplingTime = now; |
| 91 | + return Pair.of(txRate, rxRate); |
| 92 | + } else { |
| 93 | + lastSamplingTime = now; |
| 94 | + return null; |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + protected synchronized void doConnect() throws TransportException { |
| 100 | + if(socket == null) { |
| 101 | + try { |
| 102 | + // Connect, start thread to read CADUs and send RawData to broker for processing (with TM frames) |
| 103 | + socket = new Socket(this.host, this.port); |
| 104 | + final InputStream dataStream = socket.getInputStream(); |
| 105 | + readingTmThread = new Thread(() -> { |
| 106 | + readTm(dataStream); |
| 107 | + }, "CLTU/CADU - CADU reading thread"); |
| 108 | + readingTmThread.setDaemon(true); |
| 109 | + readingTmThread.start(); |
| 110 | + } catch (IOException e) { |
| 111 | + throw new TransportException(e); |
| 112 | + } |
| 113 | + } |
| 114 | + // Do nothing |
| 115 | + } |
| 116 | + |
| 117 | + private void readTm(InputStream inputStream) { |
| 118 | + while(this.socket != null) { |
| 119 | + try { |
| 120 | + byte[] cadu = inputStream.readNBytes(this.caduLength); |
| 121 | + // TODO process CADU |
| 122 | + // Remove ASM |
| 123 | + |
| 124 | + // if randomisation == ON, derandomise |
| 125 | + |
| 126 | + // get first frame-length bytes |
| 127 | + |
| 128 | + // build transfer frame info with configuration from spacecraft |
| 129 | + |
| 130 | + // distribute to raw data broker |
| 131 | + } catch (IOException e) { |
| 132 | + LOG.log(Level.SEVERE, "Reading of CADU failed: connection error on read", e); |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + @Override |
| 138 | + protected synchronized void doDisconnect() throws TransportException { |
| 139 | + if(socket != null) { |
| 140 | + try { |
| 141 | + socket.close(); |
| 142 | + } catch (IOException e) { |
| 143 | + // Ignore |
| 144 | + } |
| 145 | + socket = null; |
| 146 | + } |
| 147 | + if(readingTmThread != null) { |
| 148 | + readingTmThread = null; |
| 149 | + } |
| 150 | + // Done |
| 151 | + } |
| 152 | + |
| 153 | + @Override |
| 154 | + protected synchronized void doDispose() { |
| 155 | + try { |
| 156 | + disconnect(); |
| 157 | + } catch (TransportException e) { |
| 158 | + // Do nothing |
| 159 | + } |
| 160 | + cltuForwarderExecutor.shutdownNow(); |
| 161 | + } |
| 162 | + |
| 163 | + @Override |
| 164 | + public void abort() throws TransportException, RemoteException { |
| 165 | + disconnect(); |
| 166 | + } |
| 167 | + |
| 168 | + @Override |
| 169 | + public void configure(String driverName, SpacecraftConfiguration configuration, IServiceCoreContext context, String connectorInformation) throws RemoteException { |
| 170 | + this.driverName = driverName; |
| 171 | + this.spacecraftConfig = configuration; |
| 172 | + this.context = context; |
| 173 | + String[] infoSpl = connectorInformation.split(":", -1); // String with format: "<ip>:<port>:<CADU length>:<asm length>" |
| 174 | + this.host = infoSpl[0]; |
| 175 | + this.port = Integer.parseInt(infoSpl[1]); |
| 176 | + this.caduLength = Integer.parseInt(infoSpl[2]); |
| 177 | + this.asmLength = Integer.parseInt(infoSpl[3]); |
| 178 | + } |
| 179 | + |
| 180 | + @Override |
| 181 | + public void register(IForwardDataUnitStatusSubscriber subscriber) throws RemoteException { |
| 182 | + this.subscribers.add(subscriber); |
| 183 | + } |
| 184 | + |
| 185 | + @Override |
| 186 | + public void deregister(IForwardDataUnitStatusSubscriber subscriber) throws RemoteException { |
| 187 | + this.subscribers.remove(subscriber); |
| 188 | + } |
| 189 | + |
| 190 | + @Override |
| 191 | + public synchronized void sendCltu(byte[] cltu, long externalId) throws RemoteException { |
| 192 | + if(this.cltuForwarderExecutor.isShutdown()) { |
| 193 | + LOG.severe(String.format("Transmission of CLTU with external ID %d failed: CLTU/CADU connector disposed", externalId)); |
| 194 | + informSubscribers(externalId, ForwardDataUnitProcessingStatus.RELEASE_FAILED); |
| 195 | + return; |
| 196 | + } |
| 197 | + this.cltuForwarderExecutor.submit(() -> { |
| 198 | + Socket sock = this.socket; |
| 199 | + if (sock == null) { |
| 200 | + LOG.severe(String.format("Transmission of CLTU with external ID %d failed: CLTU/CADU connector is disconnected", externalId)); |
| 201 | + informSubscribers(externalId, ForwardDataUnitProcessingStatus.RELEASE_FAILED); |
| 202 | + return; |
| 203 | + } |
| 204 | + // Try to send CLTU |
| 205 | + try { |
| 206 | + LOG.log(Level.INFO, String.format("Sending CLTU with ID %d: %s", externalId, StringUtil.toHexDump(cltu))); |
| 207 | + sock.getOutputStream().write(cltu); |
| 208 | + } catch (IOException e) { |
| 209 | + LOG.log(Level.SEVERE, String.format("Transmission of CLTU with external ID %d failed: CLTU/CADU connector error on write", externalId), e); |
| 210 | + informSubscribers(externalId, ForwardDataUnitProcessingStatus.RELEASE_FAILED); |
| 211 | + return; |
| 212 | + } |
| 213 | + // Sent |
| 214 | + informSubscribers(externalId, ForwardDataUnitProcessingStatus.RELEASED); |
| 215 | + }); |
| 216 | + } |
| 217 | + |
| 218 | + @Override |
| 219 | + public List<String> getSupportedRoutes() throws RemoteException { |
| 220 | + return Collections.singletonList("CLTU/CADU @ " + this.host + ":" + this.port); |
| 221 | + } |
| 222 | + |
| 223 | + private void informSubscribers(long externalId, ForwardDataUnitProcessingStatus status) { |
| 224 | + informSubscribers(externalId, status, Instant.now()); |
| 225 | + } |
| 226 | + |
| 227 | + private void informSubscribers(long externalId, ForwardDataUnitProcessingStatus status, Instant time) { |
| 228 | + subscribers.forEach(o -> o.informStatusUpdate(externalId, status, time)); |
| 229 | + } |
| 230 | +} |
0 commit comments