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
------------------------------------------------------------------------------
--- Configurations of the implementation of a Curry Port Name Server (CPNS)
--- based on raw sockets. The CPNS allows the connection to port by
--- symbolic names.
---
--- If we connect to a port with symbolic name `pn`, we first connect
--- to the CPNS of the host named by `pn` to get the physical socket
--- number of this port. In order to connect to CPNS from any
--- machine in the world, the CPNS demon always listens at port `cpnsSocket`.
--- Note that this port must be identical for all machines running
--- `Network.NamedSocket` or Distributed Curry! If this port is occupied
--- by another process on a host, you cannot run CPNSD on it.
---
--- @author Michael Hanus
--- @version June 2019
------------------------------------------------------------------------------

module Network.CPNS.Config
  ( cpnsSocket, cpnsTimeOut
  , getCPNSD, getStartupLockFile, getLogFile, addLogLn
  )
 where

import Directory    ( doesFileExist, getTemporaryDirectory )
import FilePath     ( (</>) )
import List         ( splitOn )
import System       ( getEnviron )

import Network.CPNS.ConfigPackage ( packageExecutable )


-- The standard port number of CPNS demon.
cpnsSocket :: Int
cpnsSocket = 8769

-- The time out before considering the server as unreachable:
cpnsTimeOut :: Int
cpnsTimeOut = 3000

--- Gets name of lock file to coordinate the startup of the CPNS demon.
getStartupLockFile :: IO String
getStartupLockFile = do
  tmp <- getTemporaryDirectory
  return $ tmp </> "Curry_CPNSD.lock"

--- Returns the executable of the CPNS demon.
--- Raises an error if the executable does not exist.
getCPNSD :: IO String
getCPNSD = do
  let cpnsbin = packageExecutable
  exbin <- doesFileExist cpnsbin
  if exbin
    then return cpnsbin
    else error "CPNS demon not found in path! Install it by: cypm install cpns"

--- Get name of CPNSD log file.
getLogFile :: IO String
getLogFile = do
  tmp <- getTemporaryDirectory
  return $ tmp </> "Curry_CPNSD.log"

--- Adds a line to the log file
addLogLn :: String -> IO ()
addLogLn s = do
  logfile <- getLogFile
  appendFile logfile (s ++ "\n")