src/Tools/Haskell/Haskell.thy
changeset 74128 17e84ae97562
parent 74127 9e97833a0bf0
child 74129 c3794f56a2e2
equal deleted inserted replaced
74127:9e97833a0bf0 74128:17e84ae97562
   239   the, the_default,
   239   the, the_default,
   240 
   240 
   241   fold, fold_rev, single, map_index, get_index, separate,
   241   fold, fold_rev, single, map_index, get_index, separate,
   242 
   242 
   243   StringLike, STRING (..), TEXT (..), BYTES (..),
   243   StringLike, STRING (..), TEXT (..), BYTES (..),
       
   244   show_bytes, show_text,
   244 
   245 
   245   proper_string, quote, space_implode, commas, commas_quote, cat_lines,
   246   proper_string, quote, space_implode, commas, commas_quote, cat_lines,
   246   space_explode, split_lines, trim_line)
   247   space_explode, split_lines, trim_line)
   247 where
   248 where
   248 
   249 
   338 class StringLike a => BYTES a where make_bytes :: a -> Bytes
   339 class StringLike a => BYTES a where make_bytes :: a -> Bytes
   339 instance BYTES String where make_bytes = UTF8.encode
   340 instance BYTES String where make_bytes = UTF8.encode
   340 instance BYTES Text where make_bytes = UTF8.encode
   341 instance BYTES Text where make_bytes = UTF8.encode
   341 instance BYTES Lazy.Text where make_bytes = UTF8.encode . Lazy.toStrict
   342 instance BYTES Lazy.Text where make_bytes = UTF8.encode . Lazy.toStrict
   342 instance BYTES Bytes where make_bytes = id
   343 instance BYTES Bytes where make_bytes = id
       
   344 
       
   345 show_bytes :: Show a => a -> Bytes
       
   346 show_bytes = make_bytes . show
       
   347 
       
   348 show_text :: Show a => a -> Text
       
   349 show_text = make_text . show
   343 
   350 
   344 
   351 
   345 {- strings -}
   352 {- strings -}
   346 
   353 
   347 proper_string :: StringLike a => a -> Maybe a
   354 proper_string :: StringLike a => a -> Maybe a
   490 
   497 
   491 
   498 
   492 {- int -}
   499 {- int -}
   493 
   500 
   494 print_int :: Int -> Bytes
   501 print_int :: Int -> Bytes
   495 print_int = make_bytes . show
   502 print_int = show_bytes
   496 
   503 
   497 parse_int :: Bytes -> Maybe Int
   504 parse_int :: Bytes -> Maybe Int
   498 parse_int = Read.readMaybe . make_string
   505 parse_int = Read.readMaybe . make_string
   499 
   506 
   500 
   507 
  2660 localhost = Socket.tupleToHostAddress (127, 0, 0, 1)
  2667 localhost = Socket.tupleToHostAddress (127, 0, 0, 1)
  2661 
  2668 
  2662 publish_text :: Bytes -> Bytes -> UUID.T -> Bytes
  2669 publish_text :: Bytes -> Bytes -> UUID.T -> Bytes
  2663 publish_text name address password =
  2670 publish_text name address password =
  2664   "server " <> quote name <> " = " <> address <>
  2671   "server " <> quote name <> " = " <> address <>
  2665     " (password " <> quote (make_bytes $ show password) <> ")"
  2672     " (password " <> quote (show_bytes password) <> ")"
  2666 
  2673 
  2667 publish_stdout :: Bytes -> Bytes -> UUID.T -> IO ()
  2674 publish_stdout :: Bytes -> Bytes -> UUID.T -> IO ()
  2668 publish_stdout name address password =
  2675 publish_stdout name address password =
  2669   Char8.putStrLn (Bytes.unmake $ publish_text name address password)
  2676   Char8.putStrLn (Bytes.unmake $ publish_text name address password)
  2670 
  2677 
  2680       server_socket <- Socket.socket Socket.AF_INET Socket.Stream Socket.defaultProtocol
  2687       server_socket <- Socket.socket Socket.AF_INET Socket.Stream Socket.defaultProtocol
  2681       Socket.bind server_socket (Socket.SockAddrInet 0 localhost)
  2688       Socket.bind server_socket (Socket.SockAddrInet 0 localhost)
  2682       Socket.listen server_socket 50
  2689       Socket.listen server_socket 50
  2683 
  2690 
  2684       port <- Socket.socketPort server_socket
  2691       port <- Socket.socketPort server_socket
  2685       let address = localhost_name <> ":" <> make_bytes (show port)
  2692       let address = localhost_name <> ":" <> show_bytes port
  2686       password <- UUID.random
  2693       password <- UUID.random
  2687       publish address password
  2694       publish address password
  2688 
  2695 
  2689       return (server_socket, UUID.print password)
  2696       return (server_socket, UUID.print password)
  2690 
  2697