/
PingSenderSync.java
143 lines (113 loc) · 5 KB
/
PingSenderSync.java
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
//
// Copyright (c) AIT Austrian Institute of Technology GmbH.
// Distributed under the terms of the Modified BSD License.
//
package at.ac.ait.lablink.examples.ping.sync;
import at.ac.ait.lablink.core.client.ci.mqtt.impl.MqttCommInterfaceUtility;
import at.ac.ait.lablink.core.client.ex.ClientNotReadyException;
import at.ac.ait.lablink.core.client.ex.CommInterfaceNotSupportedException;
import at.ac.ait.lablink.core.client.ex.DataTypeNotSupportedException;
import at.ac.ait.lablink.core.client.ex.NoServicesInClientLogicException;
import at.ac.ait.lablink.core.client.ex.NoSuchCommInterfaceException;
import at.ac.ait.lablink.core.client.ex.ServiceTypeDoesNotMatchClientType;
import at.ac.ait.lablink.core.client.impl.LlClient;
import at.ac.ait.lablink.core.service.IImplementedService;
import at.ac.ait.lablink.core.service.IServiceStateChangeNotifier;
import at.ac.ait.lablink.core.service.LlService;
import at.ac.ait.lablink.examples.ping.PingDataService;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
/**
* Class PingSenderSync.
*/
public class PingSenderSync {
/** Client name. */
private static final String CLIENT_NAME = "PingSenderSync";
/** Service name. */
private static final String SERVICE_NAME = "PingSenderService";
/** Group name. */
private static final String GROUP_NAME = "PingDemo";
/** Scenario name. */
private static final String SCENARIO_NAME = "PingSync";
/** General Lablink properties configuration. */
private static final String llprop = "$LLCONFIG$ait.example.all.llproperties";
/** Sync properties configuration. */
private static final String llsync = "$LLCONFIG$ait.example.ping.sync.sync-host.properties";
/** Logger. */
private static final Logger logger = LogManager.getLogger( CLIENT_NAME );
/** Ping counter. */
private int intPing = 0;
/** Client. */
private LlClient client;
/**
* The main method.
*
* @param args arguments to main method
* @throws ClientNotReadyException client not ready
* @throws CommInterfaceNotSupportedException comm interface not supported
* @throws DataTypeNotSupportedException data type not supported
* @throws NoServicesInClientLogicException no services in client logic
* @throws NoSuchCommInterfaceException no such comm interface
* @throws ServiceTypeDoesNotMatchClientType service type does not match client type
* @throws ConfigurationException bad configuration
*/
public static void main( String[] args )
throws ClientNotReadyException, CommInterfaceNotSupportedException,
DataTypeNotSupportedException, NoServicesInClientLogicException,
NoSuchCommInterfaceException, ServiceTypeDoesNotMatchClientType,
ConfigurationException {
PingSenderSync app = new PingSenderSync();
app.setupClient();
}
/**
* Execute the client setup.
*
* @throws ClientNotReadyException client not ready
* @throws CommInterfaceNotSupportedException comm interface not supported
* @throws DataTypeNotSupportedException data type not supported
* @throws NoServicesInClientLogicException no services in client logic
* @throws NoSuchCommInterfaceException no such comm interface
* @throws ServiceTypeDoesNotMatchClientType service type does not match client type
* @throws ConfigurationException bad configuration
*/
private void setupClient()
throws ClientNotReadyException, CommInterfaceNotSupportedException,
DataTypeNotSupportedException, NoServicesInClientLogicException,
NoSuchCommInterfaceException, ServiceTypeDoesNotMatchClientType,
ConfigurationException {
client = new LlClient( CLIENT_NAME, MqttCommInterfaceUtility.SP_ACCESS_NAME, true, false );
PingSyncConsumer sync = new PingSyncConsumer( this );
MqttCommInterfaceUtility.addClientProperties( client,
"A simple sender.", SCENARIO_NAME, GROUP_NAME, CLIENT_NAME,
this.llprop, this.llsync, sync );
PingDataService pingSignal = new PingDataService( SERVICE_NAME, false );
pingSignal.addStateChangeNotifier( new IServiceStateChangeNotifier<LlService, String>() {
@Override
public void stateChanged( LlService service, String oldVal, String newVal ) {
logger.info( "{}: Value changed from '{}' to '{}'", service.getName(), oldVal, newVal );
}
} );
MqttCommInterfaceUtility.addDataPointProperties( pingSignal, SERVICE_NAME,
"ping signal", SERVICE_NAME, "none" );
// Add service to the client.
client.addService( pingSignal );
// Create the client.
client.create();
// Initialize the client.
client.init();
// Start the client.
client.start();
}
/**
* Send ping message.
*/
protected void sendPing() {
@SuppressWarnings( "unchecked" )
IImplementedService<String> service =
( IImplementedService<String> ) client.getImplementedServices().get( SERVICE_NAME );
service.setValue( "PING#" + Integer.toString( intPing ) );
// Increment ping counter.
++intPing;
}
}