summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTonyMorris <>2016-06-01 11:30:00 (GMT)
committerhdiff <hdiff@hdiff.luite.com>2016-06-01 11:30:00 (GMT)
commit6e31f183df76c87f0866fa5d2131b24b573fd4d5 (patch)
tree3281d92c2655f4c0f78b63e55c14f9220be44021
version 0.0.30.0.3
-rw-r--r--LICENCE27
-rw-r--r--Setup.lhs44
-rw-r--r--changelog.md3
-rw-r--r--src/Data/Aviation/Stratux/Websockets.hs107
-rw-r--r--stratux-websockets.cabal73
-rw-r--r--test/doctests.hs32
6 files changed, 286 insertions, 0 deletions
diff --git a/LICENCE b/LICENCE
new file mode 100644
index 0000000..d7ebcab
--- /dev/null
+++ b/LICENCE
@@ -0,0 +1,27 @@
+Copyright 2016 Tony Morris
+
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+1. Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+2. Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+3. Neither the name of the author nor the names of his contributors
+ may be used to endorse or promote products derived from this software
+ without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
+FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+SUCH DAMAGE.
diff --git a/Setup.lhs b/Setup.lhs
new file mode 100644
index 0000000..0832aa5
--- /dev/null
+++ b/Setup.lhs
@@ -0,0 +1,44 @@
+#!/usr/bin/env runhaskell
+\begin{code}
+{-# OPTIONS_GHC -Wall #-}
+module Main (main) where
+
+import Data.List ( nub )
+import Data.Version ( showVersion )
+import Distribution.Package ( PackageName(PackageName), PackageId, InstalledPackageId, packageVersion, packageName )
+import Distribution.PackageDescription ( PackageDescription(), TestSuite(..) )
+import Distribution.Simple ( defaultMainWithHooks, UserHooks(..), simpleUserHooks )
+import Distribution.Simple.Utils ( rewriteFile, createDirectoryIfMissingVerbose )
+import Distribution.Simple.BuildPaths ( autogenModulesDir )
+import Distribution.Simple.Setup ( BuildFlags(buildVerbosity), fromFlag )
+import Distribution.Simple.LocalBuildInfo ( withLibLBI, withTestLBI, LocalBuildInfo(), ComponentLocalBuildInfo(componentPackageDeps) )
+import Distribution.Verbosity ( Verbosity )
+import System.FilePath ( (</>) )
+
+main :: IO ()
+main = defaultMainWithHooks simpleUserHooks
+ { buildHook = \pkg lbi hooks flags -> do
+ generateBuildModule (fromFlag (buildVerbosity flags)) pkg lbi
+ buildHook simpleUserHooks pkg lbi hooks flags
+ }
+
+generateBuildModule :: Verbosity -> PackageDescription -> LocalBuildInfo -> IO ()
+generateBuildModule verbosity pkg lbi = do
+ let dir = autogenModulesDir lbi
+ createDirectoryIfMissingVerbose verbosity True dir
+ withLibLBI pkg lbi $ \_ libcfg -> do
+ withTestLBI pkg lbi $ \suite suitecfg -> do
+ rewriteFile (dir </> "Build_" ++ testName suite ++ ".hs") $ unlines
+ [ "module Build_" ++ testName suite ++ " where"
+ , "deps :: [String]"
+ , "deps = " ++ (show $ formatdeps (testDeps libcfg suitecfg))
+ ]
+ where
+ formatdeps = map (formatone . snd)
+ formatone p = case packageName p of
+ PackageName n -> n ++ "-" ++ showVersion (packageVersion p)
+
+testDeps :: ComponentLocalBuildInfo -> ComponentLocalBuildInfo -> [(InstalledPackageId, PackageId)]
+testDeps xs ys = nub $ componentPackageDeps xs ++ componentPackageDeps ys
+
+\end{code}
diff --git a/changelog.md b/changelog.md
new file mode 100644
index 0000000..4a6f922
--- /dev/null
+++ b/changelog.md
@@ -0,0 +1,3 @@
+0.0.1
+
+* Initial.
diff --git a/src/Data/Aviation/Stratux/Websockets.hs b/src/Data/Aviation/Stratux/Websockets.hs
new file mode 100644
index 0000000..4028477
--- /dev/null
+++ b/src/Data/Aviation/Stratux/Websockets.hs
@@ -0,0 +1,107 @@
+module Data.Aviation.Stratux.Websockets(
+ WSConnectionIO
+, decodeWith
+, decode
+, decode'
+, stratuxApp
+, trafficAppWith
+, trafficApp
+, trafficApp'
+) where
+
+import Control.Category(Category((.)))
+import Data.Aeson(FromJSON)
+import qualified Data.Aeson as A(eitherDecode, eitherDecode')
+import Control.Concurrent(forkIO)
+import Control.Monad(Monad((>>=), (>>)), forever, unless)
+import Control.Monad.Trans.Either(EitherT(EitherT))
+import Control.Monad.Trans.Reader(ReaderT(ReaderT))
+import Data.Aviation.Stratux.Types(Traffic)
+import Data.Either(Either)
+import Data.Function(($))
+import Data.Functor(Functor(fmap))
+import Data.Int(Int)
+import Data.String(String)
+import qualified Data.Text as T(null)
+import qualified Data.Text.IO as T(getLine)
+import Network.Socket (withSocketsDo)
+import qualified Network.WebSockets as WS(Connection, WebSocketsData, ClientApp, receiveData, sendClose, sendTextData, runClient)
+import System.IO(IO)
+
+type WSConnectionIO f a =
+ ReaderT WS.Connection (f IO) a
+
+-- | Decode from JSON anything received on the websocket.
+decodeWith ::
+ WS.WebSocketsData a =>
+ (a -> b)
+ -> (IO b -> m c)
+ -> ReaderT WS.Connection m c
+decodeWith d k =
+ ReaderT (k . fmap d . WS.receiveData)
+
+-- | Decode from JSON anything received on the websocket.
+-- Parses immediately, but defers conversion.
+decode ::
+ FromJSON a =>
+ WSConnectionIO (EitherT String) a
+decode =
+ decodeWith A.eitherDecode EitherT
+
+-- | Decode from JSON anything received on the websocket.
+-- Parses and performs conversion immediately.
+decode' ::
+ FromJSON a =>
+ WSConnectionIO (EitherT String) a
+decode' =
+ decodeWith A.eitherDecode' EitherT
+
+-- | A stratux websockets application. Loops receiving data and waits for input
+-- from stdin. An empty line to stdin terminates the program loop.
+stratuxApp ::
+ (WS.WebSocketsData t, WS.WebSocketsData u) =>
+ (t -> IO b) -- ^ The function to run on each receive of data on the websocket.
+ -> u -- ^ The connection close traffic.
+ -> WS.ClientApp ()
+stratuxApp k s c = do
+ _ <- forkIO . forever $ WS.receiveData c >>= k
+ let loop = do line <- T.getLine
+ unless (T.null line) $ WS.sendTextData c line >> loop
+ loop
+ WS.sendClose c s
+
+-- | Decodes JSON to stratux traffic.
+trafficAppWith ::
+ (WS.WebSocketsData t, WS.WebSocketsData u) =>
+ (t -> Either String Traffic) -- ^ Data to traffic.
+ -> String -- ^ host.
+ -> Int -- ^ port.
+ -> (Either String Traffic -> IO b) -- ^ For each traffic message.
+ -> u -- ^ The connection close traffic.
+ -> IO ()
+trafficAppWith q host port k s =
+ withSocketsDo $ WS.runClient host port "/traffic" (stratuxApp (k . q) s)
+
+-- | Decodes JSON to stratux traffic.
+-- Parses immediately, but defers conversion.
+trafficApp ::
+ WS.WebSocketsData u =>
+ String -- ^ host.
+ -> Int -- ^ port.
+ -> (Either String Traffic -> IO b) -- ^ For each traffic message.
+ -> u -- ^ The connection close traffic.
+ -> IO ()
+trafficApp =
+ trafficAppWith A.eitherDecode
+
+-- | Decodes JSON to stratux traffic.
+-- Parses and performs conversion immediately.
+trafficApp' ::
+ WS.WebSocketsData u =>
+ String -- ^ host.
+ -> Int -- ^ port.
+ -> (Either String Traffic -> IO b) -- ^ For each traffic message.
+ -> u -- ^ The connection close traffic.
+ -> IO ()
+trafficApp' =
+ trafficAppWith A.eitherDecode'
diff --git a/stratux-websockets.cabal b/stratux-websockets.cabal
new file mode 100644
index 0000000..d66111b
--- /dev/null
+++ b/stratux-websockets.cabal
@@ -0,0 +1,73 @@
+name: stratux-websockets
+version: 0.0.3
+license: BSD3
+license-file: LICENCE
+author: Tony Morris <ʇǝu˙sıɹɹoɯʇ@ןןǝʞsɐɥ>
+maintainer: Tony Morris
+copyright: Copyright (C) 2016 Tony Morris
+synopsis: A library for using websockets with stratux
+category: Aviation, ADSB, Stratux, JSON, Network
+description: A library for using websockets with stratux <http://stratux.me/>
+homepage: https://github.com/tonymorris/stratux-websockets
+bug-reports: https://github.com/tonymorris/stratux-websockets/issues
+cabal-version: >= 1.10
+build-type: Custom
+extra-source-files: changelog.md
+
+source-repository head
+ type: git
+ location: git@github.com:tonymorris/stratux-websockets.git
+
+flag small_base
+ description: Choose the new, split-up base package.
+
+library
+ default-language:
+ Haskell2010
+
+ build-depends:
+ base < 5 && >= 3
+ , stratux-types >= 0.0.3 && < 0.1
+ , websockets >= 0.9 && < 1.0
+ , transformers >= 0.4 && < 1.0
+ , aeson >= 0.11 && < 1.0
+ , either >= 4.0 && < 5.0
+ , text >= 1.2 && < 2.0
+ , network >= 2.6 && < 3.0
+
+ ghc-options:
+ -Wall
+
+ default-extensions:
+ NoImplicitPrelude
+
+ hs-source-dirs:
+ src
+
+ exposed-modules:
+ Data.Aviation.Stratux.Websockets
+
+test-suite doctests
+ type:
+ exitcode-stdio-1.0
+
+ main-is:
+ doctests.hs
+
+ default-language:
+ Haskell2010
+
+ build-depends:
+ base < 5 && >= 3
+ , doctest >= 0.9.7
+ , filepath >= 1.3
+ , directory >= 1.1
+ , QuickCheck >= 2.0
+ , template-haskell >= 2.8
+
+ ghc-options:
+ -Wall
+ -threaded
+
+ hs-source-dirs:
+ test
diff --git a/test/doctests.hs b/test/doctests.hs
new file mode 100644
index 0000000..6f6b78c
--- /dev/null
+++ b/test/doctests.hs
@@ -0,0 +1,32 @@
+module Main where
+
+import Build_doctests (deps)
+import Control.Applicative
+import Control.Monad
+import Data.List
+import System.Directory
+import System.FilePath
+import Test.DocTest
+
+main ::
+ IO ()
+main =
+ getSources >>= \sources -> doctest $
+ "-isrc"
+ : "-idist/build/autogen"
+ : "-optP-include"
+ : "-optPdist/build/autogen/cabal_macros.h"
+ : "-hide-all-packages"
+ : map ("-package="++) deps ++ sources
+
+getSources :: IO [FilePath]
+getSources = filter (isSuffixOf ".hs") <$> go "src"
+ where
+ go dir = do
+ (dirs, files) <- getFilesAndDirectories dir
+ (files ++) . concat <$> mapM go dirs
+
+getFilesAndDirectories :: FilePath -> IO ([FilePath], [FilePath])
+getFilesAndDirectories dir = do
+ c <- map (dir </>) . filter (`notElem` ["..", "."]) <$> getDirectoryContents dir
+ (,) <$> filterM doesDirectoryExist c <*> filterM doesFileExist c