diff options
Diffstat (limited to 'Tox.hs')
-rw-r--r-- | Tox.hs | 38 |
1 files changed, 38 insertions, 0 deletions
@@ -10,6 +10,7 @@ | |||
10 | {-# LANGUAGE TupleSections #-} | 10 | {-# LANGUAGE TupleSections #-} |
11 | module Tox where | 11 | module Tox where |
12 | 12 | ||
13 | import Control.Applicative | ||
13 | import Control.Arrow | 14 | import Control.Arrow |
14 | import Control.Concurrent (MVar) | 15 | import Control.Concurrent (MVar) |
15 | import Control.Concurrent.STM | 16 | import Control.Concurrent.STM |
@@ -58,6 +59,10 @@ import Data.Time.Clock.POSIX (POSIXTime) | |||
58 | import Global6 | 59 | import Global6 |
59 | import Data.Ord | 60 | import Data.Ord |
60 | import System.IO | 61 | import System.IO |
62 | import qualified Data.Aeson as JSON | ||
63 | ;import Data.Aeson (FromJSON, ToJSON, (.=)) | ||
64 | import Control.Monad | ||
65 | import Text.Read | ||
61 | 66 | ||
62 | newtype NodeId = NodeId ByteString | 67 | newtype NodeId = NodeId ByteString |
63 | deriving (Eq,Ord,ByteArrayAccess, Bits, Hashable) | 68 | deriving (Eq,Ord,ByteArrayAccess, Bits, Hashable) |
@@ -89,6 +94,36 @@ data NodeInfo = NodeInfo | |||
89 | } | 94 | } |
90 | deriving (Eq,Ord) | 95 | deriving (Eq,Ord) |
91 | 96 | ||
97 | instance ToJSON NodeInfo where | ||
98 | toJSON (NodeInfo nid (IPv4 ip) port) | ||
99 | = JSON.object [ "public_key" .= show nid | ||
100 | , "ipv4" .= show ip | ||
101 | , "port" .= (fromIntegral port :: Int) | ||
102 | ] | ||
103 | toJSON (NodeInfo nid (IPv6 ip6) port) | ||
104 | | Just ip <- un4map ip6 | ||
105 | = JSON.object [ "public_key" .= show nid | ||
106 | , "ipv4" .= show ip | ||
107 | , "port" .= (fromIntegral port :: Int) | ||
108 | ] | ||
109 | | otherwise | ||
110 | = JSON.object [ "node-id" .= show nid | ||
111 | , "ipv6" .= show ip6 | ||
112 | , "port" .= (fromIntegral port :: Int) | ||
113 | ] | ||
114 | instance FromJSON NodeInfo where | ||
115 | parseJSON (JSON.Object v) = do | ||
116 | nidstr <- v JSON..: "public_key" | ||
117 | ip6str <- v JSON..:? "ipv6" | ||
118 | ip4str <- v JSON..:? "ipv4" | ||
119 | portnum <- v JSON..: "port" | ||
120 | ip <- maybe empty (return . IPv6) (ip6str >>= readMaybe) | ||
121 | <|> maybe empty (return . IPv4) (ip4str >>= readMaybe) | ||
122 | let (bs,_) = Base16.decode (C8.pack nidstr) | ||
123 | guard (B.length bs == 32) | ||
124 | return $ NodeInfo (NodeId bs) ip (fromIntegral (portnum :: Word16)) | ||
125 | |||
126 | |||
92 | instance S.Serialize NodeInfo where | 127 | instance S.Serialize NodeInfo where |
93 | get = do | 128 | get = do |
94 | nid <- S.get | 129 | nid <- S.get |
@@ -569,3 +604,6 @@ gen g = let (bs, g') = randomBytesGenerate 24 g | |||
569 | Right w = S.runGet S.getWord64be ws | 604 | Right w = S.runGet S.getWord64be ws |
570 | in ( TransactionId (Nonce8 w) (Nonce24 bs), g'' ) | 605 | in ( TransactionId (Nonce8 w) (Nonce24 bs), g'' ) |
571 | 606 | ||
607 | |||
608 | ping :: ToxClient -> NodeInfo -> IO Bool | ||
609 | ping = error "todo ping" | ||