summaryrefslogtreecommitdiff
path: root/Tox.hs
diff options
context:
space:
mode:
Diffstat (limited to 'Tox.hs')
-rw-r--r--Tox.hs38
1 files changed, 38 insertions, 0 deletions
diff --git a/Tox.hs b/Tox.hs
index 8a4fccb5..53aa9a5d 100644
--- a/Tox.hs
+++ b/Tox.hs
@@ -10,6 +10,7 @@
10{-# LANGUAGE TupleSections #-} 10{-# LANGUAGE TupleSections #-}
11module Tox where 11module Tox where
12 12
13import Control.Applicative
13import Control.Arrow 14import Control.Arrow
14import Control.Concurrent (MVar) 15import Control.Concurrent (MVar)
15import Control.Concurrent.STM 16import Control.Concurrent.STM
@@ -58,6 +59,10 @@ import Data.Time.Clock.POSIX (POSIXTime)
58import Global6 59import Global6
59import Data.Ord 60import Data.Ord
60import System.IO 61import System.IO
62import qualified Data.Aeson as JSON
63 ;import Data.Aeson (FromJSON, ToJSON, (.=))
64import Control.Monad
65import Text.Read
61 66
62newtype NodeId = NodeId ByteString 67newtype 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
97instance 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 ]
114instance 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
92instance S.Serialize NodeInfo where 127instance 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
608ping :: ToxClient -> NodeInfo -> IO Bool
609ping = error "todo ping"