Git Product home page Git Product logo

swiftssdp's Introduction

SwiftSSDP GitHub license GitHub release Github stable

Update: Unfortunately I do not have time to maintain this package

Simple Service Discovery Protocol (SSDP) session based discovery package for Swift.

Package Management

Installation

GitHub spm GitHub carthage GitHub cocoapod

Using Swift Package Manager

SwiftSSDP is available through Swift Package Manager. To install it, add the following line to your Package.swift dependencies:

.Package(url: "https://github.com/pryomoax/SwiftSSDP.git", majorVersion: 0, minor: 5)

Using Carthage

SwiftSSDP is available through Carthage. To install it, add the following line to your Cartfile:

# SwiftSSDP
github "pryomoax/SwiftSSDP.git" ~> 0.5

Using CocoaPods

SwiftSSDP is currently not supported by CocoaPods (coming soon)

Usage

SSDP can be used for many things, discovering devices or services. Sonos uses SSDP for device discovery and using the urn:schemas-upnp-org:device:ZonePlayer:1 search target (ST) devices can be discovered and inspected.

Below is a simple class to start and stop Sonos device discovery. It uses a 10 second timeout, which will automatically close the discovery session session if not closed explictly.

SSDP makes use of UDP, which is an unreliable transport, and even less reliable over WiFi. SwiftSSDP automatically repeats MSEARCH broadcasts to ensure discovery of all devices. SwiftSSDP gradually backs off the interval between MSEARCH broadcasts are sent from 1/second to 1/minute. Discovery should be short lived as not to flood the network with broadcasts. Without a timeout the session should be closed explictly.

Timed Sessions

public class DeviceDiscovery {

	private let discovery: SSDPDiscovery = SSDPDiscovery.defaultDiscovery
	fileprivate var session: SSDPDiscoverySession?

	public func searchForDevices() {
		// Create the request for Sonos ZonePlayer devices
		let zonePlayerTarget = SSDPSearchTarget.deviceType(schema: SSDPSearchTarget.upnpOrgSchema, deviceType: "ZonePlayer", version: 1)
		let request = SSDPMSearchRequest(delegate: self, searchTarget: zonePlayerTarget)
    
		// Start a discovery session for the request and timeout after 10 seconds of searching.
		self.session = try! discovery.startDiscovery(request: request, timeout: 10.0)
	}
	
	public func stopSearching() {
		self.session?.close()
		self.session = nil
	}
	
}

To handle the discovery implement the SSDPDiscoveryDelegate protocol, and use when initializing a SSDPMSearchReqest

extension DeviceDiscovery: SSDPDiscoveryDelegate {
    
	public func discoveredDevice(response: SSDPMSearchResponse, session: SSDPDiscoverySession) {
       print("Found device \(response)\n")
   }
    
   public func discoveredService(response: SSDPMSearchResponse, session: SSDPDiscoverySession) {
   }
    
   public func closedSession(_ session: SSDPDiscoverySession) {
       print("Session closed\n")
   }

}

Logging

SwiftSSDP uses SwiftAbstractLogger for all logging. Logging can be independently configured for SwiftSSDP using the log category "SSDP". For convenience this is accessible via the loggerDiscoveryCategory constant.

// Attach a default (basic) console logger implementation to Logger
Logger.attach(BasicConsoleLogger.logger)

// Enable debug logging only for SSDPSwift
Logger.configureLevel(category: loggerDiscoveryCategory, level: .Debug)

Package Information

Requirements

  • Xcode 8
  • iOS 10.0+

Author

Paul Bates, [email protected]

License

SwiftSSDP is available under the MIT license. See the LICENSE file for more

swiftssdp's People

Contributors

happycodelucky avatar tom-groves avatar jhurliman avatar

Watchers

Dustin avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.