Posts Tagged ‘SSDP’
SSDP Service using Java/OSGi [Part 3, ROM]
In the last part I talked about how I implemented the SSDP code on the IOS app, to make my device discoverable – it has to be running the “SSDP Advertiser” – or a server that is listening to discovery messages.
The SSDP service runs as a thread when the OSGi bundle is installed and started – “AdvertiseServiceSSDP()”. [The call before that – SocksServer is the class that implements the network interface between the IOS app & the Plug Computer as explained in the project overview.
public void start(BundleContext bc) throws Exception {
this.bc = bc;
nServer = new SocksServer(bc);
advtROMService = new AdvertiseServiceSSDP(bc);
}
Here is the SSDP code:
public void run() {
String msg = "LISTEN-FOR=THIS MESSAGE";
String chkResponse = "RESPOND-WITH-THIS-MESSAGE";
try {
int port = 1900;
InetAddress listenSSDP = InetAddress.getByName("239.255.255.250");
MulticastSocket ms = new MulticastSocket(port); // Create socket
ms.joinGroup(listenSSDP);
System.out.println("\n Rcvr started, Ready to Receive... \n");
boolean match = false;
do {
byte[] response = new byte[256];
DatagramPacket rspPkt = new DatagramPacket(response, response.length);
ms.receive(rspPkt);
//System.out.println("\n Done Receiving!\n");
String getResponse;
getResponse = new String(rspPkt.getData());
match = getResponse.regionMatches(0, msg, 0, msg.length());
if (match)
{
System.out.println("Controller found\n");
// Send Response
InetAddress IPAddress = rspPkt.getAddress();
byte[] sendData = new byte[1024];
String sendResponse = chkResponse;
sendResponse += IPAddress.getHostName();
sendResponse += "/:portAddr";
System.out.println(sendResponse);
sendData = sendResponse.getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 1900);
ms.send(sendPacket);
System.out.println("Response Sent");
match = false;
}
} while(match==false);
} catch(Exception ignore) {
System.out.println(ignore.getMessage());
}
}
The above code when running on the Plug Computer(s) allows the IOS app to discover them. Of course the beauty is that this will work for all devices that support OSGi/JVM and have multicast networking support.
SSDP Service/Device Discovery [Part 2, ROM]
To begin my project – a Remote OSGi Manager (ROM) as described in my first part here, the first task I picked up was to try developing a very rudimentary app that would discover the RTCOA Thermostat using the SSDP protocol. As you can see from the screenshot – it is really rudimentary user interface but my focus was to get my MVC right, and more importantly get the network interface going for the project. [BTW if you observe carefully – you can see the “Marvell” code that has been used in the RTCOA Thermostat].
After much searching and reviewing samples, I decided to go with the Cocoa Async Sockets, and I have ended up using them for all of the network interface in my iOS/iPad app. I got started with the AsyncUdpSocket to leverage it for running SSDP based discovery of the RTCOA Thermostat. The screenshot above was the start. I took the SSDP objects (serviceSSDP.m, serviceSSDP.h) and moved them to the bigger project. Also as a first step – the SSDP discovery code stopped after discovering the first controller. Given that in my project I was going to support multiple controllers, I ended up modifying the code to take into account that I should be able to discover multiple controllers (or devices) and then add them to a NSMutableArray.
//// serviceSSDP.h// SimpleSSDPDiscovery//// Created by Ashu Joshi on 3/1/12.// Copyright (c) 2012 Movinture, LLC. All rights reserved.//
#import <Foundation/Foundation.h>#import “AsyncUdpSocket.h”#import “connectedLifeController.h”
@interface serviceSSDP : NSObject
// This is the string that is Multicast to Discover the Controller@property (strong, nonatomic) NSString *discoverControllerString;// The Controller would respond with the string below upon discovery being received@property (strong, nonatomic) NSString *responseStringFromController;// The list of discovered controllers@property (strong, nonatomic) NSMutableArray *controllerList;
@property (strong, nonatomic) connectedLifeController *currentController;
@property (strong, nonatomic) AsyncUdpSocket *ssdpSocket;
– (BOOL)startControllerDiscoveryProcess;
– (BOOL)startControllerDiscoveryProcess:(NSMutableArray *)listOfControllers;@end
Once I got the Thermostat to discover using SSDP discovery code using iOS/iPad, I turned my attention to the Plug Computer. I created the framework for my bundle (OSGi terminology for an application). And the first thing I did was to implement code built out code in Java to run a SSDP “advertise” service on the Plug Computer. Before even migrating or trying the code on the Plug Computer I tried the code on my PC/Mac using Eclipse, once it was working I migrated it as a bundle/service in the OSGi framework on the Plug Computer. This code uses threading so that it is always running on the Plug Computer.
In the next part I will give an overview of the Java/OSGi code and the network interface implemented….