Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Swift Outside the Box

Swift Outside the Box

Yusuke Ito

March 03, 2017
Tweet

More Decks by Yusuke Ito

Other Decks in Programming

Transcript

  1. S W I F T O U T S I

    D E T H E B O X Y U S U K E I T O S H I R O YA G I C O R P O R AT I O N T RY ! S W I F T T O K Y O 2 0 1 7 Twitter @novi_ Github @novi
  2. T O P I C S • Server Side Swift

    • 2 Real Projects • 1. • 2. • Porting Library to Swift
  3. • Swift is open-sourced since Dec 2015 • Foundation, XCTest

    and Dispatch • Package Manager • Works on macOS and Linux(Ubuntu) • Platform: x86_64, ARM(unofficial) • Oct 2016: Server APIs Work Group launched
  4. • Swift for Non-Apple Platform • for Web servers, API

    servers • for Microservices • for Daemons, Utilities, Tools • Runs on… • Linux (IBM, Amazon, Google cloud) • Linux board computers (e.g. Raspberry Pi)
  5. W H Y S W I F T ? •

    Traditionally Use Node.js, Python, Ruby, etc… for Server-Side programming • Swift has… • Types, Protocol, Value Associated Enumeration, etc… for Better, Safer Programming • Good Performance • Alternatives for Scripting Programming Languages
  6. B A C K E N D S E R

    V I C E 
 W I T H S W I F T R E A L P R O J E C T 1 , O U R C O M PA N Y P R O J E C T
  7. S W I F T B A S E D

    W E B C R AW L E R • Backend service of our products • RSS/HTML Crawling / Web scraping • Crawl specified Web pages, Parse the HTML, Save to the database as a Search bot • Extract body text excluding Navigation or submenu on the HTML
  8. a Web Page URL = https://swift.org/blog/swift-4-0-release-process/ N AV I G

    AT I O N E L E M E N T S METADATA ELEMENTS BODY ELEMENTS
  9. Settings Contents Images Save settings, Set HTML extraction rules (XPaths)

    Get HTML and Text result of crawling Fetch a Web HTML Parse the HTML/XML Save to the Database CRAWLER (Process) D ATA B A S E ( M Y S Q L / S 3 ) API SERVER (In-house use) Based on gRPC protcol
  10. W H AT W E N E E D ?

    • HTTP(S)Client • XML/HTML Parsing, XPath(CSS Selector) traversing • MySQL Library • S3 Client • gRPC, Protocol Buffers Library • Japanese Charset converter (for Shift-JIS, EUC_JP charset)
  11. S W I F T A N D C L

    A N G U A G E • Swift is LLVM based • Link with C based Library • Export C interface to Swift transparently • Swift Package Manager supports C compile (.c and .h) All C Libraries are Available on Swift!!
  12. W H AT W E N E E D ?

    ( A G A I N ) • HTTPClient, AWS S3Client • libcurl (C based) • XML/HTML Parsing, XPath(CSS Selector) traversal • libxml2 (C based) • MySQL Library • C based mysqlconnector • Japanese Charset converter (Shift-JIS, EUC_JP) • nkf(Network Kanji Filter) (C based)
  13. O U R C U R R E N T

    S TAT U S • Code base(including Tests) • Crawler:~10,000 lines • In-house library: ~9,000 lines • Unit Tests is works well with Xcode • Running on macOS fine, ~20 days OK! • Running on our Development environment (Docker, Linux), 7 days OK!
  14. O U R N E X T S T E

    P … • Deploy to Production environment (AWS) • Mesure performance, stability
  15. W O R K I N G W I T

    H 
 S E N S O R S P R O J E C T 2 : P E R S O N A L P R O J E C T
  16. • Swift for ARM and Raspberry Pi (Linux board computer)

    • Control many Devices via GPIO and I2C bus • Various Sensors, Lights, LCD, … • Combine with Swift libraries • Of Course, Written in Swift all !! Swift for ARM: https://github.com/iachievedit/package-swift
  17. Linux board Air Pressure Sensor S E N S O

    R R A S P B E R RY P I S E R V E R S I D E S W I F T Slack API S E R V E R HTTPS POST hPa ℉ (℃) I2C I N T E R N E T WiFi
  18. • Controlling I2C bus • #include <linux/i2c-dev.h> • I2C bus

    file: /dev/i2c-0… • Use “fopen”, “ioctl” system call • github.com/novi/i2c-swift-example
  19. sensor data MesurementResult(temperature: 229, pressure: 101507) // get current air

    pressure and temperature let device = try getCurrentI2CDevice() let sensor = BMP180(device: device) try sensor.reset() try sensor.calibrate() let result = try sensor.mesure(oversampling: .oss3) print("sensor data", result)
  20. // build message let temp = Float(result.temperature) / 10 let

    fahrenheit = 1.8 * Float(result.temperature) / 10 + 32 let press = Float(result.pressure) / 100 // in hpa let tempStr = String(format: "%.0f°F(%.1f°C)", fahrenheit, temp) let pressStr = String(format: "%.2fhPa", press) let message = "\(tempStr) \(pressStr) \(formatDate(Date()))" // post to slack slack.sendMessage(channel: sendChannel, text: message, success: { (a, b) in
  21. P O R T I N G L I B

    R A RY T O S W I F T
  22. 1. Find C based Library • Wrap with Swift ➜

    • Make it more Swifty ➜ • e.g.) libcurl, libxml, mysqlconnector 2. Find similar Libraries in Node.js, Python, Ruby… • Port other Language’s Library back to Swift ➜ • Make it more Swifty ➜ • e.g.) NKF Write your Swift program with them
  23. M Y L I B R A R I E

    S F O R S E R V E R S I D E S W I F T • MySQL: novi/mysql-swift • NKF: novi/nkf-swift • I2C: novi/i2c-swift
  24. S TA RT I N G S E R V

    E R S I D E S W I F T W I T H … • Batch programs • API Servers • In-house deployment tools • Command-line utilities
  25. • Start with SPM(Swift Package Manager) • Similar to Carthage

    and Cocoapods $ swift package init —type=executable hello world $ swift package init —type=library $ swift package generate-xcodeproj
  26. S C R I P T I N G S

    W I F T • $ touch test.swift && chmod a+x test.swift • $ ./test.swift
 apple.com #!/usr/bin/env swift import Foundation let url = URLComponents(string: "https://apple.com")! print(url.host!)
  27. C O N C L U S I O N

    • Swift is great programming language • Safety, Efficiently • Open-sourced • Works well with C based Library • Works well on Various Platforms