No gateways are defined SMSlib - java

I am trying to learn to use SMSlib to send sms using ZTE MF631 usb modem. Here is my code:
Service.getInstance().startService();
SerialModemGateway gateway = new SerialModemGateway("modem.com5","COM5", 115200, "ZTE", "MF631");
gateway.setInbound(true);
gateway.setOutbound(true);
gateway.setSimPin("0000");
gateway.setSmscNumber("+9779800009000");
try {
Service.getInstance().addGateway(gateway);
OutboundMessage outboundMsg = new OutboundMessage("+9779843669921", "Hello there!");
Service.getInstance().sendMessage(outboundMsg);
} finally {
gateway.stopGateway();
Service.getInstance().removeGateway(gateway);
Service.getInstance().stopService();
}
When I run my code I get the following error:
org.smslib.SMSLibException: No gateways are defined.
at org.smslib.Service.startService_Internal(Service.java:295)
at org.smslib.Service.startService(Service.java:229)
at org.smslib.Service.startService(Service.java:196)
at com.mail.utility.SendSmsUtility.sendSMS(SendSmsUtility.java:10)
at com.mail.action.SendSmsAction.sendSms(SendSmsAction.java:11)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

I think you are missing something like this before you start service:
Service.getInstance().addGateway(gateway);
Here is SMSlib example.

Related

NumberFormatException in connecting Azure Service Bus using Managed Identity

I am trying to connect to Azure Service Bus from my webjob using Managed Identities. But unfortunately, I am getting the following error
com.google.gson.JsonSyntaxException: java.lang.NumberFormatException: For input string: "10/10/2020 6:50:38 AM +00:00"
I have enabled Identity and assigned Contributor role for connecting to Service bus. Also, in the code, I have used ClientSettings to create a connection with Service bus.
ClientFactory.createMessageReceiverFromEntityPathAsync(namespace, queueName,new ClientSettings(TokenProvider.createManagedServiceIdentityTokenProvider()), ReceiveMode.RECEIVEANDDELETE).get();
Any idea how to resolve this exception, as it is thrown from built-in class ManagedServiceIdentityTokenProvider. Any help would be really helpful.
Thanks in advance.
EDIT -
I updated the service bus package to 3.2.0. Now I can't see NumberFormatException but I am getting java.lang.RuntimeException: java.net.SocketException: Permission denied: connect even though I have provided the 'Owner' role to the webjob.
If you want to connect Azure Service bus with Azure Managed Identity, we need to assign some special role(Azure Service Bus Data Owner, Azure Service Bus Data Sender and Azure Service Bus Data Receiver ) to the MSI. For more details, please refer to here.
For example
Assing role to the MSI
Code
public static void main( String[] args )
{
IMessageReceiver receiver = null;
try {
receiver= ClientFactory.createMessageReceiverFromEntityPathAsync("bowman1012",
"myqueue",
new ClientSettings(TokenProvider.createManagedIdentityTokenProvider()),
ReceiveMode.PEEKLOCK).get();
IMessage message = receiver.receiveAsync().get();
String content = new String(message.getBody(), UTF_8);
System.out.println(content);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}finally {
Boolean f= receiver.closeAsync().complete(null);
System.out.println(f);
}
}

How do I create WebSockets with Play framework 2.6?

I am trying to do the following with Play Framework 2.6:
The browser targets the server and a WebSocket is created
Later on (after some other request is performed), the servers sends a message to the browser via the WebSocket previously created
Point 1 can be easily done with a route:
public WebSocket socket() {
return WebSocket.Text.accept(request -> {
// Log events to the console
Sink<String, ?> in = Sink.foreach(System.out::println);
// Send a single 'Hello!' message and then leave the socket open
Source<String, ?> out = Source.single("Hello!").concat(Source.maybe());
return Flow.fromSinkAndSource(in, out);
});
}
and the WebSocket can be saved server side.
But then how can I send data via the WebSocket? (triggered server side)
This was easy to do with 2.5 but the documentation is not very helpful for Play 2.6.
I've managed to implement websocket with help of Akka actors. At first step define actor that will handle messages
public class WebSocketActor extends AbstractActor {
private final ActorRef out;
#Inject
public WebSocketActor(ActorRef out) {
this.out = out;
}
#Override
public Receive createReceive() {
return receiveBuilder()
.match(String.class, message ->
out.tell("Sending message at " + LocalDateTime.now().toString(), self())
)
.build();
}
public static Props props(final ActorRef out) {
return Props.create(WebSocketActor.class, out);
}
}
This actor will be created per client. ActorRef out will send message to connected client. In this example response is send to client on each string message passed to WebSocketActor.
Now define API endpoint to open access to websocket for clients. Define ActorFlow that will create new instance of actor on new connection
public WebSocket ws() {
return WebSocket.Text.accept(request -> ActorFlow.actorRef((out) -> WebSocketActor.props(out), actorSystem, materializer));
}
According to source code ActorFlow creates actors with flowActor name. So to send message to websockets somewhere in the code we can find actors by their path. This will broadcast message to all connected clients
actorSystem.actorSelection("/user/*/flowActor").tell("Hello", ActorRef.noSender());
Unfortunately I didn't find easy way to change ActorFlow default name but maybe this answer may help you play-scala-akka-websockets-change-actor-path.
Also you can check play-java-websocket-example project from playframework examples.

