| 
									
										
										
										
											2008-02-05 17:36:11 -05:00
										 |  |  | ! Copyright (C) 2003, 2008 Slava Pestov. | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | ! See http://factorcode.org/license.txt for BSD license. | 
					
						
							| 
									
										
										
										
											2008-05-05 04:51:41 -04:00
										 |  |  | USING: io io.sockets io.files io.streams.duplex logging | 
					
						
							|  |  |  | continuations kernel math math.parser namespaces parser | 
					
						
							|  |  |  | sequences strings prettyprint debugger quotations calendar | 
					
						
							| 
									
										
										
										
											2008-05-13 21:04:57 -04:00
										 |  |  | threads concurrency.combinators assocs fry ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | IN: io.server | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-02-19 15:38:02 -05:00
										 |  |  | SYMBOL: servers | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <PRIVATE
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-02-07 18:07:43 -05:00
										 |  |  | LOG: accepted-connection NOTICE | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-05-13 19:24:46 -04:00
										 |  |  | SYMBOL: remote-address | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-05-13 21:04:57 -04:00
										 |  |  | : with-connection ( client remote quot -- )
 | 
					
						
							|  |  |  |     '[ | 
					
						
							|  |  |  |         , [ remote-address set ] [ accepted-connection ] bi
 | 
					
						
							|  |  |  |         @ | 
					
						
							|  |  |  |     ] with-stream ; inline
 | 
					
						
							| 
									
										
										
										
											2008-02-07 18:07:43 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-05-13 19:24:46 -04:00
										 |  |  | \ with-connection DEBUG add-error-logging | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-02-01 23:47:01 -05:00
										 |  |  | : accept-loop ( server quot -- )
 | 
					
						
							| 
									
										
										
										
											2008-02-07 18:07:43 -05:00
										 |  |  |     [ | 
					
						
							| 
									
										
										
										
											2008-05-13 21:04:57 -04:00
										 |  |  |         >r accept r> '[ , , , with-connection ] "Client" spawn drop
 | 
					
						
							| 
									
										
										
										
											2008-02-07 18:07:43 -05:00
										 |  |  |     ] 2keep accept-loop ; inline
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-02-25 14:54:35 -05:00
										 |  |  | : server-loop ( addrspec encoding quot -- )
 | 
					
						
							| 
									
										
										
										
											2008-02-19 15:38:02 -05:00
										 |  |  |     >r <server> dup servers get push r> | 
					
						
							| 
									
										
										
										
											2008-05-13 21:04:57 -04:00
										 |  |  |     '[ , accept-loop ] with-disposal ; inline
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-02-19 15:38:02 -05:00
										 |  |  | \ server-loop NOTICE add-error-logging | 
					
						
							| 
									
										
										
										
											2008-02-07 18:07:43 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-02-19 15:38:02 -05:00
										 |  |  | PRIVATE>
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : local-server ( port -- seq )
 | 
					
						
							|  |  |  |     "localhost" swap t resolve-host ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : internet-server ( port -- seq )
 | 
					
						
							|  |  |  |     f swap t resolve-host ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-02-25 16:10:14 -05:00
										 |  |  | : with-server ( seq service encoding quot -- )
 | 
					
						
							| 
									
										
										
										
											2008-03-07 18:21:20 -05:00
										 |  |  |     V{ } clone servers [ | 
					
						
							| 
									
										
										
										
											2008-05-13 21:04:57 -04:00
										 |  |  |         '[ , [ , , server-loop ] with-logging ] parallel-each | 
					
						
							| 
									
										
										
										
											2008-03-07 18:21:20 -05:00
										 |  |  |     ] with-variable ; inline
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-02-11 17:10:03 -05:00
										 |  |  | : stop-server ( -- )
 | 
					
						
							| 
									
										
										
										
											2008-05-01 22:42:51 -04:00
										 |  |  |     servers get dispose-each ;
 | 
					
						
							| 
									
										
										
										
											2008-02-11 17:10:03 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-02-19 15:38:02 -05:00
										 |  |  | <PRIVATE
 | 
					
						
							| 
									
										
										
										
											2008-02-07 18:07:43 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-02-19 15:38:02 -05:00
										 |  |  | LOG: received-datagram NOTICE | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : datagram-loop ( quot datagram -- )
 | 
					
						
							|  |  |  |     [ | 
					
						
							| 
									
										
										
										
											2008-02-07 18:07:43 -05:00
										 |  |  |         [ receive dup received-datagram >r swap call r> ] keep
 | 
					
						
							| 
									
										
										
										
											2008-05-13 21:04:57 -04:00
										 |  |  |         pick [ send ] [ 3drop ] if
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  |     ] 2keep datagram-loop ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : spawn-datagrams ( quot addrspec -- )
 | 
					
						
							| 
									
										
										
										
											2008-01-31 01:52:06 -05:00
										 |  |  |     <datagram> [ datagram-loop ] with-disposal ; inline
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-02-07 18:07:43 -05:00
										 |  |  | \ spawn-datagrams NOTICE add-input-logging | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-02-19 15:38:02 -05:00
										 |  |  | PRIVATE>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | : with-datagrams ( seq service quot -- )
 | 
					
						
							| 
									
										
										
										
											2008-05-13 21:04:57 -04:00
										 |  |  |     '[ [ , _ spawn-datagrams ] parallel-each ] with-logging ; inline
 |