Skip to content

Commit 21cf023

Browse files
Add module Database.MongoDB.Internal.Network.
Add flag imitating bson package PR for network changes. Add stack files for compilation checking. Both ghc86 builds work. Still need to fix ghc84 and under builds with older network code.
1 parent 5bb7751 commit 21cf023

10 files changed

+72
-17
lines changed

Database/MongoDB/Connection.hs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import Control.Applicative ((<$>))
3030
#endif
3131

3232
import Control.Monad (forM_)
33-
import Network (HostName, PortID(..), connectTo)
3433
import System.IO.Unsafe (unsafePerformIO)
3534
import System.Timeout (timeout)
3635
import Text.ParserCombinators.Parsec (parse, many1, letter, digit, char, eof,
@@ -48,6 +47,7 @@ import Data.Text (Text)
4847
import qualified Data.Bson as B
4948
import qualified Data.Text as T
5049

50+
import Database.MongoDB.Internal.Network (HostName, PortID(..), connectTo)
5151
import Database.MongoDB.Internal.Protocol (Pipe, newPipe, close, isClosed)
5252
import Database.MongoDB.Internal.Util (untilSuccess, liftIOE,
5353
updateAssocs, shuffle, mergesortM)
@@ -79,11 +79,7 @@ showHostPort :: Host -> String
7979
-- TODO: Distinguish Service and UnixSocket port
8080
showHostPort (Host hostname port) = hostname ++ ":" ++ portname where
8181
portname = case port of
82-
Service s -> s
8382
PortNumber p -> show p
84-
#if !defined(mingw32_HOST_OS) && !defined(cygwin32_HOST_OS) && !defined(_WIN32)
85-
UnixSocket s -> s
86-
#endif
8783

8884
readHostPortM :: (Monad m) => String -> m Host
8985
-- ^ Read string \"hostname:port\" as @Host hosthame (PortNumber port)@ or \"hostname\" as @host hostname@ (default port). Fail if string does not match either syntax.

Database/MongoDB/Internal/Network.hs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
-- | Compatibility layer for network package, including newtype 'PortID'
2+
{-# LANGUAGE CPP, PackageImports #-}
3+
4+
module Database.MongoDB.Internal.Network (PortID(..), N.HostName, connectTo) where
5+
6+
import Control.Exception (bracketOnError)
7+
import Network.BSD as BSD
8+
import System.IO (Handle, IOMode(ReadWriteMode))
9+
10+
#if !MIN_VERSION_network(2, 8, 0)
11+
import qualified Network as N
12+
#else
13+
import qualified Network.Socket as N
14+
#endif
15+
16+
newtype PortID = PortNumber N.PortNumber deriving (Show, Eq, Ord)
17+
18+
-- Taken from network 2.8's connectTo
19+
-- https://github.com/haskell/network/blob/e73f0b96c9da924fe83f3c73488f7e69f712755f/Network.hs#L120-L129
20+
connectTo :: N.HostName -- Hostname
21+
-> PortID -- Port Identifier
22+
-> IO Handle -- Connected Socket
23+
connectTo hostname (PortNumber port) = do
24+
proto <- BSD.getProtocolNumber "tcp"
25+
bracketOnError
26+
(N.socket N.AF_INET N.Stream proto)
27+
(N.close) -- only done if there's an error
28+
(\sock -> do
29+
he <- BSD.getHostByName hostname
30+
N.connect sock (N.SockAddrInet port (hostAddress he))
31+
N.socketToHandle sock ReadWriteMode
32+
)

Database/MongoDB/Internal/Util.hs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
-- | Miscellaneous general functions and Show, Eq, and Ord instances for PortID
1+
-- | Miscellaneous general functions
22

33
{-# LANGUAGE FlexibleInstances, UndecidableInstances, StandaloneDeriving #-}
44
{-# LANGUAGE CPP #-}
5-
-- PortID instances
6-
{-# OPTIONS_GHC -fno-warn-orphans #-}
75

86
module Database.MongoDB.Internal.Util where
97

@@ -14,7 +12,6 @@ import Control.Exception (handle, throwIO, Exception)
1412
import Control.Monad (liftM, liftM2)
1513
import Data.Bits (Bits, (.|.))
1614
import Data.Word (Word8)
17-
import Network (PortID(..))
1815
import Numeric (showHex)
1916
import System.Random (newStdGen)
2017
import System.Random.Shuffle (shuffle')
@@ -28,12 +25,6 @@ import Data.Text (Text)
2825

2926
import qualified Data.Text as T
3027

31-
#if !MIN_VERSION_network(2, 4, 1)
32-
deriving instance Show PortID
33-
deriving instance Eq PortID
34-
#endif
35-
deriving instance Ord PortID
36-
3728
-- | A monadic sort implementation derived from the non-monadic one in ghc's Prelude
3829
mergesortM :: Monad m => (a -> a -> m Ordering) -> [a] -> m [a]
3930
mergesortM cmp = mergesortM' cmp . map wrap

Database/MongoDB/Transport/Tls.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ import Database.MongoDB.Internal.Protocol (newPipeWith)
3939
import Database.MongoDB.Transport (Transport(Transport))
4040
import qualified Database.MongoDB.Transport as T
4141
import System.IO.Error (mkIOError, eofErrorType)
42-
import Network (connectTo, HostName, PortID)
42+
import Database.MongoDB.Internal.Network (connectTo, HostName, PortID)
4343
import qualified Network.TLS as TLS
4444
import qualified Network.TLS.Extra.Cipher as TLS
4545
import Database.MongoDB.Query (access, slaveOk, retrieveServerData)

mongoDB.cabal

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ Build-type: Simple
1919
Stability: alpha
2020
Extra-Source-Files: CHANGELOG.md
2121

22+
-- Imitated from https://github.com/mongodb-haskell/bson/pull/18
23+
Flag _old-network
24+
description: Control whether to use <http://hackage.haskell.org/package/network-bsd network-bsd>
25+
default: False
26+
manual: False
27+
2228
Library
2329
GHC-options: -Wall
2430
default-language: Haskell2010
@@ -54,14 +60,22 @@ Library
5460
, base64-bytestring >= 1.0.0.1
5561
, nonce >= 1.0.5
5662

63+
if flag(_old-network)
64+
-- "Network.BSD" is only available in network < 2.9
65+
build-depends: network < 2.9
66+
else
67+
-- "Network.BSD" has been moved into its own package `network-bsd`
68+
build-depends: network-bsd >= 2.7 && < 2.9
69+
5770
Exposed-modules: Database.MongoDB
5871
Database.MongoDB.Admin
5972
Database.MongoDB.Connection
6073
Database.MongoDB.GridFS
6174
Database.MongoDB.Query
6275
Database.MongoDB.Transport
6376
Database.MongoDB.Transport.Tls
64-
Other-modules: Database.MongoDB.Internal.Protocol
77+
Other-modules: Database.MongoDB.Internal.Network
78+
Database.MongoDB.Internal.Protocol
6579
Database.MongoDB.Internal.Util
6680

6781
Source-repository head

stack-ghc80.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
resolver: lts-9.21
2+
flags:
3+
mongoDB:
4+
_old-network: true

stack-ghc82.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
resolver: lts-11.22
2+
flags:
3+
mongoDB:
4+
_old-network: true

stack-ghc84.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
resolver: lts-12.26
2+
flags:
3+
mongoDB:
4+
_old-network: true

stack-ghc86-network3.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
resolver: lts-13.23
2+
extra-deps:
3+
- git: git@github.com:hvr/bson.git # https://github.com/mongodb-haskell/bson/pull/18
4+
commit: 2fc8d04120c0758201762b8e22254aeb6d574f41
5+
- network-bsd-2.8.1.0
6+
- network-3.1.0.0

stack-ghc86.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
resolver: lts-13.23
2+
flags:
3+
mongoDB:
4+
_old-network: true

0 commit comments

Comments
 (0)