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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package org.openmuc.openiec61850.integrationtests;
 
import java.io.IOException;
import java.net.InetAddress;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
 
import javax.naming.ConfigurationException;
 
import org.junit.Assert;
import org.junit.Test;
import org.openmuc.openiec61850.BasicDataAttribute;
import org.openmuc.openiec61850.BdaReasonForInclusion;
import org.openmuc.openiec61850.ClientAssociation;
import org.openmuc.openiec61850.ClientEventListener;
import org.openmuc.openiec61850.ClientSap;
import org.openmuc.openiec61850.Fc;
import org.openmuc.openiec61850.FcModelNode;
import org.openmuc.openiec61850.ModelNode;
import org.openmuc.openiec61850.Report;
import org.openmuc.openiec61850.SclParseException;
import org.openmuc.openiec61850.ServerEventListener;
import org.openmuc.openiec61850.ServerModel;
import org.openmuc.openiec61850.ServerSap;
import org.openmuc.openiec61850.ServiceError;
 
public class ClientServerITest2 extends Thread implements ServerEventListener, ClientEventListener {
 
    int port = 54322;
    String host = "127.0.0.1";
    ClientSap clientSap = new ClientSap();
    ServerSap serverSap = null;
    ClientAssociation clientAssociation = null;
    ClientAssociation clientAssociation2 = null;
    ServerModel serversServerModel = null;
    private static int numReports = 0;
    private static int numSuccess = 0;
    private static int numAssociationClosed = 0;
 
    // Get the Java runtime
    public static Runtime runtime = Runtime.getRuntime();
 
    @Test
    public void testClientServerCom() throws IOException, ServiceError, ConfigurationException,
            javax.naming.ConfigurationException, SclParseException, InterruptedException {
 
        clientSap.setTSelRemote(new byte[] { 0, 1 });
        clientSap.setTSelLocal(new byte[] { 0, 0 });
        clientSap.setApTitleCalled(new int[] { 1, 1, 999, 1 });
 
        runServer("src/test/resources/testModel2.icd", port);
        System.out.println("IED Server is running");
 
        // -----------------------------------------------------------
        // Client
        // -----------------------------------------------------------
 
        System.out.println("Attempting to connect to server " + host + " on port " + port);
 
        clientAssociation = clientSap.associate(InetAddress.getByName(host), port, null, this);
 
        ServerModel serverModel = clientAssociation.getModelFromSclFile("src/test/resources/testModel2.icd");
        // ServerModel serverModel = clientAssociation.retrieveModel();
 
        getAllBdas(serverModel);
 
        // timestamp = (BdaTimestamp) serverModel.findModelNode("ied1lDevice1/MMXU1.TotW.t", Fc.MX);
        // clientAssociation.getDataValues(timestamp);
 
        clientAssociation.disconnect();
 
        Thread.sleep(500);
        serverSap.stop();
 
    }
 
    private void getAllBdas(ServerModel serverModel) throws ServiceError, IOException {
 
        for (ModelNode ld : serverModel) {
            for (ModelNode ln : ld) {
                getDataRecursive(ln, clientAssociation);
            }
        }
    }
 
    private void runServer(String sclFilePath, int port) throws SclParseException, IOException {
 
        List<ServerSap> serverSaps = null;
        serverSaps = ServerSap.getSapsFromSclFile(sclFilePath);
 
        serverSap = serverSaps.get(0);
        serverSap.setPort(port);
        serverSap.startListening(this);
        serversServerModel = serverSap.getModelCopy();
        start();
    }
 
    private static void getDataRecursive(ModelNode modelNode, ClientAssociation clientAssociation)
            throws ServiceError, IOException {
        if (modelNode.getChildren() == null) {
            return;
        }
        for (ModelNode childNode : modelNode) {
            FcModelNode fcChildNode = (FcModelNode) childNode;
            if (fcChildNode.getFc() != Fc.CO) {
                System.out.println("calling GetDataValues(" + childNode.getReference() + ")");
                clientAssociation.getDataValues(fcChildNode);
            }
            // clientAssociation.setDataValues(fcChildNode);
            getDataRecursive(childNode, clientAssociation);
        }
    }
 
    public static int findArray(Byte[] array, Byte[] subArray) {
        return Collections.indexOfSubList(Arrays.asList(array), Arrays.asList(subArray));
    }
 
