-
Notifications
You must be signed in to change notification settings - Fork 0
/
Client.hs
76 lines (60 loc) · 2.06 KB
/
Client.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import Network
import System.IO (hClose, hSetBuffering, hGetLine, hPutStrLn, BufferMode(..), Handle)
import Control.Concurrent (forkIO)
import System.Environment (getArgs)
type Port = Int
type SelfPort = Int
type Host = String
commandList = ["1. Register", "2. Get Peers List", "3. Send data to peer"]
connectToServer :: Host -> Port -> SelfPort-> IO ()
connectToServer host port selfPort= do
sHandle <- connectTo host (PortNumber (fromIntegral port))
hSetBuffering sHandle NoBuffering
communicate sHandle selfPort
hClose sHandle
communicate :: Handle -> SelfPort -> IO ()
communicate handle selfPort = do
mapM_ putStrLn commandList
cmd <- getLine
commandExecutor (read $ cmd :: Int) handle selfPort
communicate handle selfPort
commandExecutor cmd handle selfPort = do
case cmd of
1 -> register handle selfPort
2 -> getPeersList handle
3 -> sendDataToPeer
_ -> putStrLn "Unknown command"
register handle selfPort = do
hPutStrLn handle ("register " ++ (show selfPort))
getPeersList handle = do
hPutStrLn handle "peers"
peers <- hGetLine handle
putStrLn peers
sendDataToPeer = do
putStrLn "Enter peer adress "
host <- getLine
putStrLn "Enter peer port "
port <- getLine
putStrLn "Data: "
input <- getLine
sendTo host (PortNumber (fromIntegral (read $ port :: Int))) input
openSocket port = do
sock <- listenOn $ PortNumber (fromIntegral port)
putStrLn ("Opened socket on port " ++ (show port))
sockHandler sock
sockHandler sock = do
(handle, host, port) <- accept sock
hSetBuffering handle NoBuffering
forkIO $ peerConnector host port handle
sockHandler sock
peerConnector host port handle = do
line <- hGetLine handle
putStrLn ("Incoming from : "++(show host)++" "++(show port) )
putStrLn line
peerConnector host port handle
main :: IO ()
main = do
[serverAddr, serverPort, port] <- getArgs
let selfPort = (read $ port :: Int)
forkIO $ openSocket selfPort
connectToServer serverAddr (read $ serverPort :: Int) selfPort