/*
* test_bittube.cpp
*
* Created on: 2015年7月13日
* Author: ting.guit
*/
#include <binder/IPCThreadState.h>
#include <binder/ProcessState.h>
#include <binder/IServiceManager.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/epoll.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include "IBittubeService.h"
#include <gui/BitTube.h>
using namespace android;
sp<IBittubeService> service;
typedef void *Thr_Fun(void *);
void create_thread(Thr_Fun *fun ){
pthread_t pthread_id;
int ret = 0;
ret = pthread_create(&pthread_id, NULL, fun, NULL);
if( ret ) {
printf("create thread failed %d\n", ret);
exit(1);
}
}
void * _start(void *)
{
service->startLoop();
printf("bittube_service startLoop \n");
return 0;
}
int main()
{
sp < IBinder > b = defaultServiceManager()->getService(
String16("bittube_service"));
if (b == NULL) {
printf("bittube_service binder null\n");
exit(1);
}
service = interface_cast < IBittubeService > (b);
int sfd = service->getFd();
ALOGD("test-------%d",sfd);
Parcel p;
// p.writeFileDescriptor(sfd);
//BitTube *bit = new BitTube(p);
// char buff[128] = {0};
//
// service->startLoop();
//
// while(1) {
// ::recv(sfd,buff,sizeof(buff),MSG_DONTWAIT);
// ALOGD("test-------%s",buff);
// }
//BitTube::recvBuffer(bit,buff,sizeof(buff));
//return 0;
struct epoll_event event;
struct epoll_event* events;
int efd = epoll_create1(0);
if (efd == -1) {
perror("epoll_create");
abort();
}
event.data.fd = sfd;
event.events = EPOLLIN | EPOLLET;
int s = epoll_ctl(efd, EPOLL_CTL_ADD, sfd, &event);
if (s == -1) {
perror("epoll_ctl");
abort();
}
#define MAXEVENTS 64
/* Buffer where events are returned */
events = (epoll_event*)calloc(MAXEVENTS, sizeof event);
create_thread(_start);
int done;
/* The event loop */
while (1) {
int n, i;
n = epoll_wait(efd, events, MAXEVENTS, -1);
ALOGD("test-------%d",n);
for (i = 0; i < n; i++) {
if ((events[i].events & EPOLLERR) || (events[i].events & EPOLLHUP)
|| (!(events[i].events & EPOLLIN))) {
/* An error has occured on this fd, or the socket is not
ready for reading (why were we notified then?) */
fprintf(stderr, "epoll error\n");
close(events[i].data.fd);
continue;
} else if(sfd == events[i].data.fd)
{
ssize_t count;
char buf[256] = {0};
count = read(events[i].data.fd, buf, sizeof buf);
if (count == -1) {
/* If errno == EAGAIN, that means we have read all
data. So go back to the main loop. */
if (errno != EAGAIN) {
perror("read");
done = 1;
}
break;
} else if (count == 0) {
/* End of file. The remote has closed the
connection. */
done = 1;
break;
}
ALOGD("test-------%s,%d",buf,count);
}
}
sleep(1);
}
free(events);
close(sfd);
return EXIT_SUCCESS;
}