- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 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
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ServakPro;
 
/**
 *
 * @author bb
 */
import Debugger.NetDebugger;
import Debugger.NetDebuggingInter;
import Servak.Db.Experimental.SettingsBandit;
import java.io.*;
import java.net.*;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
 
public class Server extends Object implements NetDebuggingInter {
 
  static final int INITIALWORKERTHREADS = 20;// tune this!
  static final int port = SettingsBandit.SERVER_PORT;
  BlockingQueue<Socket> queue;
  private ServerSocket ss;
  private Thread internalThread;
  private volatile boolean noStopRequested;
  private NetDebugger netDebugger = new NetDebugger();
 
  private void init() {
  for (int i = 0; i < INITIALWORKERTHREADS; i++) {
  new Thread(new MultiSessionTask(ss, queue)).start();
  }
  }
 
  public Server(BlockingQueue<Socket> q) throws IOException {
  ss = new ServerSocket(port);
  noStopRequested = true;
  this.queue = q;
  this.init();
 
  Runnable r = new Runnable() {
  public void run() {
  try {
  runWorkFast();
  } catch (Exception x) {
  netDebugger.fireServerFaultEvent("SERVER: there is exception: ", x);
  x.printStackTrace();
  }
  }
  };
  internalThread = new Thread(r);
  internalThread.setName("My server Thread");
  internalThread.start();
  }
 
  private void runWorkFast() {
  for (;;) {
  try {
  Socket sock = ss.accept();
  int thr = MultiSessionTask.waitCount();
  if (thr > 0) {
  queue.add(sock);
  } else {
  queue.add(sock);
  new Thread(new DynamicMultiSessionTask(ss, queue)).start();
  }
  } catch (IOException exc) {
  exc.printStackTrace();
  }
  if (ss.isClosed()) {
  break; // for (;;)
  }
  } // catch    
  }
 
  public synchronized void stopRequest() {
  noStopRequested = false;
  //internalThread.interrupt();
  if (ss != null) {
  try {
  ss.close();
  } catch (IOException x) {
  // ignore
  } finally {
  ss = null;
  }
  }
  }
 
  public boolean isAlive() {
  return internalThread.isAlive();
  }
 
  public static void main(String[] args) {
  try {
  BlockingQueue<Socket> bq = new ArrayBlockingQueue<>(40);
  Server serv = new Server(bq);
  } catch (IOException ex) {
                                 
        
Комментарии (0) RSS
Добавить комментарий