Sekarang buatlah sebuah project Java biasa pada NetBeans IDE dan berilah nama project tersebut contohnya Implementasi, dan buatlah sebuah package dahulu dengan nama id.web.martinusadyh.iso8583.helper. Didalam package yang telah kita buat, buatlah sebuah helper class dengan nama DecimalHexBinaryConverter.java yang mempunyai fungsi sebagai konverter ketika kita ingin menghitung nilai Bitmap yang kurang lebih seperti kode dibawah ini :
SUMBER Referensi : http://martinusadyh.web.id dan wikipedia
- id.web.martinusadyh.iso8583.helper.DecimalHexBinaryConverter.java
Java12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152package id.web.martinusadyh.iso8583.helper;public class DecimalHexBinaryConverter {public static String decimalToHexa(Integer decimalNumber) {return Integer.toHexString(decimalNumber);}public static String decimalToBinary(Integer decimalNumber) {StringBuilder binaryNumber = new StringBuilder();StringBuilder sbBinary = new StringBuilder();String binaryString = Integer.toBinaryString(decimalNumber);char[] binary = binaryString.toCharArray();int counter = 0;// ambil dari index karakter terakhirfor (int i=binary.length-1; i>=0; i--) {counter++;sbBinary.append(binary[i]);// reset counter ke nol jika berhasil mengambil 4 digit karakterif (counter == 4) counter = 0;}// 4 adalah panjang karakter tiap blok di binary// ex: dec [100] == binary [0110 0100]for (int i=0; i<4-counter; i++) {if (counter > 0) sbBinary.append("0");}// sekarang dibalikfor (int i=sbBinary.length()-1; i>=0;i--) {binaryNumber.append(sbBinary.toString().charAt(i));}return binaryNumber.toString();}public static Integer binaryToDecimal(String binaryNumber) {return Integer.parseInt(binaryNumber, 2);}public static String binaryToHexa(String binaryNumber) {return decimalToHexa(binaryToDecimal(binaryNumber));}public static Integer hexaToDecimal(String hexaNumber) {return Integer.parseInt(hexaNumber, 16);}public static String hexaToBinary(String hexaNumber) {return decimalToBinary(hexaToDecimal(hexaNumber));}}
- id.web.martinusadyh.iso8583.helper.ISOUtil.java
Java123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100package id.web.martinusadyh.iso8583.helper;public class ISOUtil {/** Mencari panjang dari Bitmap 16 karakter atau 32 karakter, jika bit pertama* nilai-nya == 1 (active/TRUE) maka secondary bitmap active dan otomatis* panjang bitmap menjadi 32 karakter.* @param originalMsg message asli ISO beserta dengan MTI-nya* @return panjang bitmap yang harus di potong / diambil.*/public static Integer findLengthOfBitmap(String originalMsg) {// ambil 1 digit bitmap setelah MTIString bitPertama = originalMsg.substring(4,5);int panjangBitmap = 0;// cek nilai binary-nyaif (DecimalHexBinaryConverter.hexaToBinary(bitPertama).substring(0, 1).equalsIgnoreCase("1")) {panjangBitmap = 32;} else {panjangBitmap = 16;}return panjangBitmap;}public static String getHexaBitmapFromActiveDE(int[] activeDE) {StringBuilder finalHexaBitmap = new StringBuilder();StringBuilder binaryBitmapForReply = new StringBuilder();boolean secondarBitmapActive = false;int panjangBitmap = 16;// pengecekan secondary bitmapfor (int i=0; i<activeDE.length;i++) {if (activeDE[i] > 64) {secondarBitmapActive = true;panjangBitmap = 32;}}// x4 untuk mendapatkan jumlah seluruh data elemenpanjangBitmap *= 4;int counterBitmap=0;String active = "";for (int i=0;i<panjangBitmap; i++) {counterBitmap++;active = "0";for (int j=0; j<activeDE.length; j++) {if (counterBitmap == activeDE[j]) active = "1";}binaryBitmapForReply.append(active);}// karena secondary bitmap active, bit pertama ganti jadi 1if (secondarBitmapActive) {binaryBitmapForReply = new StringBuilder("1"+binaryBitmapForReply.toString().substring(1, binaryBitmapForReply.length()));}char[] binaryBitmapChar = binaryBitmapForReply.toString().toCharArray();int counter = 0;StringBuilder sb = new StringBuilder();for (int i=0;i<binaryBitmapChar.length;i++) {sb.append(binaryBitmapChar[i]);counter++;if (counter == 4) {finalHexaBitmap.append(DecimalHexBinaryConverter.binaryToHexa(sb.toString()));sb = new StringBuilder();counter=0;}}return finalHexaBitmap.toString();}public static String findMTI(String originalMsg) {return originalMsg.substring(0, 4);}public static String findBinaryBitmapFromHexa(String hexaBitmap) {StringBuilder binaryBitmap = new StringBuilder();char[] rawBitmap = hexaBitmap.toCharArray();for (int i=0; i<rawBitmap.length; i++) {binaryBitmap.append(DecimalHexBinaryConverter.hexaToBinary(String.valueOf(rawBitmap[i])));}return binaryBitmap.toString();}public static String findActiveDE(String binaryBitmap) {StringBuilder activeDE = new StringBuilder();char[] charBinaryBitmap = binaryBitmap.toCharArray();int counter = 0;for (int i=0;i<charBinaryBitmap.length;i++) {counter++;if (String.valueOf(charBinaryBitmap[i]).equals("1")) activeDE.append(String.valueOf(counter) + ";");}return activeDE.toString();}}
- id.web.martinusadyh.iso8583.socket.ServerISO.java
Java123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173package id.web.martinusadyh.iso8583.socket;import id.web.martinusadyh.iso8583.helper.ISOUtil;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintWriter;import java.net.ServerSocket;import java.net.Socket;import java.util.HashMap;import java.util.Map;/**** @author Martinus Ady H <mrt.itnewbies@gmail.com>*/public class ServerISO {private static final Integer PORT = 12345;private static final Map<String, Integer> mappingDENetworkMsg = new HashMap<String, Integer>();/* Method ini berfungsi untuk menginisialisasi data element dan panjang tiap* -tiap data element yang aktif */private static void initMappingDENetworkRequest() {/* [data-element] [panjang data element] */mappingDENetworkMsg.put("3", 6);mappingDENetworkMsg.put("7", 8);mappingDENetworkMsg.put("11", 6);mappingDENetworkMsg.put("12", 6);mappingDENetworkMsg.put("13", 4);mappingDENetworkMsg.put("39", 3);mappingDENetworkMsg.put("48", 999);mappingDENetworkMsg.put("70", 3);}/*** @param args the command line arguments*/public static void main(String[] args) throws IOException {initMappingDENetworkRequest();ServerSocket serverSocket = new ServerSocket(PORT);System.out.println("Server siap menerima koneksi pada port ["+PORT+"]");Socket socket = serverSocket.accept();InputStreamReader inStreamReader = new InputStreamReader(socket.getInputStream());PrintWriter sendMsg = new PrintWriter(socket.getOutputStream());int data;StringBuffer sb = new StringBuffer();int counter = 0;// tambahan 4 karakter karena msg header adalah 4 digit msg lengthint lengthOfMsg = 4;while((data = inStreamReader.read()) != 0) {counter++;sb.append((char) data);if (counter == 4) lengthOfMsg += Integer.valueOf(sb.toString());// klo panjang msg dari MTI sampai END OF MSG sama dengan nilai// header maka lanjutkan ke method processingMsg();if (lengthOfMsg == sb.toString().length()) {System.out.println("Rec. Msg ["+sb.toString()+"] len ["+sb.toString().length()+"]");processingMsg(sb.toString(), sendMsg);}}}/** Memproses msg yang dikirim oleh client berdasarkan nilai MTI.* @param data request msg yang berisi [header 4byte][MTI][BITMAP][DATA ELEMENT]* @param sendMsg object printWriter untuk menuliskan msg ke network stream*/private static void processingMsg(String data, PrintWriter sendMsg) {// msg.asli tanpa 4 digit msg.headerString origMsgWithoutMsgHeader = data.substring(4, data.length());// cek nilai MTIif (ISOUtil.findMTI(origMsgWithoutMsgHeader).equalsIgnoreCase("1800")) {handleNetworkMsg(origMsgWithoutMsgHeader, sendMsg);}}/** Method ini akan memproses network management request dan akan menambahkan* 1 data element yaitu data element 39 (response code) 000 ke client/sender* @param networkMsg request msg yang berisi [header 4byte][MTI][BITMAP][DATA ELEMENT]* @param sendMsg object printWriter untuk menuliskan msg ke network stream*/private static void handleNetworkMsg(String networkMsg, PrintWriter sendMsg) {int panjangBitmap = ISOUtil.findLengthOfBitmap(networkMsg);String hexaBitmap = networkMsg.substring(4, 4+panjangBitmap);// hitung bitmapString binaryBitmap = ISOUtil.findBinaryBitmapFromHexa(hexaBitmap);String[] activeDE = ISOUtil.findActiveDE(binaryBitmap).split(";");StringBuilder networkResp = new StringBuilder();// setting MTI untuk reply network requestnetworkResp.append("1810");// untuk reply, DE yang aktif adalah DE[3,7,11,12,13,39,48 dan 70]String bitmapReply = ISOUtil.getHexaBitmapFromActiveDE(new int[] {3,7,11,12,13,39,48, 70});networkResp.append(bitmapReply);// index msg dimulai dr (4 digit MTI+panjang bitmap = index DE ke 3)int startIndexMsg = 4+ISOUtil.findLengthOfBitmap(networkMsg);int nextIndex = startIndexMsg;String sisaDefaultDE = "";// ambil nilai DE yang sama dulufor (int i=0;i<activeDE.length;i++) {// ambil bit ke 3if (activeDE[i].equalsIgnoreCase("3")) {nextIndex += mappingDENetworkMsg.get(activeDE[i]);networkResp.append(networkMsg.substring(startIndexMsg, nextIndex));debugMessage(3, networkMsg.substring(startIndexMsg, nextIndex));} else if(activeDE[i].equalsIgnoreCase("7")) {startIndexMsg = nextIndex;nextIndex += mappingDENetworkMsg.get(activeDE[i]);networkResp.append(networkMsg.substring(startIndexMsg, nextIndex));debugMessage(7, networkMsg.substring(startIndexMsg, nextIndex));} else if(activeDE[i].equalsIgnoreCase("11")) {startIndexMsg = nextIndex;nextIndex += mappingDENetworkMsg.get(activeDE[i]);networkResp.append(networkMsg.substring(startIndexMsg, nextIndex));debugMessage(11, networkMsg.substring(startIndexMsg, nextIndex));} else if(activeDE[i].equalsIgnoreCase("12")) {startIndexMsg = nextIndex;nextIndex += mappingDENetworkMsg.get(activeDE[i]);networkResp.append(networkMsg.substring(startIndexMsg, nextIndex));debugMessage(12, networkMsg.substring(startIndexMsg, nextIndex));} else if(activeDE[i].equalsIgnoreCase("13")) {startIndexMsg = nextIndex;nextIndex += mappingDENetworkMsg.get(activeDE[i]);networkResp.append(networkMsg.substring(startIndexMsg, nextIndex));debugMessage(13, networkMsg.substring(startIndexMsg, nextIndex));} else if(activeDE[i].equalsIgnoreCase("48")) {startIndexMsg = nextIndex;// ambil dulu var.len utk DE 48int varLen = Integer.valueOf(networkMsg.substring(startIndexMsg, (startIndexMsg+3)));// 3 digit utk variabel lenvarLen += 3;nextIndex += varLen;sisaDefaultDE += networkMsg.substring(startIndexMsg, nextIndex);debugMessage(48, networkMsg.substring(startIndexMsg, nextIndex));} else if(activeDE[i].equalsIgnoreCase("70")) {startIndexMsg = nextIndex;nextIndex += mappingDENetworkMsg.get(activeDE[i]);sisaDefaultDE += networkMsg.substring(startIndexMsg, nextIndex);debugMessage(70, networkMsg.substring(startIndexMsg, nextIndex));}}// kasih response kode 39 successnetworkResp.append("000");// tambahkan sisa default DEnetworkResp.append(sisaDefaultDE);// tambahkan length 4 digit utk msg.headerString msgHeader = "";if (networkResp.length() < 10) msgHeader = "000" + networkResp.length();if (networkResp.length() < 100 && networkResp.length() >= 10) msgHeader = "00" + networkResp.length();if (networkResp.length() < 1000 && networkResp.length() >= 100) msgHeader = "0" + networkResp.length();if (networkResp.length() >= 1000) msgHeader = String.valueOf(networkResp.length());String finalMsg = msgHeader + networkResp.toString();// send to clientsendMsg.print(finalMsg);sendMsg.flush();}private static void debugMessage(Integer fieldNo, String msg) {System.out.println("["+fieldNo+"] ["+msg+"]");}}
SUMBER Referensi : http://martinusadyh.web.id dan wikipedia
Posting Komentar