Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (c) 1996-2001
   * Logica Mobile Networks Limited
   * All rights reserved.
   *
   * This software is distributed under Logica Open Source License Version 1.0
   * ("Licence Agreement"). You shall use it and distribute only in accordance
   * with the terms of the License Agreement.
   *
  */
 package org.smpp.smscsim;
 
 
 import org.smpp.*;
 import org.smpp.pdu.*;
Class SimulatorPDUProcessor gets the Request from the client and creates the proper Response and sends it. At the beginning it authenticates the client using information in the bind request and list of users provided during construction of the processor. It also stores messages sent from client and allows cancellation and replacement of the messages.

Author(s):
Logica Mobile Networks SMPP Open Source Team
Version:
$Revision: 1.2 $
See also:
PDUProcessor
SimulatorPDUProcessorFactory
SMSCSession
ShortMessageStore
org.smpp.smscsim.util.Table
 
 public class SimulatorPDUProcessor extends PDUProcessor {
The session this processor uses for sending of PDUs.
 
 	private SMSCSession session = null;

The container for received messages.
 
 	private ShortMessageStore messageStore = null;

The thread which sends delivery information for messages which require delivery information.
 
The table with system id's and passwords for authenticating of the bounding ESMEs.
 
 	private Table users = null;

Indicates if the bound has passed.
 
 	private boolean bound = false;

The system id of the bounded ESME.
 
 	private String systemId = null;

If the information about processing has to be printed to the standard output.
 
 	private boolean displayInfo = false;

The message id assigned by simulator to submitted messages.
 
 	private static int intMessageId = 2000;

System id of this simulator sent to the ESME in bind response.
 
 	private static final String SYSTEM_ID = "Smsc Simulator";

The name of attribute which contains the system id of ESME.
 
 	private static final String SYSTEM_ID_ATTR = "name";

The name of attribute which conatins password of ESME.
 
 	private static final String PASSWORD_ATTR = "password";
 
 	private Debug debug = SmppObject.getDebug();
 	private Event event = SmppObject.getEvent();

Constructs the PDU processor with given session, message store for storing of the messages and a table of users for authentication.

Parameters:
session the sessin this PDU processor works for
messageStore the store for messages received from the client
users the list of users used for authenticating of the client
	public SimulatorPDUProcessor(SMSCSession sessionShortMessageStore messageStoreTable users) {
		this. = session;
		this. = messageStore;
		this. = users;
	}
	public void stop() {
	}

Depending on the commandId of the request creates the proper response. The first request must be a BindRequest with the correct parameters.

Parameters:
request the request from client
	public void clientRequest(Request request) {
		.write("SimulatorPDUProcessor.clientRequest() " + request.debugString());
		Response response;
		int commandStatus;
		int commandId = request.getCommandId();
		try {
			display("client request: " + request.debugString());
			if (!) { // the first PDU must be bound request
				if (commandId == .
					|| commandId == .
					|| commandId == .) {
					commandStatus = checkIdentity((BindRequestrequest);
					if (commandStatus == 0) { // authenticated
						// firstly generate proper bind response
						BindResponse bindResponse = (BindResponserequest.getResponse();
						bindResponse.setSystemId();
						// and send it to the client via serverResponse
						serverResponse(bindResponse);
						// success => bound
						 = true;
else { // system id not authenticated
						// get the response
						response = request.getResponse();
						// set it the error command status
						response.setCommandStatus(commandStatus);
						// and send it to the client via serverResponse
						serverResponse(response);
						// bind failed, stopping the session
					}
else {
					// the request isn't a bound req and this is wrong: if not
					// bound, then the server expects bound PDU
					if (request.canResponse()) {
						// get the response
						response = request.getResponse();
						// and send it to the client via serverResponse
						serverResponse(response);
else {
						// cannot respond to a request which doesn't have
						// a response :-(
					}
					// bind failed, stopping the session
				}
else { // already bound, can receive other PDUs
				if (request.canResponse()) {
					response = request.getResponse();
					switch (commandId) { // for selected PDUs do extra steps
						case . :
							SubmitSMResp submitResponse = (SubmitSMRespresponse;
							submitResponse.setMessageId(assignMessageId());
							display("putting message into message store");
							.submit((SubmitSMrequestsubmitResponse.getMessageId(), );
							byte registeredDelivery =
							if (registeredDelivery == .) {
								.submit(this, (SubmitSMrequestsubmitResponse.getMessageId());
							}
							break;
							SubmitMultiSMResp submitMultiResponse = (SubmitMultiSMRespresponse;
							submitMultiResponse.setMessageId(assignMessageId());
							break;
							DeliverSMResp deliverResponse = (DeliverSMRespresponse;
							deliverResponse.setMessageId(assignMessageId());
							break;
						case . :
							DataSMResp dataResponse = (DataSMRespresponse;
							break;
						case . :
							QuerySM queryRequest = (QuerySMrequest;
							QuerySMResp queryResponse = (QuerySMRespresponse;
							display("querying message in message store");
							queryResponse.setMessageId(queryRequest.getMessageId());
							break;
						case . :
							CancelSM cancelRequest = (CancelSMrequest;
							display("cancelling message in message store");
							break;
							ReplaceSM replaceRequest = (ReplaceSMrequest;
							display("replacing message in message store");
							.replace(replaceRequest.getMessageId(), replaceRequest.getShortMessage());
							break;
						case . :
							// do nothing, just respond and after sending
							// the response stop the session
							break;
					}
					// send the prepared response
					serverResponse(response);
					if (commandId == .) {
						// unbind causes stopping of the session
					}
else {
					// can't respond => nothing to do :-)
				}
			}
			.write(e"");
catch (Exception e) {
			.write(e"");
		}
	}

Processes the response received from the client.

Parameters:
response the response from client
	public void clientResponse(Response response) {
		.write("SimulatorPDUProcessor.clientResponse() " + response.debugString());
		display("client response: " + response.debugString());
	}

Sends a request to a client. For example, it can be used to send delivery info to the client.

Parameters:
request the request to be sent to the client
	public void serverRequest(Request requestthrows IOExceptionPDUException {
		.write("SimulatorPDUProcessor.serverRequest() " + request.debugString());
		display("server request: " + request.debugString());
		.send(request);
	}

Send the response created by clientRequest to the client.

Parameters:
response the response to send to client
	public void serverResponse(Response responsethrows IOExceptionPDUException {
		synchronized(this) {
			try {
				// Sleep for 300 ms sending reply to more accuratly simulate smsc
				this.wait(300);
catchException e ) { e.printStackTrace(); }
		}
		.write("SimulatorPDUProcessor.serverResponse() " + response.debugString());
		display("server response: " + response.debugString());
		.send(response);
	}

Checks if the bind request contains valid system id and password. For this uses the table of users provided in the constructor of the SimulatorPDUProcessor. If the authentication fails, i.e. if either the user isn't found or the password is incorrect, the function returns proper status code.

Parameters:
request the bind request as received from the client
Returns:
status code of the authentication; ESME_ROK if authentication passed
	private int checkIdentity(BindRequest request) {
		int commandStatus = .;
		Record user = .find(request.getSystemId());
		if (user != null) {
			String password = user.getValue();
			if (password != null) {
				if (!request.getPassword().equals(password)) {
					commandStatus = .;
					.write("system id " + request.getSystemId() + " not authenticated. Invalid password.");
					display("not authenticated " + request.getSystemId() + " -- invalid password");
else {
					 = request.getSystemId();
					.write("system id " +  + " authenticated");
					display("authenticated " + );
				}
else {
				commandStatus = .;
					"system id " +  + " not authenticated. " + "Password attribute not found in users file");
				display("not authenticated " +  + " -- no password for user.");
			}
else {
			commandStatus = .;
			.write("system id " + request.getSystemId() + " not authenticated -- not found");
			display("not authenticated " + request.getSystemId() + " -- user not found");
		}
		return commandStatus;
	}

Creates a unique message_id for each sms sent by a client to the smsc.

Returns:
unique message id
	private String assignMessageId() {
		String messageId = "Smsc";
		messageId += ;
		return messageId;
	}

Returns the session this PDU processor works for.

Returns:
the session of this PDU processor
	public SMSCSession getSession() {
		return ;
	}

Returns the system id of the client for whose is this PDU processor processing PDUs.

Returns:
system id of client
	public String getSystemId() {
		return ;
	}

Sets if the info about processing has to be printed on the standard output.
	public void setDisplayInfo(boolean on) {
	}

Returns status of printing of processing info on the standard output.
	public boolean getDisplayInfo() {
		return ;
	}

Sets the delivery info sender object which is used to generate and send delivery pdus for messages which require the delivery info as the outcome of their sending.
	public void setDeliveryInfoSender(DeliveryInfoSender deliveryInfoSender) {
		this. = deliveryInfoSender;
	}
	private void display(String info) {
		if (getDisplayInfo()) {
			String sysId = getSystemId();
			if (sysId == null) {
				sysId = "";
			}
			..println(FileLog.getLineTimeStamp() + " [" + sysId + "] " + info);
		}
	}
/*
 * $Log: not supported by cvs2svn $
 * Revision 1.1  2003/07/23 00:28:39  sverkera
 * Imported
 *
 * 
 * Old changelog:
 * 20-09-01 ticp@logica.com added reference to the DeliveryInfoSender to support
 *                          automatic sending of delivery info PDUs
 */
New to GrepCode? Check out our FAQ X