OSDN Git Service

84fa2fb0de3f06f55c1f76655a0c6396391c807a
[bandwidth/bandwidth.git] / src / net / excentrics / bandWidth / sample / OneFileWebServer.java
1 /*
2 Copyright 2009 senju@users.sourceforge.jp
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8     http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15  */
16
17 package net.excentrics.bandWidth.sample;
18
19 import java.io.FileInputStream;
20 import java.io.IOException;
21 import java.net.InetSocketAddress;
22 import java.nio.ByteBuffer;
23 import java.nio.channels.FileChannel;
24 import java.nio.channels.ServerSocketChannel;
25 import java.nio.channels.SocketChannel;
26
27 import net.excentrics.bandWidth.SocketChannelWriter;
28
29 /**
30  * サンプルのWEBサーバー
31  * 
32  * コマンドライン引数
33  * <table>
34  * <tr>
35  * <td>1</td>
36  * <td>帯域幅(Kbps)</td>
37  * </tr>
38  * <tr>
39  * <td>2</td>
40  * <td>送信するファイルのパス(jpeg)</td>
41  * </tr>
42  * </table>
43  * 
44  * @author senju
45  * 
46  */
47 public class OneFileWebServer {
48
49         /**
50          * @param args
51          *            コマンドライン引数
52          */
53         public static void main(String[] args) {
54                 int bps = Integer.parseInt(args[0]) * 1024; // kbpsをbpsに変換
55
56                 try {
57                         // 送信するファイルとバッファーのセットアップ
58                         ByteBuffer buf = ByteBuffer.allocate(8000);
59                         FileInputStream in = new FileInputStream(args[1]);
60                         FileChannel file = in.getChannel();
61
62                         // サーバーのセットアップ
63                         ServerSocketChannel ssc = ServerSocketChannel.open();
64                         ssc.socket().bind(new InetSocketAddress(8080));
65
66                         // 1コネクション受け付け
67                         SocketChannel sc = ssc.accept();
68
69                         // リクエストを受信する
70                         sc.read(buf);
71                         buf.clear();
72                         // HTTPの頭の部分を送信
73                         String respHeader = "HTTP/1.0 200 OK\nContent-Type: image/jpeg\nConnection: Close\n\n";
74                         buf.put(respHeader.getBytes("ascii"));
75                         buf.flip();
76                         sc.write(buf);
77                         buf.clear();
78
79                         // SocketChannelWriteの準備
80                         SocketChannelWriter scw = new SocketChannelWriter(sc, bps);
81                         long sleeptime = 0;
82                         boolean isEof = false;
83                         do {
84                                 if (file.read(buf) == -1) {
85                                         isEof = true;
86                                 }
87                                 Thread.sleep(sleeptime);
88                                 buf.flip();
89                                 sleeptime = scw.write(buf);
90                                 buf.flip();
91
92                         } while (!isEof);
93
94                         // クローズ
95                         ssc.close();
96
97                 } catch (IOException e) {
98                         e.printStackTrace();
99                 } catch (InterruptedException e) {
100                         e.printStackTrace();
101                 }
102
103         }
104 }