Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

124
LINES

< > BotCompany Repo | #1001066 // startDialogServer etc.

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (11032L/63K).

1  
static new AtomicInteger dialogServer_clients;
2  
static boolean dialogServer_printConnects;
3  
static new ThreadLocal<Bool> startDialogServer_quiet;
4  
5  
static Set<S> dialogServer_knownClients = synchroTreeSet();
6  
7  
static int startDialogServerOnPortAbove(int port, IDialogHandler handler) {
8  
  while (!forbiddenPort(port) && !startDialogServerIfPortAvailable(port, handler))
9  
    ++port;
10  
  return port;
11  
}
12  
13  
static int startDialogServerOnPortAboveDaemon(int port, IDialogHandler handler) {
14  
  while (!forbiddenPort(port) && !startDialogServerIfPortAvailable(port, handler, true))
15  
    ++port;
16  
  return port;
17  
}
18  
19  
static void startDialogServer(int port, IDialogHandler handler) {
20  
  if (!startDialogServerIfPortAvailable(port, handler))
21  
    fail("Can't start dialog server on port " + port);
22  
}
23  
24  
static boolean startDialogServerIfPortAvailable(int port, final IDialogHandler handler) {
25  
  return startDialogServerIfPortAvailable(port, handler, false);
26  
}
27  
28  
static ServerSocket startDialogServer_serverSocket;
29  
  
30  
static boolean startDialogServerIfPortAvailable(int port, final IDialogHandler handler, boolean daemon) {
31  
  ServerSocket serverSocket = null;
32  
  try {
33  
    serverSocket = new ServerSocket(port);
34  
  } catch (IOException e) {
35  
    // probably the port number is used - let's assume there already is a chat server.
36  
    return false;
37  
  }
38  
  final ServerSocket _serverSocket = serverSocket;
39  
  startDialogServer_serverSocket = serverSocket;
40  
41  
  Thread thread = new Thread("Socket accept port " + port) { public void run() {
42  
   try {
43  
    while (true) {
44  
      try {
45  
        final Socket s = _serverSocket.accept();
46  
        
47  
        S client = s.getInetAddress().toString();
48  
        if (!dialogServer_knownClients.contains(client) && neq(client, "/127.0.0.1")) {
49  
          print("connect from " + client + " - clients: " + dialogServer_clients.incrementAndGet());
50  
          dialogServer_knownClients.add(client);
51  
        }
52  
        
53  
        String threadName = "Handling client " + s.getInetAddress();
54  
55  
        Thread t2 = new Thread(threadName) {
56  
         public void run() {
57  
          try {
58  
            final Writer w = new OutputStreamWriter(s.getOutputStream(), "UTF-8");
59  
            final BufferedReader in = new BufferedReader(
60  
              new InputStreamReader(s.getInputStream(), "UTF-8"));
61  
              
62  
            DialogIO io = new DialogIO() {
63  
            
64  
              // This should be the same as #1001076 (talkTo)
65  
            
66  
  boolean isLocalConnection() {
67  
    return s.getInetAddress().isLoopbackAddress();
68  
  }
69  
  
70  
  boolean isStillConnected() {
71  
    return !(eos || s.isClosed());
72  
  }
73  
  
74  
  void sendLine(String line) ctex {
75  
    w.write(line + "\n");
76  
    w.flush();
77  
  }
78  
  
79  
  S readLineImpl() ctex {
80  
    ret in.readLine();
81  
  }
82  
  
83  
  public void close() {
84  
    try {
85  
      s.close();
86  
    } catch (IOException e) {
87  
      // whatever
88  
    }
89  
  }
90  
  
91  
  Socket getSocket() {
92  
    return s;
93  
  }
94  
  };
95  
            
96  
            try {
97  
              handler.run(io);
98  
            } finally {
99  
              if (!io.noClose)
100  
                s.close();
101  
            }
102  
          } catch (IOException e) {
103  
            print("[internal] " + e);
104  
          } finally {
105  
            //print("client disconnect - " + dialogServer_clients.decrementAndGet() + " remaining");
106  
          }
107  
         }
108  
        }; // Thread t2
109  
        t2.setDaemon(true); // ?
110  
        t2.start();
111  
      } catch (SocketTimeoutException e) {
112  
      }
113  
    }   
114  
   } catch (IOException e) {
115  
     print("[internal] " + e);
116  
   }
117  
  }};
118  
  if (daemon) thread.setDaemon(true);
119  
  thread.start();
120  
 
121  
  if (!isTrue(getAndClearThreadLocal(startDialogServer_quiet)))
122  
    print("Dialog server on port " + port + " started."); 
123  
  return true;
124  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1001066
Snippet name: startDialogServer etc.
Eternal ID of this version: #1001066/7
Text MD5: 4e13054297497ea711b6edec78673978
Transpilation MD5: bbbe1a21e488c420b31f9c5dd2d3ce52
Author: stefan
Category:
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-05-28 23:10:19
Source code size: 3787 bytes / 124 lines
Pitched / IR pitched: No / No
Views / Downloads: 810 / 2938
Version history: 6 change(s)
Referenced in: [show references]