46 #include "EST_Pathname.h"
47 #include "EST_socket.h"
48 #include "EST_error.h"
49 #include "EST_Token.h"
50 #include "EST_ServiceTable.h"
52 #if defined(SYSTEM_IS_WIN32)
53 # define seed_random() srand((unsigned)time(NULL))
54 # define get_random() rand()
56 # define seed_random() srandom(time(NULL));
57 # define get_random() random()
60 #if defined(__sun__) && defined(__sparc__) && defined(__svr4__)
64 #if defined(older_solaris)
65 int srandom(
unsigned seed);
66 int gethostname(
char *name,
int namelen);
72 EST_ServiceTable::EntryTable EST_ServiceTable::entries;
73 bool EST_ServiceTable::random_init=FALSE;
75 void EST_ServiceTable::init_random(
void)
115 if ((sfile = fopen(socketsFileName,
"r"))==NULL)
118 if (str.
open(sfile, 1))
119 EST_sys_error(
"Can't access fringe file '%s'",
120 (
const char *)socketsFileName);
145 if (!entries.t.present(name))
149 entries.t.add_item(name, newent);
152 Entry &entry = entries.t.val(name);
156 else if (type==
"address")
158 else if (type==
"type")
160 else if (type==
"port")
162 else if (type==
"cookie")
165 EST_warning(
"Unknown entry field '%s' at %s",
177 if ((f=fopen(filename,
"w"))==NULL)
178 EST_sys_error(
"can't write serice file");
180 fprintf(f,
"#Services\n");
184 for(p.
begin(entries.t); p ; ++p)
187 const Entry &entry = p->v;
189 fprintf(f,
"%s.type=%s\n", (
const char *)name, (
const char *)entry.
type);
190 fprintf(f,
"%s.cookie=%s\n", (
const char *)name, (
const char *)entry.
cookie);
191 fprintf(f,
"%s.host=%s\n", (
const char *)name, (
const char *)entry.
hostname);
192 fprintf(f,
"%s.address=%s\n", (
const char *)name, (
const char *)entry.
address);
193 fprintf(f,
"%s.port=%d\n", (
const char *)name, entry.
port);
202 EST_Pathname socketsFileName(getenv(
"HOME")?getenv(
"HOME"):
"/");
204 socketsFileName +=
".estServices";
206 read_table(socketsFileName);
211 EST_Pathname socketsFileName(getenv(
"HOME")?getenv(
"HOME"):
"/");
213 socketsFileName +=
".estServices";
215 write_table(socketsFileName);
234 s <<
"Entries of type " << type <<
":\n";
243 sprintf(buff,
"%10s",
"Name");
245 sprintf(buff,
"%30s",
" Hostname");
247 sprintf(buff,
"%20s",
" IP Address");
249 sprintf(buff,
"%5s\n",
"Port");
252 for(them.
begin(entries.t); them; them++)
254 const Entry &entry = them->v;
256 if (type !=
"" && entry.
type != type)
260 sprintf(buff,
"%10s",(
const char *)entry.
name);
262 sprintf(buff,
"%30s",(
const char *)entry.
hostname);
264 sprintf(buff,
"%20s",(
const char *)entry.
address);
266 s << entry.
port <<
"\n";
276 for(them.
begin(entries.t); them; them++)
277 if (type==
"" || type == them->v.name)
286 if (entries.t.present(name))
288 Entry &entry = entries.t.val(name);
290 if (entry.type == type)
294 return *(entries.t.default_val);
303 long cookie = get_random();
304 struct sockaddr_in sin;
306 socklen_t size=
sizeof(
struct sockaddr_in);
310 if (getsockname(socket, (
struct sockaddr *)&sin, &size) != 0)
311 EST_sys_error(
"Can't find my address");
317 gethostname(myname, 100);
319 struct hostent *hent = gethostbyname(myname);
322 EST_sys_error(
"Can't look up my address");
325 if (hent->h_addr_list != NULL)
326 memcpy(&(sin.sin_addr.s_addr),hent->h_addr_list[0], sizeof (sin.sin_addr.s_addr));
333 entry.
port = ntohs(sin.sin_port);
337 entries.t.add_item(name, entry);
339 return entries.t.val(name);
344 #if defined(INSTANTIATE_TEMPLATES)
346 #include "../base_class/EST_TList.cc"
347 #include "../base_class/EST_TSortable.cc"
348 #include "../base_class/EST_TKVL.cc"
EST_String type
Type of server (eg `fringe')
EST_TokenStream & get(EST_Token &t)
get next token in stream
static EST_String Number(int i, int base=10)
Build string from an integer.
const Entry & next_element()
Return the current element and move the pointer forwards.
void set_SingleCharSymbols(const EST_String &sc)
set which characters are to be treated as single character symbols
void close(void)
Close stream.
EST_String hostname
Human readable hostname.
int open(const EST_String &filename)
open a EST_TokenStream for a file.
static EST_String cat(const EST_String s1, const EST_String s2=Empty, const EST_String s3=Empty, const EST_String s4=Empty, const EST_String s5=Empty, const EST_String s6=Empty, const EST_String s7=Empty, const EST_String s8=Empty, const EST_String s9=Empty)
EST_String name
Name of the server.
static void write_table(void)
Write the users default table. ~/.estServices
Entry()
Create an empty entry.
EST_String address
Numeric IP address (###.###.###.###)
static void list(ostream &s, const EST_String type)
List the table to given stream.
void append(const T &item)
add item onto end of list
static void read_table(void)
Read the users default table. ~/.estServices
A global table of known services. Used for fringe and festival servers.
void begin(const Container &over)
Set the iterator ready to run over this container.
const EST_String pos_description()
A string describing current position, suitable for error messages.
EST_Token get_upto_eoln(void)
get up to s in end of line as a single token.
static void names(EST_TList< EST_String > &names, const EST_String type="")
Return a list of server names.
EST_String cookie
A random string to send as authorisation.
static const Entry & create(const EST_String name, const EST_String type, int socket)
void clear(void)
remove all items in list
bool has_more_elements() const
True if there are more elements to look at.