int block, block_size ;
int block_display, block_pane, block_enter_icon, block_socket ;
int connections ;

int display, pane, enter_icon ;
int sockets[10] ;

void socket_connected(int handle; int new)
{
  int n ;

  text_to_display(display, 0, "connected", "") ;
  text_to_display(display, 0, int_to_string(new), "") ;
  define_socket_to_text(new, "\0d\0a", 2, 1) ;
  for (n=0; sockets[n] >= 0 && n < 10; n=n+1 ;)  ;
  if (n < 10)
  {
    sockets[n]=new ;
  }
  else
  {
    line_to_socket(new, "The maximum number of concurrent users has been reached, please try again later") ;
    close_socket(new) ;
  }
}

void relay_line(string line)
{
  int n ;

  for (n=0; n < 10; n=n+1 ;)
  {
    if (sockets[n] >= 0)
    {
      line_to_socket(sockets[n], line) ;
    }
  }
  string show ;
  show="-> " ;
  string_cat(show, line) ;
  text_to_display(display, 0, show, "") ;
}

int display_close_request(int display)
{
  relay_line("server dying") ;
  quit() ;
}

void enter_text(int display; int pane; int icon; string text)
{
  if (string_char(text, 0)='.')
  {
    int number ;

    text=mid_string(text, 1) ;

    number=string_to_int(text) ;
    if (number < 10 && number >= 0)
    {
      if (sockets[number] >= 0)
      {
        string show ;

        close_socket(sockets[number]) ;
        sockets[number]=-1 ;
        show="\030000DDline " ;
        string_cat(show, int_to_string(number)) ;
        string_cat(show, " has been disconnected") ;
        text_to_display(display, 0, show, "") ;
      }
      else
      {
        string show ;
        show="\030000DDNothing is connected to line " ;
        string_cat(show, int_to_string(number)) ;
        text_to_display(display, 0, show, "") ;
      }
    }
    else
    {
      string show ;
      show="\030000DDI do not have a line " ;
      string_cat(show, int_to_string(number)) ;
      text_to_display(display, 0, show, "") ;
    }
  }
  else
  {
    relay_line(text) ;
  }
  set_icon_text(display, pane, icon, "") ;
}

int pre_quit()
{
  relay_line("Talker dying") ;
  return 0 ;
}

void line_from_socket(int handle; string line)
{
  int n ;
  string show ;

  for (n=0; sockets[n] != handle; n=n+1 ;) ;
  show=int_to_string(n) ;
  string_cat(show, ": ") ;
  string_cat(show, line) ;
  text_to_display(display, 0, show, "") ;
}

void socket_closed(int handle)
{
  int n ;
  string send ;

  for (n=0; sockets[n] != handle; n=n+1 ;) n=n ;
  sockets[n]=-1 ;
  send=int_to_string(n) ;
  string_cat(send, " has disconnected (socket closed)") ;
  relay_line(send) ;
}

void main()
{
  init("RawServer", "Server for testing clients", "Sham Gardner", "0.00 (December 1997)", 0) ;

  display=open_display(80, 20, 100, "Raw Server") ;
  
  pane        =create_pane(display, 1072, 200, 1072, 200, 0, 16*13, 0, 0) ;
  enter_icon  =create_icon(display, pane,   0, 100, 1070, 160, 0, 0xFFFFFF00, 500, 0, 0x164, "",               "") ;

  int port, socket ;
  port=1234 ;
  socket=create_listening_socket(port) ;
  if (socket < 0)
  {
    report_error("Unable to bind port 1234, is something else using it?") ;
    quit() ;
  }

  int n ;
  for (n=0; n < 10; n=n+1 ;)
  {
    if (n=10)
    {
      report_error("wibble") ;
    }
    sockets[n]= -1 ;
  }
}
