import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.NoSuchElementException;
import java.util.StringTokenizer;
/**
* Example datafeed receiver.
*
* This is a functional example program that accepts real-time porting
* changes from Paradigm.One. Porting changes are transmitted via a simple
* text-based protocol over TCP/IP. Although any port can be used, port
* 5000 is the default.
* <p/>
* Porting changes are transmitted from Paradigm.One's datafeed server
* (referred to as the DatafeedSender) to the datafeed receiver (referred
* to as the DatafeedReceiver).
* <p/>
* The DatafeedSender is responsible for maintaining the connection to the
* DatafeedReceiver.
* <p/>
* Once the DatafeedSender has connected to the DatafeedReceiver, it will
* start sending porting changes one at a time. Each porting change is line
* of text terminated by a linefeed character. Once the DatafeedReceiver
* has processed the porting change, it should send back an
* acknowledgement. An acknowledgement is a line of text consisting of "OK"
* (without the double quotes) followed by a linefeed character. On
* receiving the acknowledgement the DatafeedSender sends the next porting
* change. If no acknowledgement is received for whatever reason the
* DatafeedSender will retry until an acknowledgement is received (in other
* words, the delivery of porting changes is guaranteed).
* <p/>
* Each porting change consists of 3 fields separated by a pipe (|) symbol,
* for example 0408975099|1411|20170518134525. The fields are:
* <ul>
* <li>Mobile Number: the number being ported</li>
* <li>CAC: the carrier access code</li>
* <li>Timestamp: the date and time the porting event was generated. The timestamp format is YYYYMMDDHHMISS (24-hour notation), e.g. "20170518134525". The timestamp is in AEST.</li>
* </ul>
*
* (c) 2017 Paradigm One Pty Ltd.
*/
public class DatafeedReceiver {
// Default port
private int port = 5000;
// The server socket
private ServerSocket server = null;
/**
* Receives porting changes.
*/
public void receive() throws IOException {
try {
// Create the server socket
server = new ServerSocket(port);
// Main loop
System.out.println("DatafeedReceiver started on port " + port + "...");
Socket client = null;
while (true) {
try {
client = server.accept();
// See who the connection is from
InetAddress inetAddress = client.getInetAddress();
System.out.println("Accepted connection from " + inetAddress.getHostAddress() + " (" + inetAddress.getHostName() + ")...");
// Receive the porting changes one at a time
receiveChanges(client);
System.out.println("Waiting for connection...");
} finally {
if (client != null) {
client.close();
client = null;
}
}
}
} finally {
if (server != null) {
server.close();
}
}
}
/**
* Receives porting changes one at a time.
*/
private void receiveChanges(Socket socket) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
OutputStream out = socket.getOutputStream();
String change;
while ((change = in.readLine()) != null) {
// Received a porting change
System.out.println("Received porting change: " + change);
// Parse the porting change into individual fields
String mobileNumber = null;
String cac = null;
String timestamp = null;
try {
StringTokenizer strTok = new StringTokenizer(change, "|");
mobileNumber = strTok.nextToken();
cac = strTok.nextToken();
timestamp = strTok.nextToken();
} catch (NoSuchElementException e) {
// Format not correct, but in this example continue anyway
}
System.out.println("Mobile Number : " + mobileNumber);
System.out.println("CAC : " + cac);
System.out.println("Timestamp : " + timestamp);
// Acknowledge the porting change so the DatafeedSender sends the next one
out.write("OK".getBytes());
out.write(0x0A);
out.flush();
}
}
/**
* The entry point into this program.
*/
public static void main(String[] args) throws IOException {
DatafeedReceiver datafeedReceiver = new DatafeedReceiver();
datafeedReceiver.receive();
}
}