DELL
2024-02-21 4982b9614516dde101c3e44c60a612b3bfd8d6fe
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
 * Copyright 2011-17 Fraunhofer ISE, energy & meteo Systems GmbH and other contributors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package org.openmuc.jositransport;
 
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
 
import javax.net.SocketFactory;
 
/**
 * This class implements a client Transport Service Access Point (TSAP) over TCP/IP as defined in RFC 1006, ISO 8072,
 * and ISO 8073. It can be used to create TConnections that connect to remote ServerTSAPs.
 * 
 */
public final class ClientTSap {
 
    private int maxTPDUSizeParam = 16;
    private SocketFactory socketFactory = null;
    private int messageTimeout = 0;
    private int messageFragmentTimeout = 60000;
 
    public byte[] tSelRemote = null;
    public byte[] tSelLocal = null;
 
    /**
     * Use this constructor to create a client TSAP that will start connections to remote TSAPs.
     */
    public ClientTSap() {
        socketFactory = SocketFactory.getDefault();
    }
 
    /**
     * Use this constructor to create a client TSAP that will start connections to remote TSAPs. You could pass an
     * SSLSocketFactory to enable SSL.
     * 
     * @param socketFactory
     *            the socket factory to create the underlying socket
     */
    public ClientTSap(SocketFactory socketFactory) {
        this.socketFactory = socketFactory;
    }
 
    /**
     * Set the TConnection timeout for waiting for the first byte of a new message. Default is 0 (unlimited)
     * 
     * @param messageTimeout
     *            in milliseconds
     */
    public void setMessageTimeout(int messageTimeout) {
        this.messageTimeout = messageTimeout;
    }
 
    /**
     * Set the TConnection timeout for receiving data once the beginning of a message has been received. Default is
     * 60000 (60seconds)
     * 
     * @param messageFragmentTimeout
     *            in milliseconds
     */
    public void setMessageFragmentTimeout(int messageFragmentTimeout) {
        this.messageFragmentTimeout = messageFragmentTimeout;
    }
 
    /**
     * Set the maxTPDUSize. The default maxTPduSize is 65531 (see RFC 1006).
     * 
     * @param maxTPduSizeParam
     *            The maximum length is equal to 2^(maxTPduSizeParam) octets. Note that the actual TSDU size that can be
     *            transfered is equal to TPduSize-3. Default is 65531 octets (see RFC 1006), 7 <= maxTPduSizeParam
     *            <= 16, needs to be set before listening or connecting
     */
    public void setMaxTPDUSizeParam(int maxTPduSizeParam) {
        if (maxTPduSizeParam < 7 || maxTPduSizeParam > 16) {
            throw new IllegalArgumentException("maxTPDUSizeParam is out of bound");
        }
        this.maxTPDUSizeParam = maxTPduSizeParam;
    }
 
    /**
     * Get the maximum TPDU size parameter to be used by this TSAP
     * 
     * @return the maximum TPDU size parameter
     */
    public int getMaxTPDUSizeParam() {
        return maxTPDUSizeParam;
    }
 
    /**
     * Calculates and returns the maximum TPDUSize. This is equal to 2^(maxTPDUSizeParam)
     * 
     * @param maxTPDUSizeParam
     *            the size parameter
     * @return the maximum TPDU size
     */
    public static int getMaxTPDUSize(int maxTPDUSizeParam) {
        if (maxTPDUSizeParam < 7 || maxTPDUSizeParam > 16) {
            throw new IllegalArgumentException("maxTPDUSizeParam is out of bound");
        }
        if (maxTPDUSizeParam == 16) {
            return 65531;
        }
        else {
            return (int) Math.pow(2, maxTPDUSizeParam);
        }
    }
 
    /**
     * Connect to a remote TSAP that is listening at the destination address.
     * 
     * @param address
     *            remote IP
     * @param port
     *            remote port
     * @return the Connection Object
     * @throws IOException
     *             is thrown if connection was unsuccessful.
     */
    public TConnection connectTo(InetAddress address, int port) throws IOException {
        return connectTo(address, port, null, -1);
    }
 
    /**
     * Connect to a remote TSAP that is listening at the destination address.
     * 
     * @param address
     *            remote IP
     * @param port
     *            remote port
     * @param localAddr
     *            local IP
     * @param localPort
     *            local port
     * @return the Connection Object
     * @throws IOException
     *             is thrown if connection was unsuccessful.
     */
    public TConnection connectTo(InetAddress address, int port, InetAddress localAddr, int localPort)
            throws IOException {
        Socket socket;
        if (localAddr == null) {
            socket = socketFactory.createSocket(address, port);
        }
        else {
            socket = socketFactory.createSocket(address, port, localAddr, localPort);
        }
        TConnection tConnection = new TConnection(socket, maxTPDUSizeParam, messageTimeout, messageFragmentTimeout,
                null);
        tConnection.tSelRemote = tSelRemote;
        tConnection.tSelLocal = tSelLocal;
        tConnection.startConnection();
 
        return tConnection;
    }
 
    public void setSocketFactory(SocketFactory socketFactory) {
        this.socketFactory = socketFactory;
    }
 
}