import
no.vianett.sms.SmsEventListener; |
import
no.vianett.sms.Sms; |
import
no.vianett.sms.SmsEvent; |
import
no.vianett.sms.component.SmsTransceiver; |
import
no.vianett.sms.log.SmsFileLogger; |
import
no.vianett.sms.event.SmsReceivedEvent; |
import
no.vianett.sms.event.SmsDeliveredEvent; |
import
no.vianett.sms.event.SmsSendingFailedEvent; |
import
no.vianett.sms.event.SmsDeliveryFailedEvent; |
import
java.io.FileNotFoundException; |
|
public
class
SmsEcho implements
SmsEventListener { |
private
SmsTransceiver transceiver = null;
|
private
int
counter = 0; |
|
public
SmsEcho() { |
transceiver = SmsTransceiver.getInstance();
// Get the transceiver object.
|
|
// Initialize transceiver.
|
String host = "cpa.vianett.no";
|
int
port = 31337; |
String username = "username";
|
String password =
"password";
|
boolean
keepAlive = true;
// If true, tranceiver sends
keep-alive messages to server every 30 seconds.
|
try
{ |
transceiver.initialize(host, port, username, password, keepAlive,
new
SmsFileLogger("C:\\sms.log"));
|
}
catch
(FileNotFoundException fnfe) { |
System.out.println(this
+ fnfe.getMessage()); |
} |
|
transceiver.addSmsEventListener(this);
// Registrer this class as a
listener for SMS events.
|
} |
|
// Just make a new instance of this
class. |
public
static
void
main(String[] args) { |
new
SmsEcho(); |
} |
|
/** |
* Listener. |
* |
* @param event
a <code>no.vianett.sms.SmsEvent</code> |
*/
|
public
void
eventHappened(SmsEvent event) { |
if
(event instanceof
SmsReceivedEvent) { |
System.out.println("Sms received.");
|
System.out.println("Refno : "
+ event.getReferenceId()); |
System.out.println("Event generated by
: " + event.getSource().getClass());
|
|
Sms in
= ((SmsReceivedEvent) event).getSms(); |
System.out.println("Sms : "
+ in);
// Write content of received
SMS. |
|
// Send echo back
|
Sms out = new
Sms(); |
out.setId(++this.counter);
|
out.setSender("1963");
// Set the sender number.
|
out.setMessage("Echo: "
+ in.getMessage());
|
out.setOperator(in.getOperator());
// Use the same operator.
|
out.setRecipient(in.getRecipient());
|
transceiver.send(out); |
}
else
if
(event instanceof
SmsDeliveredEvent) { |
System.out.println("Sms delivered.");
|
System.out.println("Refno : "
+ event.getReferenceId()); |
System.out.println("Event generated by
: " + event.getSource().getClass());
|
}
else
if
(event instanceof
SmsSendingFailedEvent) { |
System.out.println("Sms sending failed.");
|
System.out.println("Refno : "
+ event.getReferenceId()); |
System.out.println("Return code : "
+ ((SmsSendingFailedEvent) event).getReturnCode()); |
System.out.println("Event generated by
: " + event.getSource().getClass());
|
} else
if
(event instanceof
SmsDeliveryFailedEvent) { |
System.out.println("Sms delivery failed.");
|
System.out.println("Refno : "
+ event.getReferenceId()); |
System.out.println("Error code : "
+ ((SmsDeliveryFailedEvent) event).getErrorCode()); |
System.out.println("Event generated by
: " + event.getSource().getClass());
|
} |
} |
} |