Guest User

Untitled

a guest
May 22nd, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Vala 2.73 KB | None | 0 0
  1.  
  2. using AC;
  3.  
  4. public AC.Automata aca1;
  5. public AC.Automata aca2;
  6.  
  7. public int trigger = 0;
  8. public ulong total_strings = 0;
  9. public string? foundList;
  10.  
  11. public bool prepare () {
  12.    
  13.     stdout.printf("Start rebuild: %d\n", trigger);
  14.    
  15.     string line;
  16.     string[]? ary;
  17.    
  18.     int i = 0;
  19.    
  20.     var file = File.new_for_path ("/home/ava/tmp/import/pr_list.out");
  21.     var dos = new DataInputStream (file.read ());
  22.     while ((line = dos.read_line (null)) != null) {
  23.         line.chomp();
  24.         ary = line.down().split("||");
  25.        
  26.         if ( ary != null )
  27.         {
  28.             i++;
  29.            
  30.                 var str = AC.String () {
  31.                     id = int.parse (ary[1]),
  32.                     str = ary[0],
  33.                     length = ary[0].length
  34.                 };
  35.                 if (trigger == 0)
  36.                     aca1.add_string (str);
  37.                 else
  38.                     aca2.add_string (str);
  39.         }
  40.     };
  41.     dos.close();
  42.    
  43.     if (trigger == 0) {
  44.         aca1.build();
  45.         total_strings   = aca1.total_strings;
  46.         trigger         = 1;
  47.        
  48.         aca2.reset();
  49.     }
  50.     else {
  51.         aca2.build();
  52.         total_strings   = aca2.total_strings;
  53.         trigger         = 0;
  54.        
  55.         aca1.reset();
  56.     };
  57.    
  58.     stdout.printf("End rebuild\n");
  59.    
  60.     return true;
  61. }
  62.  
  63. public static int match_handler (Match m, void * param)
  64. {
  65.     uint j;
  66.    
  67.     for (j=0; j < m.match_num; j++)
  68.     {
  69.         if (foundList.length > 0)
  70.             foundList += ",";
  71.        
  72.         foundList += m.matched_strings[j].id.to_string();
  73.     }
  74.    
  75.     return 0;
  76. }
  77.  
  78. public void * start () {
  79.     try {
  80.         var srv = new SocketService ();
  81.         srv.add_inet_port (4801, null);
  82.         srv.incoming.connect (on_incoming_connection);
  83.         srv.start ();
  84.     } catch (GLib.Error e) {
  85.         stderr.printf ("%s\n", e.message);
  86.     }
  87.    
  88.     return null;
  89. }
  90.  
  91. bool on_incoming_connection (SocketConnection conn) {
  92.     process_request.begin (conn);
  93.     return true;
  94. }
  95.  
  96. async void process_request (SocketConnection conn) {
  97.     try {
  98.         if (total_strings > 0)
  99.         {
  100.             var dis = new DataInputStream (conn.input_stream);
  101.             var dos = new DataOutputStream (conn.output_stream);
  102.             string req = yield dis.read_line_async (Priority.HIGH_IDLE);
  103.            
  104.             stderr.printf("rec: %s\n", req);
  105.             if (req != "" && req.length >= 4) {
  106.                 foundList = "";
  107.                 var str = AC.String () {
  108.                     str = req,
  109.                     length = req.length
  110.                 };
  111.                 stdout.printf("sea: %s\n", req);
  112.                 if (trigger == 0)
  113.                     aca2.search (str, null);
  114.                 else
  115.                     aca1.search (str, null);
  116.                
  117.                 dos.put_string (foundList);
  118.             }
  119.         }
  120.     } catch (GLib.Error e) {
  121.         stderr.printf ("%s\n", e.message);
  122.     }
  123. }
  124.  
  125. public void * update () {
  126.     Timeout.add_seconds (25, prepare);
  127.    
  128.     return null;
  129. }
  130.  
  131. void main (string[] args)
  132. {
  133.     aca1 = AC.Automata (match_handler);
  134.     aca2 = AC.Automata (match_handler);
  135.    
  136.     try {
  137.         Thread.create<void*> (update, false);
  138.         Thread.create<void*> (start, false);
  139.        
  140.     } catch (ThreadError e) {
  141.         stderr.printf ("%s\n", e.message);
  142.     };
  143.    
  144.     new MainLoop ().run ();
  145. }
Add Comment
Please, Sign In to add comment