How to stop streaming in the publish method of Wowza server

I need to stop publishing the stream in the publish() method and send the client a message such as "This name is incorrect, choose another."
I've looked at sendClientOnErrorStatus() but haven't found any examples showing how to handle it on the client side.
Also, can anyone explain what the sendResult(..) method does?
sendClientOnErrorStatus() and sendResult() only work in a Flash context.
sendClientOnErrorStatus()
On the Wowza server you can override the publish and releaseStream (for streams being published) methods and do something like:
sendClientOnStatusError(client, "NetStream.Publish.Denied", "Stream name is invalid: " + streamName);
On a Flash client using ActionScript you would add a NetStatusEvent listener:
nc = new NetConnection();
nc.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
private function netStatusHandler(event:NetStatusEvent):void {
switch (event.info.code) {
// do something with the event.info
}
}
sendResult()
This is used to send the result for a NetConnection.call which calls a custom method on the Wowza server.
Example:
Wowza server:
public void someMethod(IClient client, RequestFunction function,
AMFDataList params) {
{
sendResult(client, params, "response");
}
Client:
nc = new NetConnection();
nc.call("someMethod",new Responder(function(data:String):void {
trace(data);
})
);
Source: AS3 Reference

org.smslib port in use exception

I am trying to create web application to send sms by gsm modem in JSP first I put destination mobile number and sms text in url and get by request.getparameter and first message sent with no problem but when send a message again by referenshing the same page i get this exception:
org.smslib.GatewayException: Comm library exception: java.lang.RuntimeException: gnu.io.PortInUseException: org.smslib
at org.smslib.modem.SerialModemDriver.connectPort(SerialModemDriver.java:102)
at org.smslib.modem.AModemDriver.connect(AModemDriver.java:114)
at org.smslib.modem.ModemGateway.startGateway(ModemGateway.java:189)
at org.smslib.Service$1Starter.run(Service.java:276)
I tried to stop gateway and stop service but no hope
My code:
public boolean sendMessage(String strMobileNo,String strSMSText)
{
try
{
OutboundMessage outboundMessage=new OutboundMessage();
SMS message=new SMS();
SerialModemGateway gateway = new SerialModemGateway("modem.com1", "COM12", 9600, "Huawie", "EF200");
gateway.setInbound(true);
gateway.setOutbound(true);
gateway.setSimPin("0000");
gateway.setSmscNumber("+9647701144010");
Service.getInstance().setOutboundMessageNotification(message);
Service.getInstance().addGateway(gateway);
Service.getInstance().startService();
outboundMessage.setText(strSMSText);
outboundMessage.setRecipient(strMobileNo);
outboundMessage.setEncoding(Message.MessageEncodings.ENCUCS2);
//outboundMessage.setDeliveryDelay(5000);
Service.getInstance().sendMessage(outboundMessage);
System.out.println(outboundMessage);
gateway.stopGateway();
Service.getInstance().stopService();
Thread.sleep(10000);
return true;
}
catch (Exception e)
{
e.printStackTrace();
return false;
}
}
The problem in your code is every time a request is made a new SerialModemGateway is created, which should not be done.
Try to have SerialModemGateway gateway = new SerialModemGateway("modem.com1", "COM12", 9600, "Huawie", "EF200"); statement only called once your application run, instead of every time a request is made.
try out this Service.getInstance().stopService() in last of your code its working and you can also terminate your program before running it again
I solved this problem with
Service.getInstance().removeGateway(gateway);

Smtp Authentication

I am writing a class to connect to a SMTP server over SSL and send a mail. The smtp server i am using (yahoo) requires authentication. Can someone tell me how the authentication takes place as to which commands i should use to send my user credentials?
Note: I know about the JavaMail API. I just want a simple class to send mail without outside libraries.
Internet RFC 821 covers the basics of the SMTP protocol, and RFC 2554 covers the authentication extensions. You'll need many of them to get a functional SMTP client up.
But, really, it's much simpler to just use JavaMail (unless this is a homework assignment, in which case, I'm guessing that would be cheating.)
You can do it by following in c#
class smtp
{
SmtpClient client;
MailMessage mm;
void send()
{
mm.send();
}
void smtp_configure()
{
client.Credentials = new NetworkCredential(username, password);
client.Port = smtp_port;
client.Host = smtp_host;
client.EnableSsl = true;
}
message_configure()
{
mm = new MailMessage(From, To);
mm.Body = MgsText;
mm.BodyEncoding = Encoding.UTF8;
mm.Subject = Subject;
}
Main()
{
smtp_configure();
message_configure();
send();
}
}

Categories

Resources