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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908
| package org.smpp.test;
import java.io.*;
import java.util.Properties;
import org.smpp.*;
import org.smpp.TCPIPConnection;
import org.smpp.pdu.*;
import org.smpp.debug.Debug;
import org.smpp.debug.Event;
import org.smpp.debug.FileDebug;
import org.smpp.debug.FileEvent;
import org.smpp.util.Queue;
/**
* Class <code>SMPPTest</code> shows how to use the SMPP toolkit.
* You can bound and unbind from the SMSC, you can send every possible
* pdu and wait for a pdu sent from the SMSC.
*
* @author Logica Mobile Networks SMPP Open Source Team
* @version $Revision: 1.2 $
*/
public class SMPPTest {
static final String copyright =
"Copyright (c) 1996-2001 Logica Mobile Networks Limited\n"
+ "This product includes software developed by Logica by whom copyright\n"
+ "and know-how are retained, all rights reserved.\n";
static final String version = "SMPP Open Source test & demonstration application, version 1.1\n";
static {
System.out.println(copyright);
System.out.println(version);
}
/**
* Directory for creating of debug and event files.
*/
static final String dbgDir = "./";
/**
* The debug object.
* @see FileDebug
*/
static Debug debug = new FileDebug(dbgDir, "test.dbg");
/**
* The event object.
* @see FileEvent
*/
static Event event = new FileEvent(dbgDir, "test.evt");
/**
* File with default settings for the application.
*/
static String propsFilePath = "./etc/smpptest.cfg";
static BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
/**
* This is the SMPP session used for communication with SMSC.
*/
static Session session = null;
/**
* Contains the parameters and default values for this test
* application such as system id, password, default npi and ton
* of sender etc.
*/
Properties properties = new Properties();
/**
* If the application is bound to the SMSC.
*/
boolean bound = false;
/**
* If the application has to keep reading commands
* from the keyboard and to do what's requested.
*/
private boolean keepRunning = true;
/**
* Address of the SMSC.
*/
String ipAddress = null;
/**
* The port number to bind to on the SMSC server.
*/
int port = 0;
/**
* The name which identifies you to SMSC.
*/
String systemId = null;
/**
* The password for authentication to SMSC.
*/
String password = null;
/**
* How you want to bind to the SMSC: transmitter (t), receiver (r) or
* transciever (tr). Transciever can both send messages and receive
* messages. Note, that if you bind as receiver you can still receive
* responses to you requests (submissions).
*/
String bindOption = "t";
/**
* Indicates that the Session has to be asynchronous.
* Asynchronous Session means that when submitting a Request to the SMSC
* the Session does not wait for a response. Instead the Session is provided
* with an instance of implementation of ServerPDUListener from the smpp
* library which receives all PDUs received from the SMSC. It's
* application responsibility to match the received Response with sended Requests.
*/
boolean asynchronous = false;
/**
* This is an instance of listener which obtains all PDUs received from the SMSC.
* Application doesn't have explicitly call Session's receive() function,
* all PDUs are passed to this application callback object.
* See documentation in Session, Receiver and ServerPDUEventListener classes
* form the SMPP library.
*/
SMPPTestPDUEventListener pduListener = null;
/**
* The range of addresses the smpp session will serve.
*/
AddressRange addressRange = new AddressRange();
/*
* for information about these variables have a look in SMPP 3.4
* specification
*/
String systemType = "";
String serviceType = "";
Address sourceAddress = new Address();
Address destAddress = new Address();
String scheduleDeliveryTime = "";
String validityPeriod = "";
String shortMessage = "";
int numberOfDestination = 1;
String messageId = "";
byte esmClass = 0;
byte protocolId = 0;
byte priorityFlag = 0;
byte registeredDelivery = 0;
byte replaceIfPresentFlag = 0;
byte dataCoding = 0;
byte smDefaultMsgId = 0;
/**
* If you attemt to receive message, how long will the application
* wait for data.
*/
long receiveTimeout = Data.RECEIVE_BLOCKING;
/**
* Initialises the application, lods default values for
* connection to SMSC and for various PDU fields.
*/
public SMPPTest() throws IOException {
loadProperties(propsFilePath);
}
/**
* Sets global SMPP library debug and event objects.
* Runs the application.
* @see SmppObject#setDebug(Debug)
* @see SmppObject#setEvent(Event)
*/
public static void main(String args[]) {
System.out.println("Initialising...");
System.out.println("Charset used: " + new InputStreamReader(System.in).getEncoding());
debug.activate();
event.activate();
SmppObject.setDebug(debug);
SmppObject.setEvent(event);
SMPPTest test = null;
try {
test = new SMPPTest();
} catch (IOException e) {
event.write(e, "");
debug.write("exception initialising SMPPTest " + e);
System.out.println("Exception initialising SMPPTest " + e);
}
if (test != null) {
test.menu();
}
}
/**
* Displays the menu and lets you choose from available options.
*/
public void menu() {
keepRunning = true;
String option = "1";
int optionInt;
while (keepRunning) {
System.out.println();
System.out.println("- 1 bind");
System.out.println("- 2 submit (t/tr)");
System.out.println("- 3 submit multi (t/tr)");
System.out.println("- 4 data (t/tr)");
System.out.println("- 5 query (t/tr)");
System.out.println("- 6 replace (t/tr)");
System.out.println("- 7 cancel (t/tr)");
System.out.println("- 8 enquire link (t/tr)");
System.out.println("- 9 unbind");
System.out.println("- 10 receive message (tr/r)");
System.out.println("- 0 exit");
System.out.print("> ");
optionInt = -1;
try {
option = keyboard.readLine();
optionInt = Integer.parseInt(option);
} catch (Exception e) {
debug.write("exception reading keyboard " + e);
optionInt = -1;
}
switch (optionInt) {
case 1 :
bind();
break;
case 2 :
submit();
break;
case 3 :
submitMulti();
break;
case 4 :
data();
break;
case 5 :
query();
break;
case 6 :
replace();
break;
case 7 :
cancel();
break;
case 8 :
enquireLink();
break;
case 9 :
unbind();
break;
case 10 :
receive();
break;
case 0 :
exit();
break;
case -1 :
// default option if entering an option went wrong
break;
default :
System.out.println("Invalid option. Choose between 0 and 10.");
break;
}
}
}
private void bind() {
debug.enter(this, "SMPPTest.bind()");
try {
if (bound) {
System.out.println("Already bound, unbind first.");
return;
}
BindRequest request = null;
BindResponse response = null;
String syncMode = (asynchronous ? "a" : "s");
// type of the session
syncMode = getParam("Asynchronous/Synchronnous Session? (a/s)", syncMode);
if (syncMode.compareToIgnoreCase("a") == 0) {
asynchronous = true;
} else if (syncMode.compareToIgnoreCase("s") == 0) {
asynchronous = false;
} else {
System.out.println(
"Invalid mode async/sync, expected a or s, got " + syncMode + ". Operation canceled.");
return;
}
// input values
bindOption = getParam("Transmitter/Receiver/Transciever (t/r/tr)", bindOption);
if (bindOption.compareToIgnoreCase("t") == 0) {
request = new BindTransmitter();
} else if (bindOption.compareToIgnoreCase("r") == 0) {
request = new BindReceiver();
} else if (bindOption.compareToIgnoreCase("tr") == 0) {
request = new BindTransciever();
} else {
System.out.println(
"Invalid bind mode, expected t, r or tr, got " + bindOption + ". Operation canceled.");
return;
}
ipAddress = getParam("IP address of SMSC", ipAddress);
port = getParam("Port number", port);
TCPIPConnection connection = new TCPIPConnection(ipAddress, port);
connection.setReceiveTimeout(20 * 1000);
session = new Session(connection);
systemId = getParam("Your system ID", systemId);
password = getParam("Your password", password);
// set values
request.setSystemId(systemId);
request.setPassword(password);
request.setSystemType(systemType);
request.setInterfaceVersion((byte) 0x34);
request.setAddressRange(addressRange);
// send the request
System.out.println("Bind request " + request.debugString());
if (asynchronous) {
pduListener = new SMPPTestPDUEventListener(session);
response = session.bind(request, pduListener);
} else {
response = session.bind(request);
}
System.out.println("Bind response " + response.debugString());
if (response.getCommandStatus() == Data.ESME_ROK) {
bound = true;
}
} catch (Exception e) {
event.write(e, "");
debug.write("Bind operation failed. " + e);
System.out.println("Bind operation failed. " + e);
} finally {
debug.exit(this);
}
}
private void unbind() {
debug.enter(this, "SMPPTest.unbind()");
try {
if (!bound) {
System.out.println("Not bound, cannot unbind.");
return;
}
// send the request
System.out.println("Going to unbind.");
if (session.getReceiver().isReceiver()) {
System.out.println("It can take a while to stop the receiver.");
}
UnbindResp response = session.unbind();
System.out.println("Unbind response " + response.debugString());
bound = false;
} catch (Exception e) {
event.write(e, "");
debug.write("Unbind operation failed. " + e);
System.out.println("Unbind operation failed. " + e);
} finally {
debug.exit(this);
}
}
private void submit() {
debug.enter(this, "SMPPTest.submit()");
try {
SubmitSM request = new SubmitSM();
SubmitSMResp response;
// input values
serviceType = getParam("Service type", serviceType);
sourceAddress = getAddress("Source", sourceAddress);
destAddress = getAddress("Destination", destAddress);
replaceIfPresentFlag = getParam("Replace if present flag", replaceIfPresentFlag);
shortMessage = getParam("The short message", shortMessage);
scheduleDeliveryTime = getParam("Schedule delivery time", scheduleDeliveryTime);
validityPeriod = getParam("Validity period", validityPeriod);
esmClass = getParam("Esm class", esmClass);
protocolId = getParam("Protocol id", protocolId);
priorityFlag = getParam("Priority flag", priorityFlag);
registeredDelivery = getParam("Registered delivery", registeredDelivery);
dataCoding = getParam("Data encoding", dataCoding);
smDefaultMsgId = getParam("Sm default msg id", smDefaultMsgId);
// set values
request.setServiceType(serviceType);
request.setSourceAddr(sourceAddress);
request.setDestAddr(destAddress);
request.setReplaceIfPresentFlag(replaceIfPresentFlag);
request.setShortMessage(shortMessage,Data.ENC_GSM7BIT);
request.setScheduleDeliveryTime(scheduleDeliveryTime);
request.setValidityPeriod(validityPeriod);
request.setEsmClass(esmClass);
request.setProtocolId(protocolId);
request.setPriorityFlag(priorityFlag);
request.setRegisteredDelivery(registeredDelivery);
request.setDataCoding(dataCoding);
request.setSmDefaultMsgId(smDefaultMsgId);
// send the request
int count = 1;
System.out.println();
count = getParam("How many times to submit this message (load test)", count);
for (int i = 0; i < count; i++) {
request.assignSequenceNumber(true);
System.out.print("#" + i + " ");
System.out.println("Submit request " + request.debugString());
if (asynchronous) {
session.submit(request);
System.out.println();
} else {
response = session.submit(request);
System.out.println("Submit response " + response.debugString());
messageId = response.getMessageId();
}
}
} catch (Exception e) {
event.write(e, "");
debug.write("Submit operation failed. " + e);
System.out.println("Submit operation failed. " + e);
} finally {
debug.exit(this);
}
}
private void submitMulti() {
debug.enter(this, "SMPPTest.submitMulti()");
try {
SubmitMultiSM request = new SubmitMultiSM();
SubmitMultiSMResp response;
// input values and set some :-)
serviceType = getParam("Service type", serviceType);
sourceAddress = getAddress("Source", sourceAddress);
numberOfDestination = getParam("Number of destinations", numberOfDestination);
for (int i = 0; i < numberOfDestination; i++) {
request.addDestAddress(new DestinationAddress(getAddress("Destination", destAddress)));
}
replaceIfPresentFlag = getParam("Replace if present flag", replaceIfPresentFlag);
shortMessage = getParam("The short message", shortMessage);
scheduleDeliveryTime = getParam("Schdule delivery time", scheduleDeliveryTime);
validityPeriod = getParam("Validity period", validityPeriod);
esmClass = getParam("Esm class", esmClass);
protocolId = getParam("Protocol id", protocolId);
priorityFlag = getParam("Priority flag", priorityFlag);
registeredDelivery = getParam("Registered delivery", registeredDelivery);
dataCoding = getParam("Data encoding", dataCoding);
smDefaultMsgId = getParam("Sm default msg id", smDefaultMsgId);
// set other values
request.setServiceType(serviceType);
request.setSourceAddr(sourceAddress);
request.setReplaceIfPresentFlag(replaceIfPresentFlag);
request.setShortMessage(shortMessage);
request.setScheduleDeliveryTime(scheduleDeliveryTime);
request.setValidityPeriod(validityPeriod);
request.setEsmClass(esmClass);
request.setProtocolId(protocolId);
request.setPriorityFlag(priorityFlag);
request.setRegisteredDelivery(registeredDelivery);
request.setDataCoding(dataCoding);
request.setSmDefaultMsgId(smDefaultMsgId);
// send the request
System.out.println("Submit Multi request " + request.debugString());
if (asynchronous) {
session.submitMulti(request);
} else {
response = session.submitMulti(request);
System.out.println("Submit Multi response " + response.debugString());
messageId = response.getMessageId();
}
} catch (Exception e) {
event.write(e, "");
debug.write("Submit Multi operation failed. " + e);
System.out.println("Submit Multi operation failed. " + e);
} finally {
debug.exit(this);
}
}
private void replace() {
debug.enter(this, "SMPPTest.replace()");
try {
ReplaceSM request = new ReplaceSM();
ReplaceSMResp response;
// input values
messageId = getParam("Message id", messageId);
sourceAddress = getAddress("Source", sourceAddress);
shortMessage = getParam("The short message", shortMessage);
scheduleDeliveryTime = getParam("Schedule delivery time", scheduleDeliveryTime);
validityPeriod = getParam("Validity period", validityPeriod);
registeredDelivery = getParam("Registered delivery", registeredDelivery);
smDefaultMsgId = getParam("Sm default msg id", smDefaultMsgId);
// set values
request.setMessageId(messageId);
request.setSourceAddr(sourceAddress);
request.setShortMessage(shortMessage);
request.setScheduleDeliveryTime(scheduleDeliveryTime);
request.setValidityPeriod(validityPeriod);
request.setRegisteredDelivery(registeredDelivery);
request.setSmDefaultMsgId(smDefaultMsgId);
// send the request
System.out.println("Replace request " + request.debugString());
if (asynchronous) {
session.replace(request);
} else {
response = session.replace(request);
System.out.println("Replace response " + response.debugString());
}
} catch (Exception e) {
event.write(e, "");
debug.write("Replace operation failed. " + e);
System.out.println("Replace operation failed. " + e);
} finally {
debug.exit(this);
}
}
private void cancel() {
debug.enter(this, "SMPPTest.cancel()");
try {
CancelSM request = new CancelSM();
CancelSMResp response;
// input values
serviceType = getParam("Service type", serviceType);
messageId = getParam("Message id", messageId);
sourceAddress = getAddress("Source", sourceAddress);
destAddress = getAddress("Destination", destAddress);
// set values
request.setServiceType(serviceType);
request.setMessageId(messageId);
request.setSourceAddr(sourceAddress);
request.setDestAddr(destAddress);
// send the request
System.out.println("Cancel request " + request.debugString());
if (asynchronous) {
session.cancel(request);
} else {
response = session.cancel(request);
System.out.println("Cancel response " + response.debugString());
}
} catch (Exception e) {
event.write(e, "");
debug.write("Cancel operation failed. " + e);
System.out.println("Cancel operation failed. " + e);
} finally {
debug.exit(this);
}
}
private void data() {
debug.enter(this, "SMPPTest.data()");
try {
DataSM request = new DataSM();
DataSMResp response;
// input values
serviceType = getParam("Service type", serviceType);
sourceAddress = getAddress("Source", sourceAddress, Data.SM_DATA_ADDR_LEN);
destAddress = getAddress("Destination", destAddress, Data.SM_DATA_ADDR_LEN);
esmClass = getParam("Esm class", esmClass);
registeredDelivery = getParam("Registered delivery", registeredDelivery);
dataCoding = getParam("Data encoding", dataCoding);
// set values
request.setServiceType(serviceType);
request.setSourceAddr(sourceAddress);
request.setDestAddr(destAddress);
request.setEsmClass(esmClass);
request.setRegisteredDelivery(registeredDelivery);
request.setDataCoding(dataCoding);
// send the request
System.out.println("Data request " + request.debugString());
if (asynchronous) {
session.data(request);
} else {
response = session.data(request);
System.out.println("Data response " + response.debugString());
messageId = response.getMessageId();
}
} catch (Exception e) {
event.write(e, "");
debug.write("Data operation failed. " + e);
System.out.println("Data operation failed. " + e);
} finally {
debug.exit(this);
}
}
private void query() {
debug.enter(this, "SMPPTest.query()");
try {
QuerySM request = new QuerySM();
QuerySMResp response;
// input values
messageId = getParam("Message id", messageId);
sourceAddress = getAddress("Source", sourceAddress);
// set values
request.setMessageId(messageId);
request.setSourceAddr(sourceAddress);
// send the request
System.out.println("Query request " + request.debugString());
if (asynchronous) {
session.query(request);
} else {
response = session.query(request);
System.out.println("Query response " + response.debugString());
messageId = response.getMessageId();
}
} catch (Exception e) {
event.write(e, "");
debug.write("Query operation failed. " + e);
System.out.println("Query operation failed. " + e);
} finally {
debug.exit(this);
}
}
private void enquireLink() {
debug.enter(this, "SMPPTest.enquireLink()");
try {
EnquireLink request = new EnquireLink();
EnquireLinkResp response;
System.out.println("Enquire Link request " + request.debugString());
if (asynchronous) {
session.enquireLink(request);
} else {
response = session.enquireLink(request);
System.out.println("Enquire Link response " + response.debugString());
}
} catch (Exception e) {
event.write(e, "");
debug.write("Enquire Link operation failed. " + e);
System.out.println("Enquire Link operation failed. " + e);
} finally {
debug.exit(this);
}
}
private void receive() {
debug.enter(this, "SMPPTest.receive()");
try {
PDU pdu = null;
System.out.print("Going to receive a PDU. ");
if (receiveTimeout == Data.RECEIVE_BLOCKING) {
System.out.print(
"The receive is blocking, i.e. the application " + "will stop until a PDU will be received.");
} else {
System.out.print("The receive timeout is " + receiveTimeout / 1000 + " sec.");
}
System.out.println();
if (asynchronous) {
ServerPDUEvent pduEvent = pduListener.getRequestEvent(receiveTimeout);
if (pduEvent != null) {
pdu = pduEvent.getPDU();
}
} else {
pdu = session.receive(receiveTimeout);
}
if (pdu != null) {
System.out.println("Received PDU " + pdu.debugString());
if (pdu.isRequest()) {
Response response = ((Request) pdu).getResponse();
// respond with default response
System.out.println("Going to send default response to request " + response.debugString());
session.respond(response);
}
} else {
System.out.println("No PDU received this time.");
}
} catch (Exception e) {
event.write(e, "");
debug.write("Receiving failed. " + e);
System.out.println("Receiving failed. " + e);
} finally {
debug.exit(this);
}
}
private void exit() {
debug.enter(this, "SMPPTest.exit()");
if (bound) {
unbind();
}
keepRunning = false;
debug.exit(this);
}
private class SMPPTestPDUEventListener extends SmppObject implements ServerPDUEventListener {
Session session;
Queue requestEvents = new Queue();
public SMPPTestPDUEventListener(Session session) {
this.session = session;
}
public void handleEvent(ServerPDUEvent event) {
PDU pdu = event.getPDU();
if (pdu.isRequest()) {
System.out.println("async request received, enqueuing " + pdu.debugString());
synchronized (requestEvents) {
requestEvents.enqueue(event);
requestEvents.notify();
}
} else if (pdu.isResponse()) {
System.out.println("async response received " + pdu.debugString());
} else {
System.out.println(
"pdu of unknown class (not request nor " + "response) received, discarding " + pdu.debugString());
}
}
public ServerPDUEvent getRequestEvent(long timeout) {
ServerPDUEvent pduEvent = null;
synchronized (requestEvents) {
if (requestEvents.isEmpty()) {
try {
requestEvents.wait(timeout);
} catch (InterruptedException e) {
// ignoring, actually this is what we're waiting for
}
}
if (!requestEvents.isEmpty()) {
pduEvent = (ServerPDUEvent) requestEvents.dequeue();
}
}
return pduEvent;
}
}
private String getParam(String prompt, String defaultValue) {
String value = "";
String promptFull = prompt;
promptFull += defaultValue == null ? "" : " [" + defaultValue + "] ";
System.out.print(promptFull);
try {
value = keyboard.readLine();
} catch (IOException e) {
event.write(e, "");
debug.write("Got exception getting a param. " + e);
}
if (value.compareTo("") == 0) {
return defaultValue;
} else {
return value;
}
}
private byte getParam(String prompt, byte defaultValue) {
return Byte.parseByte(getParam(prompt, Byte.toString(defaultValue)));
}
private int getParam(String prompt, int defaultValue) {
return Integer.parseInt(getParam(prompt, Integer.toString(defaultValue)));
}
private Address getAddress(String type, Address address, int maxAddressLength)
throws WrongLengthOfStringException {
byte ton = getParam(type + " address TON", address.getTon());
byte npi = getParam(type + " address NPI", address.getNpi());
String addr = getParam(type + " address", address.getAddress());
address.setTon(ton);
address.setNpi(npi);
address.setAddress(addr, maxAddressLength);
return address;
}
private Address getAddress(String type, Address address) throws WrongLengthOfStringException {
return getAddress(type, address, Data.SM_ADDR_LEN);
}
private void loadProperties(String fileName) throws IOException {
System.out.println("Reading configuration file " + fileName + "...");
FileInputStream propsFile = new FileInputStream(fileName);
properties.load(propsFile);
propsFile.close();
System.out.println("Setting default parameters...");
byte ton;
byte npi;
String addr;
String bindMode;
int rcvTimeout;
String syncMode;
ipAddress = properties.getProperty("ip-address");
port = getIntProperty("port", port);
systemId = properties.getProperty("system-id");
password = properties.getProperty("password");
ton = getByteProperty("addr-ton", addressRange.getTon());
npi = getByteProperty("addr-npi", addressRange.getNpi());
addr = properties.getProperty("address-range", addressRange.getAddressRange());
addressRange.setTon(ton);
addressRange.setNpi(npi);
try {
addressRange.setAddressRange(addr);
} catch (WrongLengthOfStringException e) {
System.out.println("The length of address-range parameter is wrong.");
}
ton = getByteProperty("source-ton", sourceAddress.getTon());
npi = getByteProperty("source-npi", sourceAddress.getNpi());
addr = properties.getProperty("source-address", sourceAddress.getAddress());
setAddressParameter("source-address", sourceAddress, ton, npi, addr);
ton = getByteProperty("destination-ton", destAddress.getTon());
npi = getByteProperty("destination-npi", destAddress.getNpi());
addr = properties.getProperty("destination-address", destAddress.getAddress());
setAddressParameter("destination-address", destAddress, ton, npi, addr);
serviceType = properties.getProperty("service-type", serviceType);
systemType = properties.getProperty("system-type", systemType);
bindMode = properties.getProperty("bind-mode", bindOption);
if (bindMode.equalsIgnoreCase("transmitter")) {
bindMode = "t";
} else if (bindMode.equalsIgnoreCase("receiver")) {
bindMode = "r";
} else if (bindMode.equalsIgnoreCase("transciever")) {
bindMode = "tr";
} else if (
!bindMode.equalsIgnoreCase("t") && !bindMode.equalsIgnoreCase("r") && !bindMode.equalsIgnoreCase("tr")) {
System.out.println(
"The value of bind-mode parameter in "
+ "the configuration file "
+ fileName
+ " is wrong. "
+ "Setting the default");
bindMode = "t";
}
bindOption = bindMode;
// receive timeout in the cfg file is in seconds, we need milliseconds
// also conversion from -1 which indicates infinite blocking
// in the cfg file to Data.RECEIVE_BLOCKING which indicates infinite
// blocking in the library is needed.
if (receiveTimeout == Data.RECEIVE_BLOCKING) {
rcvTimeout = -1;
} else {
rcvTimeout = ((int) receiveTimeout) / 1000;
}
rcvTimeout = getIntProperty("receive-timeout", rcvTimeout);
if (rcvTimeout == -1) {
receiveTimeout = Data.RECEIVE_BLOCKING;
} else {
receiveTimeout = rcvTimeout * 1000;
}
syncMode = properties.getProperty("sync-mode", (asynchronous ? "async" : "sync"));
if (syncMode.equalsIgnoreCase("sync")) {
asynchronous = false;
} else if (syncMode.equalsIgnoreCase("async")) {
asynchronous = true;
} else {
asynchronous = false;
}
private int getIntProperty(String propName, int defaultValue) {
return Integer.parseInt(properties.getProperty(propName, Integer.toString(defaultValue)));
}
private void setAddressParameter(String descr, Address address, byte ton, byte npi, String addr) {
address.setTon(ton);
address.setNpi(npi);
try {
address.setAddress(addr);
} catch (WrongLengthOfStringException e) {
System.out.println("The length of " + descr + " parameter is wrong.");
}
}
} |
Partager