Demo entry 6641480

java

   

Submitted by anonymous on Sep 18, 2017 at 15:36
Language: Java. Code size: 3.8 kB.

package moteur;

import java.io.IOException;
import java.net.DatagramPacket;
/*
 * This class is for receive messages from DMI
 * and collection of all the messages received
 *
 *   receiveThread used the same socket as sendThread to send OHS
 */
public class Sniffer implements Runnable {

	private static Thread receiveHOSThread = new Thread(new Sniffer());

	static byte[] HOS = new byte[376]; // the length of HOS = 376

	volatile boolean keepRunning = true;
	public void run() {
		// Create a DatagramPacket ready to receive something
		DatagramPacket receivedPacket = new DatagramPacket(HOS, HOS.length,
				Channel.addr_Inspector);

		while (keepRunning) {
			try {
				Channel.socketA.receive(receivedPacket); // blocks until a datagram is eceived
				System.out.println("I receive an HOS from DMI:     ");
			} catch (IOException e) {
				e.printStackTrace();
			}

			for (int i = 0; i < HOS.length; i++) {
				System.out.print(HOS[i] + "  ");
			}
			System.out.println();
			System.out.println();

		/*
		 * ****** write HOS telegram to each byte ***********
		 */
			HOS_Telegram.setTelegramID(HOS[0]);
			HOS_Telegram.setTelegramLength(byteToshort(HOS[1], HOS[2]));
			HOS_Telegram.setReleaseVersion(HOS[3]);
			HOS_Telegram.setSenderID(byteToshort(HOS[4], HOS[5]));
			HOS_Telegram.setReceiverID(byteToshort(HOS[6], HOS[7]));
			HOS_Telegram.setSequenceCounter(byteToshort(HOS[8], HOS[9]));

			HOS_Telegram.setHMIType(HOS[10]);
			HOS_Telegram.setHMIDiagnosticImage(HOS[11]);
			HOS_Telegram.setHMICommunicationStatus(HOS[12]);
			HOS_Telegram.setMaintenanceMenuActive(HOS[13]);
			HOS_Telegram.setRequestForUpdate(HOS[14]);
			HOS_Telegram.setOBCUResponsibleRef(HOS[15]);
			HOS_Telegram.setLastReceivedEventNo(byteToshort(HOS[16], HOS[17]));
			HOS_Telegram.setModeLevelPreselection(HOS[18]);
			HOS_Telegram.setRMSubsetSelection(HOS[19]);
			HOS_Telegram.setFAMSelectionDeselection(HOS[20]);
			HOS_Telegram.setCMSelectionDeselection(HOS[21]);
			HOS_Telegram.setReversalOperationSelection(HOS[22]);
			HOS_Telegram.setOverrideDDI(HOS[23]);
			HOS_Telegram.setButtonReactivationRequest(HOS[24]);
			HOS_Telegram.setDoorOpeningModeSelection(HOS[25]);
			HOS_Telegram.setDoorClosingModeSelection(HOS[26]);
			HOS_Telegram.setEnforceDoorReleaseSelection(HOS[27]);
			HOS_Telegram.setDoorclosedoverrideSelection(HOS[28]);
			HOS_Telegram.setPSDclosedoverrideSelection(HOS[29]);
			HOS_Telegram.setDoorReleaseRequestSelection(HOS[30]);
			HOS_Telegram.setDoorReleaseProhibitionSelection(HOS[31]);
			HOS_Telegram.setCrewNumberEntry(HOS[32]);
			HOS_Telegram.setTimetableAcknowledgementPressed(HOS[33]);
			HOS_Telegram.setSwitchOverCommand(HOS[34]);
			HOS_Telegram.setRemoteResetCommand(HOS[35]);
			HOS_Telegram.setTrainConsistSelection(HOS[36]);
			HOS_Telegram.setAPSN(byteToshort(HOS[37], HOS[38]));
			HOS_Telegram.setCRC(byteToshort(HOS[39], HOS[40]));
			HOS_Telegram.setMessageHOSCRC(byteToInt(HOS[41], HOS[42], HOS[43], HOS[44]));

			if(Channel.addr_Inspector.isUnresolved()){
				keepRunning = false;
			}
		}
	}

	// getter and setter
	public static Thread getReceiveHOSThread() {
		return receiveHOSThread;
	}

	public static void setReceiveHOSThread(Thread receiveThread) {
		Sniffer.receiveHOSThread = receiveThread;
	}

	public static byte[] getHOS() {
		return HOS;
	}

	public static short byteToshort(byte low, byte high) // method for transfer 2 bytes to short
	{
		return (short)( ((high&0xFF)<<8) | (low&0xFF) );
	}

	public static int byteToInt(byte b1, byte b2, byte b3, byte b4) // method for transfer 2 bytes to short
	{
		return (int) ((0xFF & b4) << 24) | ((0xFF & b3) << 16) |
            ((0xFF & b2) << 8) | (0xFF & b1);
	}

}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).