use pipe to contact with each other
2008-09-01 09:35
295 查看
1.目的:(1)在通话建立时候,建立点对点的通话管道,(2)在A对B说话的时候,将对话压入B管道,反之同样,(3)在通话结束的时候切断管道
2.由于时间比较紧迫,限制最大管道数量200
3.代码如下package PUB;
import java.io.*;
public class PipeManager
{
private static PipedInputStream[] pipedIS ;
private static PipedOutputStream[] pipedOS ;
private static int[] inchat ;
private static int maxpipe=20;
private static String[] mysessions ;
private static PipeManager instance = null;
private PipeManager()
{
mysessions = new String[maxpipe];
pipedIS = new PipedInputStream[maxpipe];
pipedOS = new PipedOutputStream[maxpipe];
inchat = new int[maxpipe];
for(int i=0;i<maxpipe;i++)
{
mysessions[i]="";
inchat[i]=0;
pipedIS[i] = new PipedInputStream();
pipedOS[i] = new PipedOutputStream();
try
{
pipedIS[i].connect(pipedOS[i]);
}
catch (IOException e)
{
System.err.println("连接失败");
}
}
System.out.println("PipeManager init");
}
/** *//**
* @return 单一实例
*/
public static PipeManager getInstance(){
if(instance==null) instance = new PipeManager();
return instance;
}
//找到管道名touser,内容mysessionid-msg
public synchronized boolean writemsg(String mysessionid,String touser,String msg)
{
if(msg.length()==0) return false;
// if(1==1) return true;
int nget=-1;
int nullget=-1;
for(int i=0;i<maxpipe;i++)
{
if(mysessions[i]!=null&&mysessions[i].equals(mysessionid))
{
nget=i;
}
if(mysessions[i]==null||mysessions[i].length()==0)
{
nullget=i;
}
if(nget>=0&&nullget>=0) break;
}
if(nullget==-1)
{
//有问题
return false;
}
if(nget==-1)
{
mysessions[nullget]=mysessionid;
nget=nullget;
}
//写数据
try
{
String writestr=touser+"-"+msg;
byte[] b = writestr.getBytes("utf-8");
int len = b.length;
// 在该线程阻塞之前,有最多1024字节的数据被写入
pipedOS[nget].write( b, 0,len );
inchat[nget]++;
}
catch (IOException e)
{
System.err.println("写操作错误");
System.exit(1);
}
return true;
}
//找到管道名mysessionid,读出内容fromuser-msg
public synchronized String readmsg(String mysessionid)
{
int nget=-1;
int nullget=-1;
for(int i=0;i<maxpipe;i++)
{
if(mysessions[i]!=null&&mysessions[i].equals(mysessionid))
{
nget=i;
}
if(mysessions[i]==null||mysessions[i].length()==0)
{
nullget=i;
}
if(nget>=0&&nullget>=0) break;
}
if(nget==-1) return "";
if( inchat[nget]>0) inchat[nget]--;
else return "";
//读取数据
byte[] inArray = new byte[1000];
int bytesRead = 0;
try
{
int canread = pipedIS[nget].available();
bytesRead = pipedIS[nget].read(inArray, 0, canread);
System.out.println("已经读取" + bytesRead + "字节...");
if(bytesRead>0)
{
return new String(inArray,"utf-8");
}
}
catch (IOException e)
{
System.err.println("读取输入错误.");
System.exit(1);
}
return "";
}
//删除管道,找到对应管道对,close,并=null
public boolean removepipe(String mysessionid)
{
int nget=-1;
int nullget=-1;
for(int i=0;i<maxpipe;i++)
{
if(mysessions[i]!=null&&mysessions[i].equals(mysessionid))
{
nget=i;
}
if(mysessions[i]==null||mysessions[i].length()==0)
{
nullget=i;
}
if(nget>=0&&nullget>=0) break;
}
if(nget==-1) return false;
readmsg(mysessionid);
mysessions[nget]="";
return true;
}
public int getinchat(String mysessionid)
{
int nget=-1;
int nullget=-1;
for(int i=0;i<maxpipe;i++)
{
if(mysessions[i]!=null&&mysessions[i].equals(mysessionid))
{
nget=i;
}
if(mysessions[i]==null||mysessions[i].length()==0)
{
nullget=i;
}
if(nget>=0&&nullget>=0) break;
}
if(nget==-1) return 0;
if( inchat[nget]>0) return inchat[nget];
else return 0;
}
}
2.由于时间比较紧迫,限制最大管道数量200
3.代码如下package PUB;
import java.io.*;
public class PipeManager
{
private static PipedInputStream[] pipedIS ;
private static PipedOutputStream[] pipedOS ;
private static int[] inchat ;
private static int maxpipe=20;
private static String[] mysessions ;
private static PipeManager instance = null;
private PipeManager()
{
mysessions = new String[maxpipe];
pipedIS = new PipedInputStream[maxpipe];
pipedOS = new PipedOutputStream[maxpipe];
inchat = new int[maxpipe];
for(int i=0;i<maxpipe;i++)
{
mysessions[i]="";
inchat[i]=0;
pipedIS[i] = new PipedInputStream();
pipedOS[i] = new PipedOutputStream();
try
{
pipedIS[i].connect(pipedOS[i]);
}
catch (IOException e)
{
System.err.println("连接失败");
}
}
System.out.println("PipeManager init");
}
/** *//**
* @return 单一实例
*/
public static PipeManager getInstance(){
if(instance==null) instance = new PipeManager();
return instance;
}
//找到管道名touser,内容mysessionid-msg
public synchronized boolean writemsg(String mysessionid,String touser,String msg)
{
if(msg.length()==0) return false;
// if(1==1) return true;
int nget=-1;
int nullget=-1;
for(int i=0;i<maxpipe;i++)
{
if(mysessions[i]!=null&&mysessions[i].equals(mysessionid))
{
nget=i;
}
if(mysessions[i]==null||mysessions[i].length()==0)
{
nullget=i;
}
if(nget>=0&&nullget>=0) break;
}
if(nullget==-1)
{
//有问题
return false;
}
if(nget==-1)
{
mysessions[nullget]=mysessionid;
nget=nullget;
}
//写数据
try
{
String writestr=touser+"-"+msg;
byte[] b = writestr.getBytes("utf-8");
int len = b.length;
// 在该线程阻塞之前,有最多1024字节的数据被写入
pipedOS[nget].write( b, 0,len );
inchat[nget]++;
}
catch (IOException e)
{
System.err.println("写操作错误");
System.exit(1);
}
return true;
}
//找到管道名mysessionid,读出内容fromuser-msg
public synchronized String readmsg(String mysessionid)
{
int nget=-1;
int nullget=-1;
for(int i=0;i<maxpipe;i++)
{
if(mysessions[i]!=null&&mysessions[i].equals(mysessionid))
{
nget=i;
}
if(mysessions[i]==null||mysessions[i].length()==0)
{
nullget=i;
}
if(nget>=0&&nullget>=0) break;
}
if(nget==-1) return "";
if( inchat[nget]>0) inchat[nget]--;
else return "";
//读取数据
byte[] inArray = new byte[1000];
int bytesRead = 0;
try
{
int canread = pipedIS[nget].available();
bytesRead = pipedIS[nget].read(inArray, 0, canread);
System.out.println("已经读取" + bytesRead + "字节...");
if(bytesRead>0)
{
return new String(inArray,"utf-8");
}
}
catch (IOException e)
{
System.err.println("读取输入错误.");
System.exit(1);
}
return "";
}
//删除管道,找到对应管道对,close,并=null
public boolean removepipe(String mysessionid)
{
int nget=-1;
int nullget=-1;
for(int i=0;i<maxpipe;i++)
{
if(mysessions[i]!=null&&mysessions[i].equals(mysessionid))
{
nget=i;
}
if(mysessions[i]==null||mysessions[i].length()==0)
{
nullget=i;
}
if(nget>=0&&nullget>=0) break;
}
if(nget==-1) return false;
readmsg(mysessionid);
mysessions[nget]="";
return true;
}
public int getinchat(String mysessionid)
{
int nget=-1;
int nullget=-1;
for(int i=0;i<maxpipe;i++)
{
if(mysessions[i]!=null&&mysessions[i].equals(mysessionid))
{
nget=i;
}
if(mysessions[i]==null||mysessions[i].length()==0)
{
nullget=i;
}
if(nget>=0&&nullget>=0) break;
}
if(nget==-1) return 0;
if( inchat[nget]>0) return inchat[nget];
else return 0;
}
}
相关文章推荐
- Enabling HDFS/YARN HA and Other CDH Components to Use HDFS HA with Cloudera Manager 5.2
- How to specify image folder path for each user when use TinyMce ImageManager with Symfony
- How to create aligned partitions in Linux for use with NetApp LUNs, VMDKs, VHDs and other virtual di
- udp_socket with thread to contect with each other
- 【转载】Allowing applications to play nice(r) with each other: Handling remote control buttons
- Use pthread_exit() To Terminate main() With The Other Threads Alive
- linker command failed with exit code 1 (use -v to see invocation)错误调试
- error: linker command failed with exit code 1 (use -v to see invocation)怎么解决
- 添加第三方类库造成的linker command failed with exit code 1 (use -v to see invocation)的错误调试
- Xcode编译常见错误解决:linker command failed with exit code 1 (use -v to see invocation)
- Duplicate symbol issues/////linker command failed with exit code 1 (use -v to see invocation)
- clang: error: linker command failed with exit code 1 (use -v to see invocation)
- sizeWithFont:constrainedToSize:' is deprecated: first deprecated in iOS 7.0 - Use -boundingRectWithS
- 在线平互动台活动启动《Discover How to Make the Computer Easier to Use with Windows Vista》
- java.lang.IllegalArgumentException: You need to use a Theme.AppCompat theme (or descendant) with ...
- LINK : warning LNK4098: defaultlib "LIBC" conflicts with use of other libs; use /NODEFAULTLIB:library
- symbol(s) not found for architecture x86_64 之 linker command failed with exit code 1 (use -v to see invocation)解决方案排查
- 使用SVN clang: error: linker command failed with exit code 1 (use -v to see invocation)
- Xampp error:Port 80 in use by "Unable to open process" with PID 4!
- 关于You need to use a Theme.AppCompat theme (or descendant) with this activity的问题