    public static String getByteArrayString(byte[] byteArray) {
        StringBuilder builder = new StringBuilder();
        int l = 1;
        for (byte b : byteArray) {
            if ((l != 1) && ((l - 1) % 8 == 0)) {
                builder.append(' ');
            }
            if ((l != 1) && ((l - 1) % 16 == 0)) {
                builder.append('\n');
            }
            l++;
            builder.append("0x");
            String hexString = Integer.toHexString(b & 0xff);
            if (hexString.length() == 1) {
                builder.append(0);
            }
            builder.append(hexString + " ");
        }
        return builder.toString();
    }
 
    @Override
    public void serverStoppedListening(ServerSap serverSAP) {
        // TODO Auto-generated method stub
    }
 
    @Override
    public List<ServiceError> write(List<BasicDataAttribute> bdas) {
        System.out.println("DataSource: got write request");
        return null;
    }
 
    @Override
    public void run() {
 
        // BdaFloat32 totWMag = (BdaFloat32) serversServerModel.findModelNode("ied1lDevice1/MMXU1.TotW.mag.f", Fc.MX);
        // BdaQuality q = (BdaQuality) serversServerModel.findModelNode("ied1lDevice1/MMXU1.TotW.q", Fc.MX);
        // BdaTimestamp t = (BdaTimestamp) serversServerModel.findModelNode("ied1lDevice1/MMXU1.TotW.t", Fc.MX);
        //
        // List<BasicDataAttribute> totWBdas = new ArrayList<BasicDataAttribute>(3);
        // totWBdas.add(totWMag);
        // totWBdas.add(q);
        // totWBdas.add(t);
        //
        // float totWMagVal = 0.0f;
        // q.setValidity(BdaQuality.Validity.GOOD);
        //
        // // for (int i = 0; i < 500000; i++) {
        //
        // totWMagVal += 1.0;
        //
        // logger.info("setting totWmag to: " + totWMagVal);
        // totWMag.setFloat(totWMagVal);
        // t.setCurrentTime();
        //
        // if (q.getValidity() == Validity.GOOD) {
        // q.setValidity(BdaQuality.Validity.INVALID);
        // }
        // else {
        // q.setValidity(BdaQuality.Validity.GOOD);
        // }
        //
        // try {
        // Thread.sleep(4000);
        // } catch (InterruptedException e) {
        // }
        // serverSap.setValues(totWBdas);
 
        // // Run the garbage collector
        // runtime.gc();
        // // Calculate the used memory
        // long memory = runtime.totalMemory() - runtime.freeMemory();
        // System.out.println("Used memory is bytes: " + memory);
        // System.out.println("Used memory is megabytes: " + bytesToMegabytes(memory));
 
        // try {
        // Thread.sleep(2000);
        // } catch (InterruptedException e) {
        // }
        // }
 
    }
 
    private static final long MEGABYTE = 1024L * 1024L;
 
    public static long bytesToMegabytes(long bytes) {
        return bytes / MEGABYTE;
    }
 
    @Override
    public void newReport(Report report) {
        System.out.println("got report!");
        numReports++;
 
        if (numReports == 1) {
            List<BdaReasonForInclusion> reasons = report.getReasonCodes();
            Assert.assertEquals(2, reasons.size());
            Assert.assertTrue(reasons.get(0).isGeneralInterrogation());
            Assert.assertFalse(reasons.get(0).isDataUpdate());
        }
        else if (numReports == 2) {
            List<BdaReasonForInclusion> reasons = report.getReasonCodes();
            Assert.assertEquals(1, reasons.size());
            Assert.assertFalse(reasons.get(0).isGeneralInterrogation());
            Assert.assertTrue(reasons.get(0).isDataChange());
        }
        else if (numReports >= 3) {
            List<BdaReasonForInclusion> reasons = report.getReasonCodes();
            Assert.assertEquals(2, reasons.size());
            Assert.assertTrue(reasons.get(0).isIntegrity());
            Assert.assertTrue(reasons.get(1).isIntegrity());
        }
 
        numSuccess++;
 
    }
 
    @Override
    public void associationClosed(IOException e) {
        System.out.println("Association closed!");
        numAssociationClosed++;
    }
}